26static void PR_compress_path( std::string& dir ) {
28 static const char pattern[] =
"\\..\\";
30 static const char pattern[] =
"/../";
34 static const char file_separator =
'\\';
35 static const char double_file_separator[] =
"\\\\";
37 static const char file_separator =
'/';
38 static const char double_file_separator[] =
"//";
41 if ( dir.size() == 0 )
return;
49 std::string::size_type pos;
50 pos = dir.find( double_file_separator );
51 if ( pos == std::string::npos )
break;
57 std::string::size_type pos1;
58 std::string::size_type pos2;
60 pos1 = dir.find( pattern );
61 if ( pos1 == std::string::npos )
break;
66 std::string p = dir.substr( 0, pos1 );
71 pos2 = p.find_last_of( file_separator );
73 if ( pos2 == std::string::npos )
break;
82 dir.erase( pos2, pos1 + 4 - pos2 - 1 );
88static void PR_dirname(
const std::string& file_name, std::string& result ) {
89 std::string::size_type pos = file_name.find_last_of(
'/' );
90 if ( pos == std::string::npos ) { pos = file_name.find_last_of(
'\\' ); }
92 if ( pos == std::string::npos ) { result =
""; }
100static bool PR_absolute_path(
const std::string& name ) {
101 if ( name.size() == 0 )
return (
false );
103 if ( ( name[0] ==
'/' ) || ( name[0] ==
'\\' ) )
return (
true );
105 if ( name.size() >= 2 )
107 if ( name[1] ==
':' ) {
return (
true ); }
112static void PR_basename(
const std::string& file_name, std::string& result ) {
113 std::string::size_type pos = file_name.find_last_of(
'/' );
115 if ( pos == std::string::npos ) { pos = file_name.find_last_of(
'\\' ); }
117 if ( pos == std::string::npos ) { result = file_name; }
118 else { result = file_name.substr( pos + 1 ); }
121static bool PR_test_exist(
const std::string& name, std::string& real_name,
123 struct stat file_stat;
128 strcpy( buf, name.c_str() );
131 static const char file_separator =
'\\';
133 static const char file_separator =
'/';
142 status = lstat( buf, &file_stat );
146 if ( S_ISLNK( file_stat.st_mode ) != 0 )
151 int n = readlink( buf, buf,
sizeof( buf ) );
152 if (
n >= 0 ) buf[
n] = 0;
156 if ( PR_absolute_path( buf ) ) { real_name = buf; }
159 PR_dirname( real_name, real_name );
160 real_name += file_separator;
163 PR_compress_path( real_name );
165 strcpy( buf, real_name.c_str() );
177 status = stat( name.c_str(), &file_stat );
181 if ( ( file_stat.st_mode & S_IFDIR ) == 0 ) {
return ( file_type ==
PR_regular_file ); }
184 else {
return (
false ); }
187static void PR_scan_dir(
const std::string& dir_name, std::vector<std::string>& list ) {
189 static const char file_separator =
'\\';
191 static const char file_separator =
'/';
194 static std::string dir_prefix;
195 static std::string name_prefix;
196 static std::string real_name;
198 dir_prefix = dir_name;
199 if ( dir_name ==
"" ) dir_prefix =
".";
204 if ( !PR_test_exist( dir_prefix, real_name,
PR_directory ) )
206 PR_dirname( dir_prefix, dir_prefix );
207 PR_basename( dir_name, name_prefix );
210 bool need_filter =
false;
212 std::string::size_type wild_card;
215 wild_card = name_prefix.find(
'*' );
216 if ( wild_card != std::string::npos )
218 name_prefix.erase( wild_card );
220 if ( name_prefix.size() > 0 ) { need_filter =
true; }
228 struct _finddata_t entry;
230 static std::string search;
233 search += file_separator;
236 dir = _findfirst( search.c_str(), &entry );
241 if ( ( strcmp( (
char*)entry.name,
"." ) != 0 ) &&
242 ( strcmp( (
char*)entry.name,
".." ) != 0 ) &&
243 ( strncmp( (
char*)entry.name,
".nfs", 4 ) != 0 ) )
245 const char* name = entry.name;
248 ( strncmp( name, name_prefix.c_str(), name_prefix.size() ) == 0 ) )
250 std::string& name_entry = list.add();
252 name_entry = dir_prefix;
253 name_entry += file_separator;
258 int status = _findnext( dir, &entry );
259 if ( status != 0 ) {
break; }
268 DIR* dir = opendir( dir_prefix.c_str() );
270 struct dirent* entry;
277 while ( ( entry = readdir( dir ) ) != 0 )
280 if ( !strcmp( (
char*)entry->d_name,
"." ) )
continue;
281 if ( !strcmp( (
char*)entry->d_name,
".." ) )
continue;
282 if ( !strncmp( (
char*)entry->d_name,
".nfs", 4 ) )
continue;
284 const char* name = entry->d_name;
288 if ( need_filter && ( strncmp( name, name_prefix.c_str(), name_prefix.size() ) != 0 ) )
291 std::string name_entry;
293 name_entry = dir_prefix;
294 name_entry += file_separator;
297 list.push_back( name_entry );
305static void PR_indent(
int level ) {
313static bool PR_find(
const std::string& search_base,
const std::string& logical_file_prefix,
314 const std::string& logical_file_name,
PR_file_type file_type,
316 static int level = 0;
319 static const char file_separator =
'\\';
321 static const char file_separator =
'/';
324 std::string file_path =
"";
325 std::string real_name =
"";
332 if ( search_base !=
"" )
334 file_path = search_base;
335 file_path += file_separator;
337 else { file_path =
""; }
339 file_path += logical_file_name;
344 if ( PR_test_exist( file_path, result, file_type ) ) { found =
true; }
346 if ( !found && ( logical_file_prefix !=
"" ) )
348 if ( search_base !=
"" )
350 file_path = search_base;
351 file_path += file_separator;
353 else { file_path =
""; }
355 file_path += logical_file_prefix;
356 file_path += file_separator;
357 file_path += logical_file_name;
362 if ( PR_test_exist( file_path, result, file_type ) ) { found =
true; }
370 std::string dir_name =
"";
371 std::string file_name =
"";
372 std::vector<std::string> list;
374 PR_scan_dir( search_base, list );
376 std::vector<std::string>::iterator it;
378 for ( it = list.begin(); it != list.end(); ++it )
380 const std::string& d = *it;
387 bool s = PR_find( d, logical_file_prefix, logical_file_name, file_type, search_type,
404static bool PR_find_from_list(
const std::string& logical_file_name,
408 static const char path_separator =
';';
410 static const char path_separator =
':';
413 std::string::size_type pos = 0;
415 std::string file_name =
"";
416 std::string file_prefix =
"";
418 PR_basename( logical_file_name, file_name );
419 PR_dirname( logical_file_name, file_prefix );
421 std::string real_name =
"";
425 if ( PR_find(
"", file_prefix, file_name, file_type, search_type, result ) )
430 for (
int i = 0;; i++ )
434 std::string::size_type next = search_list.find( path_separator, pos );
436 std::string path = search_list.substr( pos, next - pos );
438 if ( next == std::string::npos )
440 path = search_list.substr( pos );
445 path = search_list.substr( pos, next - pos );
451 if ( PR_find( path, file_prefix, file_name, file_type, search_type, result ) )
465 const std::string& search_path,
SearchType search_type ) {
466 const char* path_env = ::getenv( search_path.c_str() );
468 std::string path_list;
470 if ( path_env != 0 ) { path_list = path_env; }
476 const std::string& search_list,
480 if ( !PR_find_from_list( logical_file_name, search_list,
PR_regular_file, search_type,
488 const std::string& search_path,
490 const char* path_env = ::getenv( search_path.c_str() );
492 std::string path_list;
494 if ( path_env != 0 ) { path_list = path_env; }
500 const std::string& search_list,
504 if ( !PR_find_from_list( logical_file_name, search_list,
PR_directory, search_type,
513 const char* path_env = ::getenv( search_path.c_str() );
518 static const char path_separator =
';';
520 static const char path_separator =
':';
523 std::string path_list( path_env );
525 std::string::size_type pos = 0;
527 for (
int i = 0;; i++ )
531 std::string::size_type next = path_list.find( path_separator, pos );
533 std::string path = path_list.substr( pos, next - pos );
535 if ( next == std::string::npos )
537 path = path_list.substr( pos );
542 path = path_list.substr( pos, next - pos );
546 std::string real_name =
"";
562 const std::string& search_path ) {
567 const std::string& search_list ) {
572 const std::string& search_path ) {
577 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)