forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
349 lines (311 loc) · 10.5 KB
/
Copy pathfile.cpp
File metadata and controls
349 lines (311 loc) · 10.5 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#else
#include <unistd.h>
#endif
#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
#include <algorithm>
#include <sys/stat.h>
#include "file.hpp"
#include "context.hpp"
#include "utf8_string.hpp"
#include "sass2scss.h"
#ifdef _WIN32
#include <windows.h>
#endif
#ifndef FS_CASE_SENSITIVE
#ifdef _WIN32
#define FS_CASE_SENSITIVE 0
#else
#define FS_CASE_SENSITIVE 1
#endif
#endif
namespace Sass {
namespace File {
using namespace std;
// return the current directory
// always with forward slashes
string get_cwd()
{
const size_t wd_len = 1024;
char wd[wd_len];
string cwd = getcwd(wd, wd_len);
#ifdef _WIN32
//convert backslashes to forward slashes
replace(cwd.begin(), cwd.end(), '\\', '/');
#endif
if (cwd[cwd.length() - 1] != '/') cwd += '/';
return cwd;
}
// test if path exists and is a file
bool file_exists(const string& path)
{
#ifdef _WIN32
wstring wpath = UTF_8::convert_to_utf16(path);
DWORD dwAttrib = GetFileAttributesW(wpath.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
(!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)));
#else
struct stat st_buf;
return (stat (path.c_str(), &st_buf) == 0) &&
(!S_ISDIR (st_buf.st_mode));
#endif
}
// return if given path is absolute
// works with *nix and windows paths
bool is_absolute_path(const string& path)
{
#ifdef _WIN32
if (path.length() >= 2 && isalpha(path[0]) && path[1] == ':') return true;
#endif
return path[0] == '/';
}
// helper function to find the last directory seperator
inline size_t find_last_folder_separator(const string& path, size_t limit = string::npos)
{
size_t pos = string::npos;
size_t pos_p = path.find_last_of('/', limit);
#ifdef _WIN32
size_t pos_w = path.find_last_of('\\', limit);
#else
size_t pos_w = string::npos;
#endif
if (pos_p != string::npos && pos_w != string::npos) {
pos = max(pos_p, pos_w);
}
else if (pos_p != string::npos) {
pos = pos_p;
}
else {
pos = pos_w;
}
return pos;
}
// return only the directory part of path
string dir_name(const string& path)
{
size_t pos = find_last_folder_separator(path);
if (pos == string::npos) return "";
else return path.substr(0, pos+1);
}
// return only the filename part of path
string base_name(const string& path)
{
size_t pos = find_last_folder_separator(path);
if (pos == string::npos) return path;
else return path.substr(pos+1);
}
// do a locigal clean up of the path
// no physical check on the filesystem
string make_canonical_path (string path)
{
// declarations
size_t pos;
#ifdef _WIN32
//convert backslashes to forward slashes
replace(path.begin(), path.end(), '\\', '/');
#endif
pos = 0; // remove all self references inside the path string
while((pos = path.find("/./", pos)) != string::npos) path.erase(pos, 2);
pos = 0; // remove all leading and trailing self references
while(path.length() > 1 && path.substr(0, 2) == "./") path.erase(0, 2);
while((pos = path.length()) > 1 && path.substr(pos - 2) == "/.") path.erase(pos - 2);
pos = 0; // collapse multiple delimiters into a single one
while((pos = path.find("//", pos)) != string::npos) path.erase(pos, 1);
return path;
}
// join two path segments cleanly together
// but only if right side is not absolute yet
string join_paths(string l, string r)
{
#ifdef _WIN32
// convert Windows backslashes to URL forward slashes
replace(l.begin(), l.end(), '\\', '/');
replace(r.begin(), r.end(), '\\', '/');
#endif
if (l.empty()) return r;
if (r.empty()) return l;
if (is_absolute_path(r)) return r;
if (l[l.length()-1] != '/') l += '/';
while ((r.length() > 3) && ((r.substr(0, 3) == "../") || (r.substr(0, 3)) == "..\\")) {
r = r.substr(3);
size_t pos = find_last_folder_separator(l, l.length() - 2);
l = l.substr(0, pos == string::npos ? pos : pos + 1);
}
return l + r;
}
// create an absolute path by resolving relative paths with cwd
string make_absolute_path(const string& path, const string& cwd)
{
return make_canonical_path((is_absolute_path(path) ? path : join_paths(cwd, path)));
}
// create a path that is relative to the given base directory
// path and base will first be resolved against cwd to make them absolute
string resolve_relative_path(const string& uri, const string& base, const string& cwd)
{
string absolute_uri = make_absolute_path(uri, cwd);
string absolute_base = make_absolute_path(base, cwd);
#ifdef _WIN32
// absolute link must have a drive letter, and we know that we
// can only create relative links if both are on the same drive
if (absolute_base[0] != absolute_uri[0]) return absolute_uri;
#endif
string stripped_uri = "";
string stripped_base = "";
size_t index = 0;
size_t minSize = min(absolute_uri.size(), absolute_base.size());
for (size_t i = 0; i < minSize; ++i) {
#ifdef FS_CASE_SENSITIVE
if (absolute_uri[i] != absolute_base[i]) break;
#else
// compare the charactes in a case insensitive manner
// windows fs is only case insensitive in ascii ranges
if (tolower(absolute_uri[i]) != tolower(absolute_base[i])) break;
#endif
if (absolute_uri[i] == '/') index = i + 1;
}
for (size_t i = index; i < absolute_uri.size(); ++i) {
stripped_uri += absolute_uri[i];
}
for (size_t i = index; i < absolute_base.size(); ++i) {
stripped_base += absolute_base[i];
}
size_t left = 0;
size_t directories = 0;
for (size_t right = 0; right < stripped_base.size(); ++right) {
if (stripped_base[right] == '/') {
if (stripped_base.substr(left, 2) != "..") {
++directories;
}
else if (directories > 1) {
--directories;
}
else {
directories = 0;
}
left = right + 1;
}
}
string result = "";
for (size_t i = 0; i < directories; ++i) {
result += "../";
}
result += stripped_uri;
return result;
}
// Resolution order for ambiguous imports:
// (1) filename as given
// (2) underscore + given
// (3) underscore + given + extension
// (4) given + extension
string resolve_file(const string& filename)
{
// supported extensions
const vector<string> exts = {
".scss", ".sass", ".css"
};
// split the filename
string base(dir_name(filename));
string name(base_name(filename));
// create full path (maybe relative)
string path(join_paths(base, name));
if (file_exists(path)) return path;
// next test variation with underscore
path = join_paths(base, "_" + name);
if (file_exists(path)) return path;
// next test exts plus underscore
for(auto ext : exts) {
path = join_paths(base, "_" + name + ext);
if (file_exists(path)) return path;
}
// next test plain name with exts
for(auto ext : exts) {
path = join_paths(base, name + ext);
if (file_exists(path)) return path;
}
// nothing found
return string("");
}
// helper function to resolve a filename
string find_file(const string& file, const vector<string> paths)
{
// search in every include path for a match
for (size_t i = 0, S = paths.size(); i < S; ++i)
{
string path(join_paths(paths[i], file));
string resolved(resolve_file(path));
if (resolved != "") return resolved;
}
// nothing found
return string("");
}
// inc paths can be directly passed from C code
string find_file(const string& file, const char* paths[])
{
if (paths == 0) return string("");
vector<string> includes(0);
// includes.push_back(".");
const char** it = paths;
while (it && *it) {
includes.push_back(*it);
++it;
}
return find_file(file, includes);
}
// try to load the given filename
// returned memory must be freed
// will auto convert .sass files
char* read_file(const string& path)
{
#ifdef _WIN32
BYTE* pBuffer;
DWORD dwBytes;
// windows unicode filepaths are encoded in utf16
wstring wpath = UTF_8::convert_to_utf16(path);
HANDLE hFile = CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) return 0;
DWORD dwFileLength = GetFileSize(hFile, NULL);
if (dwFileLength == INVALID_FILE_SIZE) return 0;
// allocate an extra byte for the null char
pBuffer = (BYTE*)malloc((dwFileLength+1)*sizeof(BYTE));
ReadFile(hFile, pBuffer, dwFileLength, &dwBytes, NULL);
pBuffer[dwFileLength] = '\0';
CloseHandle(hFile);
// just convert from unsigned char*
char* contents = (char*) pBuffer;
#else
struct stat st;
if (stat(path.c_str(), &st) == -1 || S_ISDIR(st.st_mode)) return 0;
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
char* contents = 0;
if (file.is_open()) {
size_t size = file.tellg();
// allocate an extra byte for the null char
contents = (char*) malloc((size+1)*sizeof(char));
file.seekg(0, ios::beg);
file.read(contents, size);
contents[size] = '\0';
file.close();
}
#endif
string extension;
if (path.length() > 5) {
extension = path.substr(path.length() - 5, 5);
}
for(size_t i=0; i<extension.size();++i)
extension[i] = tolower(extension[i]);
if (extension == ".sass" && contents != 0) {
char * converted = sass2scss(contents, SASS2SCSS_PRETTIFY_1 | SASS2SCSS_KEEP_COMMENT);
free(contents); // free the indented contents
return converted; // should be freed by caller
} else {
return contents;
}
}
}
}