/
file.cpp
174 lines (162 loc) · 4.38 KB
/
file.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "main.h"
#include "file.h"
#include "util.h"
#include <iterator>
#include <string>
void file::write_file(std::string file, std::string data){
std::ofstream out(file, std::ofstream::trunc);
if(out.is_open() == false){
throw std::runtime_error((std::string)"Unable to open file " + file);
}
out << data;
out.close();
}
std::string file::read_file(std::string file){
std::string retval;
if(exists(file) == false){
retval = "";
}else{
std::ifstream in(file);
if(in.is_open() == false){
throw std::runtime_error((std::string)"Unable to open file " + file);
}
std::string buffer;
while(getline(in, buffer)){
retval += buffer + "\n";
}
in.close();
}
return retval;
}
bool file::exists(std::string file){
std::string filename;
if(filename.find_first_of("/") != std::string::npos){
filename = file.substr(file.find_last_of("/"), file.size());
}else{
filename = file;
}
const int file_len = filename.size();
if(file_len >= 250){
print("file length is above 250, this is bad", P_ERR);
}else if(file_len >= 127){
print("file length is above 127, this shouldn't happen too much", P_WARN);
}
struct stat sb;
switch(stat(file.c_str(), &sb)){
case EACCES:
print("search permission is denied for file " + file, P_ERR);
return false;
case ELOOP:
print("too many symbolic links for file " + file, P_ERR);
// should this count?
return false;
case ENAMETOOLONG:
print("file path is too long for file " + file, P_ERR);
return false;
case ENOENT:
print("directory doesn't exist or path is blank for file " + file, P_ERR);
return false;
case ENOTDIR:
print("directory file mixup in the path for file " + file, P_ERR);
return false;
case EOVERFLOW:
print("data of file is too large to be represented for file " + file, P_ERR);
return true;
default:
//print("file " + file + " exists", P_DEBUG);
return true;
}
}
void file::write(std::string file, std::string data){
write_file(file, data);
}
std::string file::read(std::string file){
return read_file(file);
}
void file::wait_for_file(std::string file){
while(exists(file) == false){
sleep_ms(1);
}
}
bool file::is_dir(std::string file){
struct stat sb;
switch(stat(file.c_str(), &sb)){
case EACCES:
print("search permission is denied for file " + file, P_ERR);
return false;
case ELOOP:
print("too many symbolic links for file " + file, P_ERR);
// should this count?
return false;
case ENAMETOOLONG:
print("file path is too long for file " + file, P_ERR);
return false;
case ENOENT:
print("directory doesn't exist or path is blank for file " + file, P_ERR);
return false;
case ENOTDIR:
print("directory file mixup in the path for file " + file, P_ERR);
return false;
case EOVERFLOW:
print("data of file is too large to be represented for file " + file, P_ERR);
return true;
default:
return S_ISDIR(sb.st_mode);
}
}
bool file::is_file(std::string file){
struct stat sb;
switch(stat(file.c_str(), &sb)){
case EACCES:
print("search permission is denied for file " + file, P_ERR);
return false;
case ELOOP:
print("too many symbolic links for file " + file, P_ERR);
// should this count?
return false;
case ENAMETOOLONG:
print("file path is too long for file " + file, P_ERR);
return false;
case ENOENT:
print("directory doesn't exist or path is blank for file " + file, P_ERR);
return false;
case ENOTDIR:
print("directory file mixup in the path for file " + file, P_ERR);
return false;
case EOVERFLOW:
print("data of file is too large to be represented for file " + file, P_ERR);
return true;
default:
return S_ISREG(sb.st_mode);
}
}
std::string file::ensure_slash_at_end(std::string str){
if(str[str.size()-1] != SLASH){
str += std::string(1, SLASH);
}
return str;
}
std::vector<uint8_t> file::read_file_vector(std::string file){
std::ifstream in(file, std::ios::in | std::ios::binary);
if(in.is_open() == false){
P_V_S(file, P_WARN);
print("can't open output file for reading", P_ERR);
}
in >> std::noskipws;
std::vector<uint8_t> retval;
std::copy(
std::istream_iterator<uint8_t>(in),
std::istream_iterator<uint8_t>(),
std::back_inserter(retval));
return retval;
}
void file::write_file_vector(std::string file, std::vector<uint8_t> raw_data){
std::ofstream out(file, std::ios::binary);
if(out.is_open() == false){
print("can't open file for writing", P_ERR);
}
out.write(
(char*)&(raw_data[0]),
raw_data.size());
out.close();
}