#include #include "os.h" using namespace std; //------------------------------------------------------------------------------ //IMPORTANT: You are not allowed to remove or add any parameters to any functions. //------------------------------------------------------------------------------ /* Constructor Dynamically allocate root data. set isFolder = true. dynamically allocate Mlist for our root data as well (since it's a directory type). push our newly created data object pointer to wd and dataStack from the top. */ OS::OS(){ //is folder is part of the node data root_data = new Data; // dynamically allocate this new data root_data->isFolder = true; // it is a folder root_data->childList = new MList; // then make its childlist new as well wd.push_top(root_data); // then push it to the top of the mlist that we are currently on dataStack.push_top(root_data); } /* Destructor to clean-up memory, i will leave this for you to figure it out. */ OS::~OS(){ } /* Search a node in the current directory If one is found, return a pointer to that node If one is not found, return NULL */ Node* OS::search_item(string fname){ if(wd.isEmpty()) return NULL; Node* data = wd.top(); return wd.search(data,fname); } /* Delete a node in the current directory Note: this function only deletes (permanently) the node, not the Data obj the node points to If the item you want to delete doesn't exist in the current directly, do cout<<"Error: cannot find file or directory '"<name = fname; newfolder->isFolder= true; newfolder->childList = new MList; newfolder->size = 0; wd.push_bottom(newfolder); wd.sortByNameInsertion(); dataStack.push_bottom(newfolder); } else{ Data* newfile = new Data; newfile->name = fname; newfile->isFolder = false; newfile->childList = NULL; newfile->size = 0; wd.push_bottom(newfile); wd.sortByNameInsertion(); dataStack.push_bottom(newfile); } } } /* Read or write a file according to the boolean isRead (true = read, false = write) Things to keep in mind: 1). make sure that a file "fname" exists in our current directly, if not cout<<"Error: cannot find file '"< "; string input; getline(cin,input); then simply just set text to the input string. 5). the size of the file should be based on the length of the input string. */ void OS::file(string fname, bool isRead){ Node* data = wd.top(); data = wd.search(data,fname); if(data==NULL){ std::cout<<"Error: cannot find file or directory '"<nodeData->isFolder){ std::cout<<"Error: '"<nodeData->text< "; std::string input; std::getline(std::cin,input); data->nodeData->text = input; data->nodeData->size = input.length(); } } } //Change directory void OS::cd_dir(string fname){ if(fname == ".."){ //this option takes you up one directory (move the directory back one directory) //you should not be able to go back anymore if you are currently at root. if(wd.top() == dataStack.top()) return; // we should use the top node in wd, find it in datastack and then change wd to that in datastack Node* changer = wd.top(); // now find changer in datastack changer = dataStack.search(dataStack.top(),changer->nodeData->name)->prev; wd.clear(); // idk what to do with this } else if(fname == "~"){ //this option takes you directly to the home directory (i.e., root). Node* topOfDataStack= dataStack.top(); wd.clear(); // does this fuck shit up bad?? // wd.push_bottom(topOfDataStack->nodeData); wd = *topOfDataStack->nodeData->childList; } else{ /* This option means that you are trying to access (go into) a directory 1). check whether there exists a node with that name, if you cannot find it: cout<<"Error: cannot find directory '"<nodeData->isFolder){ std::cout<<"Error: '"<prev!=NULL){ newlist = newlist->prev; } else newlist = dataStack.top(); // now we are to the parent of the mlist wd.clear(); wd = *newlist->nodeData->childList; } } } //Print list of item in the current directory, the way you print it will be according to the passed-in option void OS::ls(string option){ if(option == "-d"){ //Default option - print the list of items in the current directory from top to bottom. //use a single space as delimiter. wd.printTtB(option); } else if(option == "-sort=name"){ //Use Insertion Sort to sort items in the current directory and print from top to bottom (alphabetically A-Za-z). //use a single space as delimiter. wd.sortByNameInsertion(); } else if(option == "-r"){ //Reverse print the list of items in the current directory (i.e., print form bottom to top). //use single space as delimiter. wd.printBtT(option); } else if(option == "-sort=size"){ //Sort list by size and print the list of items in the current directory from top to bottom. //use single space as delimiter. wd.sortBySizeSelection(); } else{ cout<<"usage: ls [optional: -r for reverse list, -sort=size to sort list by file size, -sort=name to soft list by name]"; } } //Priting path from root to your current directory. //use slash "/" as our delimiter. //Example output: root/cs103/usc/viterbi/ void OS::present_working_dir(){ }