-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathfilelibunix.cpp
More file actions
181 lines (163 loc) · 4.62 KB
/
Copy pathfilelibunix.cpp
File metadata and controls
181 lines (163 loc) · 4.62 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
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
/*
* File: filelibunix.cpp
* ---------------------
* This file contains Unix implementations of filelib.h primitives.
* This code used to live in platform.cpp before the Java back-end was retired.
*
* @version 2018/10/23
* - added getAbsolutePath
*/
#include "filelib.h"
// define all of the following only on non-Windows OS
// (see filelibwindows.cpp for Windows versions)
#ifndef _WIN32
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <stdint.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <ios>
#include <string>
#include "error.h"
#include "strlib.h"
namespace platform {
bool filelib_isDirectory(const std::string& filename) {
struct stat fileInfo;
if (stat(filename.c_str(), &fileInfo) != 0) {
return false;
}
return S_ISDIR(fileInfo.st_mode) != 0;
}
void filelib_createDirectory(const std::string& path) {
std::string pathStr = path;
if (endsWith(path, "/")) {
pathStr = path.substr(0, path.length() - 1);
}
if (mkdir(pathStr.c_str(), 0777) != 0) {
if (errno == EEXIST && filelib_isDirectory(pathStr)) {
return;
}
std::string msg = "createDirectory: ";
std::string err = std::string(strerror(errno));
error(msg + err);
}
}
void filelib_deleteFile(const std::string& path) {
remove(path.c_str());
}
std::string filelib_expandPathname(const std::string& filename) {
if (filename == "") {
return "";
}
int len = filename.length();
std::string expanded = filename;
if (expanded[0] == '~') {
int spos = 1;
while (spos < len && expanded[spos] != '\\' && expanded[spos] != '/') {
spos++;
}
char *homedir = nullptr;
if (spos == 1) {
homedir = getenv("HOME");
if (!homedir) {
homedir = getpwuid(getuid())->pw_dir;
}
} else {
struct passwd *pw = getpwnam(expanded.substr(1, spos - 1).c_str());
if (!pw) {
error("expandPathname: No such user");
} else {
homedir = pw->pw_dir;
}
}
expanded = std::string(homedir) + expanded.substr(spos);
len = expanded.length();
}
for (int i = 0; i < len; i++) {
if (expanded[i] == '\\') {
expanded[i] = '/';
}
}
return expanded;
}
bool filelib_fileExists(const std::string& filename) {
struct stat fileInfo;
return stat(filename.c_str(), &fileInfo) == 0;
}
std::string filelib_getAbsolutePath(const std::string& path) {
char realpathOut[4096];
realpath(path.c_str(), realpathOut);
std::string absPath(realpathOut);
return absPath;
}
std::string filelib_getCurrentDirectory() {
char currentDirBuf[4096] = {'\0'};
char* cwd = getcwd(currentDirBuf, 4096 - 1);
std::string result;
if (cwd) {
result = std::string(cwd);
} else {
error("getCurrentDirectory: " + std::string(strerror(errno)));
}
return result;
}
std::string filelib_getDirectoryPathSeparator() {
return "/";
}
std::string filelib_getSearchPathSeparator() {
return ":";
}
// http://stackoverflow.com/questions/8087805/
// how-to-get-system-or-user-temp-folder-in-unix-and-windows
std::string filelib_getTempDirectory() {
char* dir = getenv("TMPDIR");
if (!dir) dir = getenv("TMP");
if (!dir) dir = getenv("TEMP");
if (!dir) dir = getenv("TEMPDIR");
if (!dir) return "/tmp";
return dir;
}
bool filelib_isFile(const std::string& filename) {
struct stat fileInfo;
if (stat(filename.c_str(), &fileInfo) != 0) {
return false;
}
return S_ISREG(fileInfo.st_mode) != 0;
}
void filelib_listDirectory(const std::string& path, Vector<std::string>& list) {
DIR* dir = opendir(path.empty() ? "." : path.c_str());
if (!dir) {
error(std::string("listDirectory: Can't open \"") + path + "\"");
}
list.clear();
while (true) {
struct dirent* ep = readdir(dir);
if (!ep) {
break;
}
std::string name = std::string(ep->d_name);
if (name != "." && name != "..") {
list.add(name);
}
}
closedir(dir);
sort(list.begin(), list.end());
}
void filelib_setCurrentDirectory(const std::string& path) {
if (chdir(path.c_str()) != 0) {
std::string msg = "setCurrentDirectory: ";
std::string err = std::string(strerror(errno));
error(msg + err);
}
}
} // namespace platform
#endif // _WIN32