|
| 1 | +#include "DirectorySimulator.h" |
| 2 | + |
| 3 | +DirectorySimulator::DirectorySimulator(const std::string& filename,TreeNode *currentnode) { |
| 4 | + std::ifstream file(filename); |
| 5 | + std::string line; |
| 6 | + this->node = currentnode; |
| 7 | + |
| 8 | + while (std::getline(file, line) && line != "end of dirs") { |
| 9 | + if (line == "selected dirs") { |
| 10 | + while (std::getline(file, line) && line != "end of dirs") { |
| 11 | + std::istringstream iss(line); |
| 12 | + std::string token; |
| 13 | + |
| 14 | + std::getline(iss, token, ','); |
| 15 | + std::string fullPath = token; |
| 16 | + |
| 17 | + std::getline(iss, token, ','); |
| 18 | + char operation = token.empty() ? '\0' : token[0]; |
| 19 | + |
| 20 | + std::getline(iss, token, ','); |
| 21 | + std::string time = token; |
| 22 | + |
| 23 | + std::getline(iss, token, ','); |
| 24 | + std::string size = token; |
| 25 | + |
| 26 | + // 获取去掉最后一个文件的目录路径 |
| 27 | + size_t lastSlashPos = fullPath.find_last_of("\\"); |
| 28 | + std::string parentDirectory = (lastSlashPos != std::string::npos) ? fullPath.substr(0, lastSlashPos) : ""; |
| 29 | + lastSlashPos = parentDirectory.find_last_of("\\"); |
| 30 | + parentDirectory = (lastSlashPos != std::string::npos) ? parentDirectory.substr(0, lastSlashPos) : ""; |
| 31 | + directoryOperations.push_back({fullPath,parentDirectory, operation, time, size}); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void DirectorySimulator::executeDirectoryOperations(const char *filename) { |
| 38 | + std::ofstream outputFile(filename); |
| 39 | + if (!outputFile.is_open()) |
| 40 | + { |
| 41 | + std::cerr << "Error: Unable to open the file for writing." << std::endl; |
| 42 | + return; |
| 43 | + } |
| 44 | + for (const auto& op : directoryOperations) { |
| 45 | + std::cout << "Operation: " << op.operation << " on directory: " << op.fullPath <<std::endl<<op.parentDirectory<< std::endl; |
| 46 | + // 执行目录操作的代码 |
| 47 | + |
| 48 | + TreeNode *panode = node->findDirectory(node,op.parentDirectory.c_str()); |
| 49 | + TreeNode *nownode = node->findDirectory(node,op.fullPath.c_str()); |
| 50 | + |
| 51 | + if(nownode != nullptr) |
| 52 | + { |
| 53 | + outputFile << "Deleted Directory: " << nownode->path << " (" << nownode->name << ")" << std::endl; |
| 54 | + |
| 55 | + if(panode->firstChild == nownode) |
| 56 | + { |
| 57 | + panode->firstChild = nownode->nextSibling; |
| 58 | + nownode->nextSibling = nullptr; |
| 59 | + nownode->printDeletedNodesToFile(outputFile); |
| 60 | + delete nownode; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + TreeNode *p = panode->firstChild; |
| 65 | + while(p->nextSibling != nownode) |
| 66 | + { |
| 67 | + |
| 68 | + p = p->nextSibling; |
| 69 | + } |
| 70 | + printf("%s\n",p->name);printf("%s\n",p->nextSibling->name); |
| 71 | + fflush(stdout); |
| 72 | + p->nextSibling = nownode->nextSibling; |
| 73 | + nownode->nextSibling = nullptr; |
| 74 | + nownode->printDeletedNodesToFile(outputFile); |
| 75 | + delete nownode; |
| 76 | + } |
| 77 | + std::cout<<"Success!"<<std::endl; |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + outputFile.close(); |
| 82 | +} |
0 commit comments