fix(util/lean_path): bug at init_lean_path (it was missing last path), and add normalization function
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
50de85ee29
commit
e5f53595b6
1 changed files with 17 additions and 1 deletions
|
@ -17,6 +17,7 @@ namespace lean {
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
static char g_path_sep = ';';
|
static char g_path_sep = ';';
|
||||||
static char g_sep = '\\';
|
static char g_sep = '\\';
|
||||||
|
static char g_bad_sep = '/';
|
||||||
static std::string get_exe_location() {
|
static std::string get_exe_location() {
|
||||||
HMODULE hModule = GetModuleHandleW(NULL);
|
HMODULE hModule = GetModuleHandleW(NULL);
|
||||||
WCHAR path[MAX_PATH];
|
WCHAR path[MAX_PATH];
|
||||||
|
@ -39,6 +40,7 @@ static std::string get_exe_location() {
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
static char g_path_sep = ':';
|
static char g_path_sep = ':';
|
||||||
static char g_sep = '/';
|
static char g_sep = '/';
|
||||||
|
static char g_bad_sep = '\\';
|
||||||
static std::string get_exe_location() {
|
static std::string get_exe_location() {
|
||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
uint32_t bufsize = PATH_MAX;
|
uint32_t bufsize = PATH_MAX;
|
||||||
|
@ -55,6 +57,7 @@ static std::string get_exe_location() {
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
static char g_path_sep = ':';
|
static char g_path_sep = ':';
|
||||||
static char g_sep = '/';
|
static char g_sep = '/';
|
||||||
|
static char g_bad_sep = '\\';
|
||||||
static std::string get_exe_location() {
|
static std::string get_exe_location() {
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
char dest[PATH_MAX];
|
char dest[PATH_MAX];
|
||||||
|
@ -67,6 +70,15 @@ static std::string get_exe_location() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::string normalize_path(std::string f) {
|
||||||
|
for (auto & c : f) {
|
||||||
|
if (c == g_bad_sep)
|
||||||
|
c = g_sep;
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
static std::string g_lean_path;
|
static std::string g_lean_path;
|
||||||
static std::vector<std::string> g_lean_path_vector;
|
static std::vector<std::string> g_lean_path_vector;
|
||||||
struct init_lean_path {
|
struct init_lean_path {
|
||||||
|
@ -79,6 +91,7 @@ struct init_lean_path {
|
||||||
} else {
|
} else {
|
||||||
g_lean_path = r;
|
g_lean_path = r;
|
||||||
}
|
}
|
||||||
|
g_lean_path = normalize_path(g_lean_path);
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
unsigned j = 0;
|
unsigned j = 0;
|
||||||
unsigned sz = g_lean_path.size();
|
unsigned sz = g_lean_path.size();
|
||||||
|
@ -89,13 +102,16 @@ struct init_lean_path {
|
||||||
i = j + 1;
|
i = j + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (j > i)
|
||||||
|
g_lean_path_vector.push_back(g_lean_path.substr(i, j - i));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
static init_lean_path g_init_lean_path;
|
static init_lean_path g_init_lean_path;
|
||||||
|
|
||||||
std::string find_file(char const * fname) {
|
std::string find_file(char const * fname) {
|
||||||
|
std::string nfname = normalize_path(std::string(fname));
|
||||||
for (auto path : g_lean_path_vector) {
|
for (auto path : g_lean_path_vector) {
|
||||||
auto file = path + g_sep + fname;
|
std::string file = path + g_sep + nfname;
|
||||||
std::ifstream ifile(file);
|
std::ifstream ifile(file);
|
||||||
if (ifile)
|
if (ifile)
|
||||||
return file;
|
return file;
|
||||||
|
|
Loading…
Reference in a new issue