Пример #1
0
//-----------------------------------------------------------------------------
bool find_file( std::string& path_to_file, const std::string& file_to_find, const std::string& search_path, bool recursive ) {
  if( !path_exists( search_path ) ) return false; // search_path doesn't exist
  if( !is_directory( search_path ) ) return false;// search_path not a directory
  
  // search_path exists and is a directory
  
  // check the current path for the file
  std::string path = combine_path( search_path, file_to_find );
  if( file_exists( path ) ) {
    path_to_file = path;
    return true;
  }

  // if not recursive, then failed to find file.
  if( !recursive ) return false;

  // otherwise enumerate all the subdirectories in the directory and search 
  // within those locations.
  std::vector<std::string> child_dirs = get_subdirectories( search_path );

  for( std::vector<std::string>::iterator it = child_dirs.begin(); it != child_dirs.end(); it++ ) {
    std::string sub_path = *it;
    try {
      bool result = find_file( path_to_file, file_to_find, sub_path, true );
      if( result ) return result;
    } catch( std::exception ex ) {
 
    }
  }
 
  // otherwise failed to find file.
  return false;
}
Пример #2
0
bool search_for_file( const std::string &base_path,
						const std::string &base_name,
						const char **valid_exts,
						std::string &result )
{
	std::vector<std::string> result_list;
	std::vector<std::string> ignore_list;

	if ( get_filename_from_base(
		result_list, ignore_list, base_path, base_name, valid_exts )  )
	{
		result = result_list.front();
		return true;
	}

	std::vector<std::string> subs;
	get_subdirectories( subs, base_path );

	std::vector<std::string>::iterator itr;
	for ( itr = subs.begin(); itr != subs.end(); ++itr )
	{

		if ( search_for_file(
				base_path + (*itr) + "/",
				base_name,
				valid_exts,
				result ) )
		{
			return true;
		}
	}

	return false;
}