-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
82 lines (56 loc) · 1.61 KB
/
FileSystem.cpp
File metadata and controls
82 lines (56 loc) · 1.61 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
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
#include "FileSystem.hpp"
using namespace std;
using namespace FileSystem;
string FileSystem::ReadFileToEnd(string file) {
std::ifstream t(file);
std::string str;
t.seekg(0, std::ios::end);
std::streampos pos = t.tellg();
str.reserve((size_t) pos); // This is probably not a great idea
t.seekg(0, std::ios::beg);
str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
return str;
}
string FileSystem::ReadFile(string file, size_t size) {
return string();
}
void* FileSystem::ReadFileRawToEnd(string file) {
off_t fileSize = GetFileSize(file);
if(fileSize == 0)
return NULL;
void* allocatedBuffer = malloc((size_t) fileSize);
if(allocatedBuffer == NULL)
return NULL;
FILE* fp = fopen(file.c_str(), "r");
if(fp) {
size_t readSize = fread(allocatedBuffer, 1, (size_t) fileSize, fp);
fclose(fp);
if(readSize == fileSize) {
return allocatedBuffer;
}
}
free(allocatedBuffer);
return NULL;
}
off_t FileSystem::GetFileSize(std::string filename) {
struct stat sts;
int rc = stat(filename.c_str(), &sts);
if(rc == 0 || rc == -1)
return 0;
return sts.st_size;
}
bool FileSystem::WriteFile(string file, string data) {
return false;
}
bool FileSystem::AppendFile(string file, string data) {
return false;
}
string FileSystem::ResolveSymLink(string path) {
char buf[PATH_MAX];
ssize_t len = ::readlink(path.c_str(), buf, sizeof(buf) - 1);
if(len != -1) {
buf[len] = 0;
return string(buf);
}
return string();
}