Skip to content

Commit 9140f01

Browse files
committed
Move file name resolver into own function
Added cross-platform `file_exists` function
1 parent ba0b3e0 commit 9140f01

File tree

2 files changed

+53
-41
lines changed

2 files changed

+53
-41
lines changed

file.cpp

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -200,50 +200,47 @@ namespace Sass {
200200
return result;
201201
}
202202

203+
// Resolution order for ambiguous imports:
204+
// (1) filename as given
205+
// (2) underscore + given
206+
// (3) underscore + given + extension
207+
// (4) given + extension
208+
string resolve_file_name(const string& base, const string& name)
209+
{
210+
// supported extensions
211+
const vector<string> exts = {
212+
".scss", ".sass", ".css"
213+
};
214+
// create full path (maybe relative)
215+
string path(join_paths(base, name));
216+
if (file_exists(path)) return path;
217+
// next test variation with underscore
218+
path = join_paths(base, "_" + name);
219+
if (file_exists(path)) return path;
220+
// next test exts plus underscore
221+
for(auto ext : exts) {
222+
path = join_paths(base, "_" + name + ext);
223+
if (file_exists(path)) return path;
224+
}
225+
// next test plain name with exts
226+
for(auto ext : exts) {
227+
path = join_paths(base, name + ext);
228+
if (file_exists(path)) return path;
229+
}
230+
// nothing found
231+
return string("");
232+
}
233+
203234
char* resolve_and_load(string path, string& real_path)
204235
{
205-
// Resolution order for ambiguous imports:
206-
// (1) filename as given
207-
// (2) underscore + given
208-
// (3) underscore + given + extension
209-
// (4) given + extension
210-
char* contents = 0;
211236
real_path = path;
212-
vector<string> exts(3);
213-
exts[0] = ".scss";
214-
exts[1] = ".sass";
215-
exts[2] = ".css";
216-
217-
// if the file isn't found with the given filename (1)
218-
if (!(contents = read_file(real_path))) {
219-
string dir(dir_name(path));
220-
string base(base_name(path));
221-
real_path = dir + base;
222-
// (2) underscore + given
223-
string test_path(dir + "_" + base);
224-
if ((contents = read_file(test_path))) {
225-
real_path = test_path;
226-
}
227-
// (3) underscore + given + extension
228-
if (!contents) {
229-
for(auto ext : exts) {
230-
test_path = dir + "_" + base + ext;
231-
if ((contents = read_file(test_path))) {
232-
real_path = test_path;
233-
break;
234-
}
235-
}
236-
}
237-
// (4) given + extension
238-
if (!contents) {
239-
for(auto ext : exts) {
240-
test_path = dir + base + ext;
241-
if ((contents = read_file(test_path))) {
242-
real_path = test_path;
243-
break;
244-
}
245-
}
246-
}
237+
char* contents = 0;
238+
string base(dir_name(path));
239+
string name(base_name(path));
240+
string resolved = resolve_file_name(base, name);
241+
if (resolved != "") {
242+
real_path = resolved;
243+
contents = read_file(resolved);
247244
}
248245
#ifdef _WIN32
249246
// convert Windows backslashes to URL forward slashes
@@ -252,6 +249,19 @@ namespace Sass {
252249
return contents;
253250
}
254251

252+
bool file_exists(const string& path)
253+
{
254+
#ifdef _WIN32
255+
wstring wpath = UTF_8::convert_to_utf16(path);
256+
DWORD dwAttrib = GetFileAttributesW(wpath.c_str());
257+
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
258+
(!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)));
259+
#else
260+
struct stat buffer;
261+
return (stat (path.c_str(), &buffer) == 0);
262+
#endif
263+
}
264+
255265
char* read_file(string path)
256266
{
257267
#ifdef _WIN32

file.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ namespace Sass {
1111
string base_name(string);
1212
string dir_name(string);
1313
string join_paths(string, string);
14+
bool file_exists(const string& path);
1415
bool is_absolute_path(const string& path);
1516
string make_canonical_path (string path);
1617
string make_absolute_path(const string& path, const string& cwd);
1718
string resolve_relative_path(const string& uri, const string& base, const string& cwd);
19+
string resolve_file_name(const string& base, const string& name);
1820
char* resolve_and_load(string path, string& real_path);
1921
char* read_file(string path);
2022
}

0 commit comments

Comments
 (0)