3#include "HltTools/PathResolver.h"
34static void PR_compress_path (std::string& dir)
37 static const char pattern[] =
"\\..\\";
39 static const char pattern[] =
"/../";
43 static const char file_separator =
'\\';
44 static const char double_file_separator[] =
"\\\\";
46 static const char file_separator =
'/';
47 static const char double_file_separator[] =
"//";
50 if (dir.size () == 0)
return;
58 std::string::size_type pos;
59 pos = dir.find (double_file_separator);
60 if (pos == std::string::npos)
break;
66 std::string::size_type pos1;
67 std::string::size_type pos2;
69 pos1 = dir.find (pattern);
70 if (pos1 == std::string::npos)
break;
75 std::string p = dir.substr (0, pos1);
80 pos2 = p.find_last_of (file_separator);
82 if (pos2 == std::string::npos)
break;
91 dir.erase (pos2, pos1 + 4 - pos2 - 1);
97static void PR_dirname (
const std::string& file_name, std::string& result)
99 std::string::size_type pos = file_name.find_last_of (
'/');
100 if (pos == std::string::npos)
102 pos = file_name.find_last_of (
'\\');
105 if (pos == std::string::npos)
116static bool PR_absolute_path (
const std::string& name)
118 if (name.size () == 0)
return (
false);
120 if ((name[0] ==
'/') ||
121 (name[0] ==
'\\'))
return (
true);
123 if (name.size () >= 2)
133static void PR_basename (
const std::string& file_name, std::string& result)
135 std::string::size_type pos = file_name.find_last_of (
'/');
137 if (pos == std::string::npos)
139 pos = file_name.find_last_of (
'\\');
142 if (pos == std::string::npos)
148 result = file_name.substr (pos + 1);
152static bool PR_test_exist (
const std::string& name, std::string& real_name,
PR_file_type file_type)
154 struct stat file_stat;
159 strcpy (buf, name.c_str ());
162 static const char file_separator =
'\\';
164 static const char file_separator =
'/';
173 status = lstat (buf, &file_stat);
177 if (S_ISLNK (file_stat.st_mode) != 0)
181 int n = readlink (buf, buf,
sizeof (buf));
182 if (
n >= 0) buf[
n] = 0;
186 if (PR_absolute_path (buf))
192 PR_dirname (real_name, real_name);
193 real_name += file_separator;
196 PR_compress_path (real_name);
198 strcpy (buf, real_name.c_str ());
216 status = stat (name.c_str (), &file_stat);
220 if ((file_stat.st_mode & S_IFDIR) == 0)
235static void PR_scan_dir (
const std::string& dir_name,
236 std::vector<std::string>& list)
239 static const char file_separator =
'\\';
241 static const char file_separator =
'/';
244 static std::string dir_prefix;
245 static std::string name_prefix;
246 static std::string real_name;
248 dir_prefix = dir_name;
249 if (dir_name ==
"") dir_prefix =
".";
253 if (!PR_test_exist (dir_prefix, real_name,
PR_directory))
255 PR_dirname (dir_prefix, dir_prefix);
256 PR_basename (dir_name, name_prefix);
259 bool need_filter =
false;
261 std::string::size_type wild_card;
264 wild_card = name_prefix.find (
'*');
265 if (wild_card != std::string::npos)
267 name_prefix.erase (wild_card);
269 if (name_prefix.size () > 0)
281 struct _finddata_t entry;
283 static std::string search;
286 search += file_separator;
289 dir = _findfirst (search.c_str (), &entry);
294 if ((strcmp ((
char*) entry.name,
".") != 0) &&
295 (strcmp ((
char*) entry.name,
"..") != 0) &&
296 (strncmp ((
char*) entry.name,
".nfs", 4) != 0))
298 const char* name = entry.name;
301 (strncmp (name, name_prefix.c_str (), name_prefix.size ()) == 0))
303 std::string& name_entry = list.add ();
305 name_entry = dir_prefix;
306 name_entry += file_separator;
311 int status = _findnext (dir, &entry);
324 DIR* dir = opendir (dir_prefix.c_str ());
326 struct dirent* entry;
333 while ((entry = readdir (dir)) != 0)
336 if (!strcmp ((
char*) entry->d_name,
"."))
continue;
337 if (!strcmp ((
char*) entry->d_name,
".."))
continue;
338 if (!strncmp ((
char*) entry->d_name,
".nfs", 4))
continue;
340 const char* name = entry->d_name;
345 (strncmp (name, name_prefix.c_str (), name_prefix.size ()) != 0))
continue;
347 std::string name_entry;
349 name_entry = dir_prefix;
350 name_entry += file_separator;
353 list.push_back (name_entry);
362static void PR_indent (
int level)
371static bool PR_find (
const std::string& search_base,
372 const std::string& logical_file_prefix,
373 const std::string& logical_file_name,
378 static int level = 0;
381 static const char file_separator =
'\\';
383 static const char file_separator =
'/';
386 std::string file_path =
"";
387 std::string real_name =
"";
393 if (search_base !=
"")
395 file_path = search_base;
396 file_path += file_separator;
403 file_path += logical_file_name;
408 if (PR_test_exist (file_path, result, file_type))
413 if (!found && (logical_file_prefix !=
""))
415 if (search_base !=
"")
417 file_path = search_base;
418 file_path += file_separator;
425 file_path += logical_file_prefix;
426 file_path += file_separator;
427 file_path += logical_file_name;
432 if (PR_test_exist (file_path, result, file_type))
442 std::string dir_name =
"";
443 std::string file_name =
"";
444 std::vector<std::string> list;
446 PR_scan_dir (search_base, list);
448 std::vector<std::string>::iterator it;
450 for (it = list.begin (); it != list.end (); ++it)
452 const std::string& d = *it;
459 bool s = PR_find (d, logical_file_prefix, logical_file_name, file_type, search_type, result);
475static bool PR_find_from_list (
const std::string& logical_file_name,
476 const std::string& search_list,
482 static const char path_separator =
';';
484 static const char path_separator =
':';
487 std::string::size_type pos = 0;
489 std::string file_name =
"";
490 std::string file_prefix =
"";
492 PR_basename (logical_file_name, file_name);
493 PR_dirname (logical_file_name, file_prefix);
495 std::string real_name =
"";
499 if (PR_find (
"", file_prefix, file_name, file_type, search_type, result))
510 std::string::size_type
next = search_list.find (path_separator, pos);
512 std::string path = search_list.substr (pos, next - pos);
514 if (next == std::string::npos)
516 path = search_list.substr (pos);
521 path = search_list.substr (pos, next - pos);
527 if (PR_find (path, file_prefix, file_name, file_type, search_type, result))
541 const std::string& search_path,
544 const char* path_env = ::getenv (search_path.c_str ());
546 std::string path_list;
550 path_list = path_env;
557 const std::string& search_list,
562 if (!PR_find_from_list (logical_file_name, search_list,
PR_regular_file, search_type, result))
571 const std::string& search_path,
574 const char* path_env = ::getenv (search_path.c_str ());
576 std::string path_list;
580 path_list = path_env;
587 const std::string& search_list,
592 if (!PR_find_from_list (logical_file_name, search_list,
PR_directory, search_type, result))
602 const char* path_env = ::getenv (search_path.c_str ());
607 static const char path_separator =
';';
609 static const char path_separator =
':';
612 std::string path_list (path_env);
614 std::string::size_type pos = 0;
620 std::string::size_type next = path_list.find (path_separator, pos);
622 std::string path = path_list.substr (pos, next - pos);
624 if (next == std::string::npos)
626 path = path_list.substr (pos);
631 path = path_list.substr (pos, next - pos);
635 std::string real_name =
"";
659 const std::string& search_path)
665 const std::string& search_list)
671 const std::string& search_path)
677 const std::string& search_list)
std::string PathResolverFindDirectory(const std::string &logical_file_name, const std::string &search_path)
std::string PathResolverFindFile(const std::string &logical_file_name, const std::string &search_path)
std::string PathResolverFindDirectoryFromList(const std::string &logical_file_name, const std::string &search_list)
std::string PathResolverFindXMLFile(const std::string &logical_file_name)
std::string PathResolverFindFileFromList(const std::string &logical_file_name, const std::string &search_list)
std::string PathResolverFindDataFile(const std::string &logical_file_name)
PathResolver::SearchPathStatus PathResolverCheckSearchPath(const std::string &search_path)
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
static std::string find_directory(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
static std::string find_directory_from_list(const std::string &logical_file_name, const std::string &search_list, SearchType search_type=LocalSearch)
@ EnvironmentVariableUndefined
static std::string find_file_from_list(const std::string &logical_file_name, const std::string &search_list, SearchType search_type=LocalSearch)
static SearchPathStatus check_search_path(const std::string &search_path)