void FileCommands::edit(FileSystemUser* fileSystem, string fileName) { FileObject* fo = fileSystem->getFileObject(); fileName += "$"; int t = fo->getObject(fileName); if(t) { fo->setPos(t); string newContent; char buff[512]; cout<<"enter new content: "; cin.getline(buff, 512); newContent = buff; int begPos = fo->getBeginPos(); string sys = fileSystem->getSystem(); fo->go(fileName); int beg = fo->getPos(); fo->setEndPos(); int end = fo->getPos(); sys.erase(beg, end-beg + 1); sys.insert(beg, newContent); fileSystem->setNewSystem(sys); fo->setBeginPos(begPos); fo->setFileContent(fileName, newContent); }else { notFoundFile(fileName); return; } }
void FileCommands::cd(FileSystemUser* fileSystem, string path) { FileObject* fo = fileSystem->getFileObject(); vector<string> dirs = StringService::pathSplit(path); int oldPos = fo->getPos(); for(int i = 0; i<dirs.size(); ++i) { if(dirs[i]!="..") { if(!fo->containsObject(dirs[i])) { notFoundPath(path); fo->setPos(oldPos); return; }else { fo->go(dirs[i]); } }else { fo->goUp(); } } for(int i = 0; i<dirs.size(); ++i) { if(dirs[i] == "..") { fileSystem->cdUp(); }else { fileSystem->cdDir(dirs[i]); } } }
void FileCommands::remdir(FileSystemUser* fileSystem, string dir) { FileObject* fo = fileSystem->getFileObject(); if(!fo->containsObject(dir)) { notFoundDir(dir); return; } string sys = fileSystem->getSystem(); int t = fo->getObject(dir); cout<<"t: "<<t<<" pos:"<<fo->getPos()<<endl; if(fo->getPos() == fo->getBeginPos()) { fo->setEndPos(); } sys.erase(t, fo->getPos() - t); fileSystem->setNewSystem(sys); fo->remObj(dir); }
void FileCommands::remfile(FileSystemUser* fileSystem, string file) { FileObject* fo = fileSystem->getFileObject(); if(!fo->containsObject(file)) { notFoundFile(file); return; } string sys = fileSystem->getSystem(); int t = fo->getObject(file); sys.erase(t, fo->getPos() - t - 1); fileSystem->setNewSystem(sys); fo->remObj(file); }
void FileCommands::mkfile(FileSystemUser* fileSystem, string file) { FileObject* fo = fileSystem->getFileObject(); if(fo->containsObject(file)) { duplicateNames(file); return; } file += fileDelim; file += openDelim; file += closeDelim; fo->setEndPos(); string sys = fileSystem->getSystem(); sys.insert(fo->getPos(), file); fileSystem->setNewSystem(sys); fo->addObj(file); }
void FileCommands::mkdir(FileSystemUser* fileSystem, string dir) { FileObject* fo = fileSystem->getFileObject(); if(fo->containsObject(dir)) { duplicateNames(dir); return; } dir += "{"; dir += "}"; fo->setEndPos(); string sys = fileSystem->getSystem(); sys.insert(fo->getPos(), dir); fileSystem->setNewSystem(sys); fo->addObj(dir); }