-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSCommand.cpp
More file actions
53 lines (49 loc) · 1.3 KB
/
LSCommand.cpp
File metadata and controls
53 lines (49 loc) · 1.3 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
/*
Author: Jon Shidal
Purpose: CSE 332 lab 5
*/
#include "LSCommand.h"
#include "ReadFileVisitor.h"
#include<iostream>
#include "ReadMetadataFileVisitor.h"
using namespace std;
LSCommand::LSCommand(AbstractFileSystem * fileSys) : fs(fileSys) {}
int LSCommand::execute(std::string& CWD, std::string options) {
AbstractFile* f = fs->openFile(CWD);
if (f == nullptr) { // file creation failed
cout << "failed to open directory" << endl;
return directorydoesnotexist;
}
if (options == "-l") {
vector<char> contents = f->read();
fs->closeFile(f);
string filename = CWD + "/";
for (unsigned int i = 0; i < contents.size(); i++) {
if (contents[i] == '\n') {
AbstractFile* f2 = fs->openFile(filename);
if (f2 == nullptr) { // file creation failed
cout << "failed to open " << filename << endl;
return directorydoesnotexist;
}
ReadMetadataFileVisitor reader2;
f2->accept(&reader2);
fs->closeFile(f2);
filename = CWD + "/";
}
else {
filename += contents[i];
}
}
return success;
}
else {
ReadFileVisitor oreader;
f->accept(&oreader);
fs->closeFile(f);
return success;
}
}
void LSCommand::displayInfo() {
cout << "lists the contents of the current directory" << endl;
cout << "Usage: ls" << endl;
}