-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.cpp
90 lines (77 loc) · 2.56 KB
/
filesystem.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "filesystem.h"
FileSystem::FileSystem(boost::filesystem::path _basepath):
basepath(_basepath), current("/")
{
}
boost::filesystem::path FileSystem::getpath(std::string& askPath) {
boost::filesystem::path path;
if(askPath[0] == '/'){
path = basepath / boost::filesystem::path(askPath);
} else {
path = basepath /current / boost::filesystem::path(askPath);
}
return path;
}
std::__cxx11::string FileSystem::pwd(){
return current.string();
}
std::string FileSystem::ls( std::string& askPath){
boost::filesystem::path path = getpath(askPath);
if (!boost::filesystem::is_directory(path)){
throw std::runtime_error("It is not directory");
}
boost::filesystem::directory_iterator files(path);
std::string res;
while(files != boost::filesystem::directory_iterator()){
res += files->path().filename().string();
res += "\r\n";
files ++;
}
return res;
}
void FileSystem::cd(std::string &askPath){
boost::filesystem::path path = getpath(askPath);
if (!boost::filesystem::is_directory(path)){
throw std::runtime_error("It is not directory");
}
if(askPath[0] == '/'){
current = boost::filesystem::path(askPath);
} else {
current /= boost::filesystem::path(askPath);
}
}
void FileSystem::mkdir(std::string &askPath){
boost::filesystem::path path = getpath(askPath);
if(!boost::filesystem::create_directory(path)){
throw std::runtime_error("error");
}
}
unsigned int FileSystem::rmdir(std::string &askPath){
boost::filesystem::path path = getpath(askPath);
unsigned int num = 0;
try{
num = boost::filesystem::remove_all(path);
}catch(std::exception& e){
std::runtime_error(e.what());
}
return num;
}
std::shared_ptr<std::fstream> FileSystem::file(std::string &askPath){
boost::filesystem::path path = getpath(askPath);
if (!boost::filesystem::is_regular_file(path)){
throw std::runtime_error("no file no problem");
}
std::shared_ptr<std::fstream> result(new std::fstream(path.string(),std::ios::binary|std::ios::in|std::ios::out));
if(!result->is_open()){
throw std::runtime_error("file open problem");
}
return result;
}
std::shared_ptr<std::fstream> FileSystem::newfile(std::string &askPath){
boost::filesystem::path path = getpath(askPath);
std::shared_ptr<std::fstream> result(new std::fstream(path.string(),std::ios::binary|std::ios::out));
if(!result->is_open()){
throw std::runtime_error("file open problem");
}
return result;
}