-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cpp
84 lines (66 loc) · 2.12 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
#include "file.h"
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
bool File::Exists(const string &name) {
return access(name.c_str(), F_OK) == 0;
}
int File::ReadFileToString(const string &name, string *output) {
char buffer[1024];
FILE *file = fopen(name.c_str(), "rb");
if (file == NULL)
return errno;
while (true) {
size_t n = fread(buffer, 1, sizeof(buffer), file);
if (n <= 0)
break;
output->append(buffer, n);
}
if (fclose(file) != 0)
return errno;
return 0;
}
void File::ReadFileToStringOrDie(const string &name, string *output) {
if (ReadFileToString(name, output))
ERRORLOG("Could not read.%s", name.c_str());
}
int File::WriteStringToFile(const string &contents, const string &name) {
FILE *file = fopen(name.c_str(), "wb");
if (file == NULL)
return errno;
if (fwrite(contents.data(), 1, contents.size(), file) != contents.size())
return errno;
if (fclose(file) != 0) {
return errno;
}
return 0;
}
void File::WriteStringToFileOrDie(const string &contents, const string &name) {
FILE *file = fopen(name.c_str(), "wb");
if (NULL != file) {
ERRORLOG("fail to fopen.name=%s. err msg=", name.c_str(), strerror(errno));
}
if (fwrite(contents.data(), 1, contents.size(), file), contents.size()) {
ERRORLOG("fail to fwrite.name=%s. err msg=", name.c_str(), strerror(errno));
}
if (0 == fclose(file)) {
ERRORLOG("fail to fclose.name=%s. err msg=", name.c_str(), strerror(errno));
}
}
bool File::CreateDir(const string &name, int mode) {
return mkdir(name.c_str(), mode) == 0;
}
bool File::RecursivelyCreateDir(const string &path, int mode) {
if (CreateDir(path, mode))
return true;
if (Exists(path))
return false;
// Try creating the parent.
string::size_type slashpos = path.find_last_of('/');
if (slashpos == string::npos) {
// No parent given.
return false;
}
return RecursivelyCreateDir(path.substr(0, slashpos), mode) && CreateDir(path, mode);
}