I have a problem with sorting. I can't make the sorting function. I tried
void SortContentDescending(Fat& fat, Data& data)
{
cout<<"Sort Down By Name"<<endl;
Item** items = fat.Sort();
string swap;
Item* swap;
int lenght=0;
for(int j=0;items[j]!=NULL;j++)
{
lenght++;
}
for(int i=0; i<lenght; i++)
{
for(int k=0; k<lenght-1-i;k++)
{
if(items[k],items[k+1])
{
swap:=items[k+1];
items[k+1]=items[k];
items[k]=swap;
}
}
}
ListContent(fat);
}
But i can't seem to get rid of errors. Can someone look it over and help me. Thanks.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include "Interfaces.cpp"
using namespace std;
void NullString(char* buff)
{
for(int i=0;i<(int)strlen(buff);i++)
buff[i]=NULL;
}
class TextManagerItem : public ManagerItem
{
private:
char fileName[FileNameLen];
int fileStart;
int fileEnd;
public:
TextManagerItem()
{
NullString(this->fileName);
this->fileStart = 0;
this->fileEnd = 0;
}
virtual char* GetFileName() { return this->fileName; }
virtual void SetFileName(char* fileName) { strcpy(this->fileName, fileName); }
int GetFileStart() { return this->fileStart; }
void SetFileStart(int fileStart) { this->fileStart = fileStart; }
int GetFileEnd() { return this->fileEnd; }
void SetFileEnd(int fileEnd) { this->fileEnd = fileEnd; }
};
class TextFatManager : public FatManager
{
private:
char dataFile[DataFileNameLen];
public:
TextFatManager(char* dataFile)
{
strcpy(this->dataFile, dataFile);
}
virtual void Add(ManagerItem*);
virtual ManagerItem* Find(char*);
virtual ManagerItem** List();
};
void TextFatManager::Add(ManagerItem* item)
{
TextManagerItem* specificItem = (TextManagerItem*)item;
ofstream out(this->dataFile, ios::app|ios::end);
out<<specificItem->GetFileName()<<endl;
out<<specificItem->GetFileStart()<<endl;
out<<specificItem->GetFileEnd()<<endl;
out.close();
}
ManagerItem* TextFatManager::Find(char* fileName)
{
char stringBuffer[FileNameLen];
int intBuffer;
TextManagerItem* result = NULL;
ifstream in(this->dataFile);
{
result = new TextManagerItem();
in>>stringBuffer;
result->SetFileName(stringBuffer);
in>>intBuffer;
result->SetFileStart(intBuffer);
in>>intBuffer;
result->SetFileEnd(intBuffer);
if (strcmp(result->GetFileName(), fileName) == 0)
break;
if (in.eof())
{
result = NULL;
break;
}
}
return (ManagerItem*)result;
}
ManagerItem** TextFatManager::List()
{
char stringBuffer[FileNameLen];
int intBuffer;
TextManagerItem** result = new TextManagerItem*[100];
ifstream* in = new ifstream(this->dataFile);
if (!in->is_open())
{
ofstream out(this->dataFile);
out.close();
in = new ifstream(this->dataFile);
}
int i=0;
while(true)
{
result[i] = new TextManagerItem();
(*in)>>stringBuffer;
result[i]->SetFileName(stringBuffer);
(*in)>>intBuffer;
result[i]->SetFileStart(intBuffer);
(*in)>>intBuffer;
result[i]->SetFileEnd(intBuffer);
if (in->eof())
{
result[i] = NULL;
break;
}
i++;
}
return (ManagerItem**)result;
}
class TextEncryptionManager
{
public:
void Encrypt(char* string)
{
for(int i=0;i<strlen(string);i++)
string[i] = string[i] + 1;
}
void Decrypt(char* string)
{
for(int i=0;i<strlen(string);i++)
string[i] = string[i] - 1;
}
};
class TextDataManager : public DataManager
{
private:
TextEncryptionManager encryptor;
char dataFile[DataFileNameLen];
public:
TextDataManager(char* dataFile)
{
strcpy(this->dataFile, dataFile);
}
virtual ManagerItem* Add(char* fileName);
virtual void Export (ManagerItem* item, char* fileName);
};
ManagerItem* TextDataManager::Add(char* fileName)
{
ifstream in(fileName);
if (!in.is_open())
return NULL;
TextManagerItem* result = new TextManagerItem();
result->SetFileName(fileName);
ofstream out(this->dataFile, ios::app|ios::end);
out.seekp(0, ios::end);
result->SetFileStart(out.tellp());
char* buff = new char[1];
while(true)
{
in.read(buff, 1);
if (in.eof())
break;
this->encryptor.Encrypt(buff);
out.write(buff, 1);
}
result->SetFileEnd(out.tellp());
in.close();
out.close();
return (ManagerItem*)result;
}
void TextDataManager::Export(ManagerItem* item, char* fileName)
{
TextManagerItem* specificItem = (TextManagerItem*)item;
ofstream out(fileName);
ifstream in(this->dataFile);
in.seekg(specificItem->GetFileStart());
char* buffer = new char[1];
int cursor = specificItem->GetFileStart();
while(true)
{
in.read(buffer, 1);
this->encryptor.Decrypt(buffer);
cursor++;
if (cursor > specificItem->GetFileEnd())
break;
out.write(buffer, 1);
}
in.close();
out.close();
}
void ListContent(FatManager& fatManager)
{
ManagerItem** items = fatManager.List();
cout<<endl;
for(int i=0;items[i] != NULL;i++)
cout<<"VirtualDrive\\"<<items[i]->GetFileName()<<endl;
if (items[0] == NULL)
cout<<"No files found!"<<endl;
cout<<endl;
}
void ExportContent(FatManager& fatManager, DataManager& dataManager)
{
char* fileName = new char[FileNameLen];
char* targetFileName = new char[FileNameLen];
cout<<"[File to Export]:";
cin>>fileName;
cout<<"[Destination File Name]:";
cin>>targetFileName;
ManagerItem* item = fatManager.Find(fileName);
if (item == NULL)
{
cout<<"File not found!"<<endl;
return;
}
dataManager.Export(item, targetFileName);
cout<<"File exported successfully"<<endl;
}
void AddContent(FatManager& fatManager, DataManager& dataManager)
{
char buffer[FileNameLen];
cout<<"[Enter fileName]:";
cin>>buffer;
ManagerItem* item = dataManager.Add(buffer);
if (item == NULL)
cout<<"File not found!"<<endl;
else
{
fatManager.Add(item);
cout<<"File successfully added!"<<endl;
}
}
void main()
{
char command[CommandLen];
NullString(command);
TextFatManager fatManager("fat.vdrv");
TextDataManager dataManager("data.vdrv");
while(true)
{
cout<<"VirtualDrive\\:>";
cin>>command;
if (strcmp(command, "dir") == 0)
ListContent(fatManager);
else if (strcmp(command, "import") == 0)
AddContent(fatManager, dataManager);
else if (strcmp(command, "export") == 0)
ExportContent(fatManager, dataManager);
else if (strcmp(command, "exit") == 0)
break;
else if (strcmp(command, "cls") == 0)
system("cls");
else
cout<<"Bad command"<<endl;
}
}