-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveCommand.cpp
More file actions
57 lines (54 loc) · 1.57 KB
/
RemoveCommand.cpp
File metadata and controls
57 lines (54 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Author: Jon Shidal
Purpose: CSE 332 lab 5
*/
#include "RemoveCommand.h"
#include<iostream>
#include <sstream>
using namespace std;
RemoveCommand::RemoveCommand(AbstractFileSystem* fileSys) : fs(fileSys) {}
// removes a file from the current working directory
int RemoveCommand::execute(std::string& CWD, std::string options) {
string fullPath = CWD;
istringstream ss(options);
string middle_path, option;
ss >> middle_path;
if(ss>> option && option=="-r") {
middle_path = CWD + '/' + middle_path;
AbstractFile* temp = fs->openFile(middle_path);
std::vector<char> contents = temp->read();
string oneFile;
for (unsigned int i = 0; i < contents.size(); i++) {
if (contents[i] == '\n') {
string temp_path = middle_path + '/' + oneFile;
int r = fs->deleteFile(temp_path);
if (r == AbstractFileSystem::fileisopen) {
cout << oneFile << " is currently in use" << endl;
}
oneFile = "";
continue;
}
else {
oneFile += contents[i];
}
}
fs->closeFile(temp);
return success;
}
fullPath += '/';
fullPath += options;
int result = fs->deleteFile(fullPath);
if (result == AbstractFileSystem::filedoesnotexist) {
cout << "File does not exist" << endl;
return filedoesnotexist;
}
else if (result == AbstractFileSystem::fileisopen) {
cout << "File is currently in use" << endl;
return fileinuse;
}
return success;
}
void RemoveCommand::displayInfo() {
cout << "touch removes a file from the file system and then deletes the file" << endl;
cout << "Usage: rm <filename>" << endl;
}