-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileutil.cpp
207 lines (190 loc) · 4.65 KB
/
fileutil.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright 2011 Pyarelal Knowles, under GNU LGPL (see LICENCE.txt) */
#include "prec.h"
#include <string>
#include <fstream>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#endif
std::string getHomeDir()
{
static std::string home;
#ifndef _WIN32
if (!home.size())
home = getenv("HOME");
if (!home.size())
home = getenv("USERPROFILE");
if (!home.size())
{
char* drive = getenv("HOMEDRIVE");
char* path = getenv("HOMEPATH");
home = std::string(drive) + path;
}
#endif
return home;
}
std::string expanduser(std::string filename)
{
if (filename[0] == '~' && (filename.size() == 0 || filename[1] == '/'))
{
filename.replace(0, 1, getHomeDir());
}
return filename;
}
std::string basefilename(std::string filename)
{
size_t s = filename.find_last_of("/");
size_t p = filename.find_last_of(".");
s = (int)s < 0 ? 0 : s + 1;
if ((int)p == -1 || p < s)
p = filename.size();
return filename.substr(s, p-s);
}
std::string basefilepath(std::string filename)
{
size_t p = filename.find_last_of("/");
if ((int)p >= 0)
return filename.substr(0, p + 1);
return "";
}
std::string fileExtension(std::string filename)
{
int i = (int)filename.find_last_of(".");
if (i > 0)
return filename.substr(i+1);
return "";
}
std::string joinPath(const std::string& a, const std::string& b)
{
if (!a.size())
return b;
if (a[a.size()-1] == '/')
return a + b;
return a + "/" + b;
}
std::vector<std::string> listDirectory(std::string path)
{
std::vector<std::string> ret;
#ifdef _WIN32
printf("Error: no listDirectory() implemented for windows in pyarlib\n");
#else
DIR *dp;
struct dirent *ep;
dp = opendir(path.c_str());
if (dp != NULL)
{
while ((ep = readdir(dp)))
ret.push_back(ep->d_name);
closedir(dp);
}
#endif
return ret;
}
bool fileExists(const char* filename)
{
#ifdef _WIN32
DWORD fileAttr;
fileAttr = GetFileAttributesA(filename);
if (0xFFFFFFFF == fileAttr)
return false;
return true;
#else
struct stat info;
return stat(filename, &info) == 0;
#endif
}
bool pathExists(const char* path)
{
//FIXME: untested on windows
return fileExists(path);
}
int fileTime(const char* filename)
{
#ifdef _WIN32
//is it just me or is it normal to feel like throwin up when
//ever I have to deal with the windows API
FILETIME modtime;
HANDLE fh = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (fh == INVALID_HANDLE_VALUE)
return 0;
if (GetFileTime(fh, NULL, NULL, &modtime) == 0)
return 0;
CloseHandle(fh);
int tmp = (((__int64)modtime.dwHighDateTime) << 32)/10000000;
return tmp + modtime.dwLowDateTime/10000000; //NOTE: untested :D
#else
struct stat info;
if (stat(filename, &info) == 0)
return info.st_mtime;
return 0;
#endif
}
bool readFile(std::string& str, const char* filename)
{
if (!filename) return false;
std::ifstream ifile(filename, std::ios::in | std::ios::binary | std::ios::ate);
if (!ifile.good()) return false;
str.reserve((unsigned int)ifile.tellg()+1); //reserve file size
ifile.seekg(0); //go to start
str.assign((std::istreambuf_iterator<char>(ifile)), std::istreambuf_iterator<char>());
//ifile.read((char*)&str[0], str.size());
ifile.close();
return true;
}
bool readUncomment(std::istream& stream, std::string& line)
{
//FIXME: does not handle "strings with //comments" or 'literals/*such as this'
static bool commentBlock = false;
if (!getline(stream, line))
return false;
//if currently within a block/c-style comment
if (commentBlock)
{
int bcomment = (int)line.find("*/");
if (bcomment < 0)
{
line = "\n";
return true;
}
else
line = line.substr(bcomment+2);
commentBlock = false;
}
//remove single line block/c-style comments
while (true)
{
int first = (int)line.find("/*");
if (first < 0) break;
int second = (int)line.find("*/", first);
if (second < 0) break;
line = line.substr(0, first) + line.substr(second+2);
}
//remove "//" comments
int lcomment = (int)line.find("//");
if (lcomment >= 0)
{
line = line.substr(0, lcomment);
}
//check for multiline block comments
int bcomment = (int)line.find("/*");
if (bcomment >= 0)
{
line = line.substr(0, bcomment);
commentBlock = true;
}
line += "\n";
bool done = !stream.good();
if (done) commentBlock = false;
return true;
}
std::string stripComments(const std::string& text)
{
std::stringstream str(text), out;
std::string line;
while (readUncomment(str, line))
out << line;
return out.str();
}