-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolvr.cpp
270 lines (244 loc) · 9.02 KB
/
resolvr.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <iostream>
#include <string>
#include <list>
#include <sstream>
#include <stdlib.h>
#define VERSION "1.0.0"
std::string path;
std::string base;
std::string prog;
// Windows compilers will normally set this, otherwise Windows compilation can
// be forced by explicitly setting -DOS_WIN
#ifdef _WIN32
#define OS_WIN
#endif
#define RESET "\033[0m"
#define BOLD "\033[1m"
#ifdef OS_WIN
// Use implementation of realpath based on GetFullPathNameA by Nach M. S.
#include "realpath.h"
#define SEPARATOR '\\'
#define VOL_SEP ':'
#else
#define SEPARATOR '/'
#define VOL_SEP '\0'
#endif
bool resolveSymlinks = false;
bool isAbsolute = false;
enum HelpLevel {
HelpLevelShort,
HelpLevelLong
};
bool isRelative(const std::string &path)
{
return (path.at(0) != SEPARATOR);
}
std::string volumePart(const std::string &path)
{
std::string result;
if (VOL_SEP && path.at(1) == VOL_SEP)
{
result = path.substr(0, 2);
}
return result;
}
std::string pathPart(const std::string &path)
{
std::string result = path;
if (VOL_SEP && (path.at(1) == VOL_SEP))
{
result = path.substr(2);
}
return result;
}
void printHelp(HelpLevel helpLevel = HelpLevelShort)
{
if (helpLevel == HelpLevelShort)
{
std::cerr << "Usage: " << prog << " [-h] | [-s] path [base_path]" << std::endl;
}
else
{
std::cerr << "\n"
BOLD "SYNOPSIS\n" RESET
"\n"
" " << prog << " [-h] | [-s] path [base_path]\n"
"\n"
BOLD "DESCRIPTION\n" RESET
"\n"
" Returns " BOLD "path" RESET " resolved to canonical form, that is with \"..\", \".\" and doubled-\n"
" seperators \"" << SEPARATOR << SEPARATOR << "\" replaced where possible so as to form the simplest result\n"
" that still specifies the same path.\n"
"\n"
" In the two-argument form, returns the " BOLD "path" RESET " in the first argument resolved\n"
" as relative to the " BOLD "base_path" RESET " in the second argument.\n"
"\n"
" If the path/s could be resolved resolvr returns EXIT_SUCCESS and outputs the\n"
" resolved path, otherwise it returns EXIT_FAILURE.\n"
"\n"
BOLD "OPTIONS\n" RESET
"\n"
BOLD " -s\n" RESET
" In the default case this program only does string manipulation, and does not\n"
" reference the file system at all. With -s symlinks are resolved and paths\n"
" are checked on the file system; and any " BOLD "base_path" RESET " argument is ignored. See\n"
" the section PATH RESOLUTION below.\n"
"\n"
BOLD " -h\n" RESET
" Print this help and exit with EXIT_SUCCESS.\n"
"\n"
BOLD " base_path\n" RESET
" In the two argument form, where " BOLD "base_path" RESET " is specified, the first argument\n"
" should be a relative path and the second argument should be absolute. In\n"
" the case that the first argument is absolute then the second argument is\n"
" simply ignored. This is not an error as scripts will want to do (eg):\n"
"\n"
" RESOURCE_PATH=`resolvr $(dirname $0)/../resources $PWD`\n"
"\n"
" and in the case that the script is invoked with an absolute path, then silently\n"
" ignoring the second argument is the right thing to do. If the " BOLD "base_path" RESET " is\n"
" relative then the program will exit with EXIT_FAILURE.\n"
"\n"
BOLD "PLATFORMS\n" RESET
"\n"
" On Windows uses \"\\\" as the path separator character, on all other platforms\n"
" the \"/\" character. As a result this simple tool does not support VMS and many\n"
" other platforms that have other path separators.\n"
"\n"
BOLD "PATH RESOLUTION\n" RESET
"\n"
" In the -s case path resolution is as per the realpath C library function as here:\n"
"\n"
" http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html\n"
"\n"
" so in the -s case this tool is really just a simple wrapper around realpath (on\n"
" those systems that provide it).\n"
"\n"
" If -s is specified then this program will resolve symlinks to their targets, and\n"
" will fail with EXIT_FAILURE if the target path does not exist. In the case of\n"
" -s the " BOLD "second argument is ignored" RESET " and the current directory is assumed\n"
" as the base for any relative path.\n"
"\n"
" Some systems (not Mac OSX or Windows) have command line versions of realpath or\n"
" an equivalent in readlink -f which will return a canonical path.\n"
"\n"
" If shell versions of realpath are available this program in the -s case is equi-\n"
" valent to the bash shell code:\n"
"\n"
" realpath \"$2/$1\"\n"
"\n"
BOLD "CREDIT\n" RESET
"\n"
" Written by Sarah Smith 2013.\n"
"\n"
" Windows implementation of \"realpath\" is by Nach M. S. as described here:\n"
"\n"
" http://sourceforge.net/p/mingw/patches/256/"
<< std::endl;
}
}
int main(int argc, char *argv[])
{
std::list<std::string> args;
for (int i = 0; i < argc; ++i)
{
args.push_back( std::string( argv[i] ));
}
prog = args.front();
args.pop_front();
while (args.size() > 0)
{
std::string opt = args.front();
args.pop_front();
if (opt == "-h")
{
printHelp(HelpLevelLong);
exit( EXIT_SUCCESS );
}
else if (opt == "-s")
{
resolveSymlinks = true;
}
else if (path.size() == 0)
{
path = opt;
}
else if (base.size() == 0)
{
base = opt;
}
else
{
std::cerr << "Ignoring extra arguments. (Do you need to quote or escape spaces?)" << std::endl;
printHelp();
}
}
if (path.size() == 0)
{
printHelp();
exit( EXIT_FAILURE );
}
std::string volPart = volumePart(path);
if (volPart.size() > 0)
path = pathPart(path);
if (base.size() > 0)
{
// Two-argument form with base_path
if (isRelative(path))
{
path = base + SEPARATOR + path;
}
else
{
printHelp();
exit( EXIT_FAILURE );
}
}
isAbsolute = !isRelative(path);
if (resolveSymlinks)
{
// Note in this case we ignore the relative_path "base" argument
char *resolved = realpath( path.c_str(), NULL );
path = resolved;
free( resolved );
}
else
{
std::stringstream ss(path);
std::string item;
std::list<std::string> pathParts;
while (std::getline(ss, item, SEPARATOR))
{
// drop empty parts (from eg "//") and 'dots' from eg "/./"
if (item.size() == 0 || item == ".") continue;
// go up a dir on "/../" parts if not at beginning of a path
if (item == ".." && pathParts.size() > 0)
{
pathParts.pop_back();
continue;
}
// in all other cases accumulate to the resulting path
pathParts.push_back(item);
}
path.clear();
if (pathParts.size() == 0)
{
path = isAbsolute ? SEPARATOR : '.';
}
else
{
if (isAbsolute)
path = SEPARATOR;
path = path + pathParts.front();
pathParts.pop_front();
std::list<std::string>::const_iterator it = pathParts.begin();
for ( ; it != pathParts.end(); ++it)
{
path = path + SEPARATOR + *it;
}
}
}
if (volPart.size() > 0)
path = volPart + path;
std::cout << path << std::endl;
}