FileSystem Namespace Reference


Functions

std::string dirname (const std::string &filename)
 returns the path of the directory the file is in
std::string basename (const std::string &filename)
 returns the name of the file
std::string strip_extension (const std::string &filename)
 remove everything starting from and including the last dot
std::string normalize (const std::string &filename)
 normalize filename so that "blup/bla/blo/../../bar" will become "blup/bar"


Function Documentation

std::string FileSystem::basename ( const std::string &  filename  ) 

returns the name of the file

Definition at line 35 of file file_system.cpp.

Referenced by Font::Font(), SpriteManager::load(), Main::run(), and TitleScreen::start_game().

00036 {
00037   std::string::size_type p = filename.find_last_of('/');
00038   if(p == std::string::npos)
00039     p = filename.find_last_of('\\');
00040   if(p == std::string::npos)
00041     return filename;
00042 
00043   return filename.substr(p+1, filename.size()-p-1);
00044 }

std::string FileSystem::dirname ( const std::string &  filename  ) 

returns the path of the directory the file is in

Definition at line 24 of file file_system.cpp.

Referenced by Sector::activate(), Font::Font(), GameSession::get_working_directory(), worldmap::WorldMap::load(), World::load(), SpriteManager::load(), load_music_file(), TileSetParser::parse(), Main::run(), and World::set_savegame_filename().

00025 {
00026   std::string::size_type p = filename.find_last_of('/');
00027   if(p == std::string::npos)
00028     p = filename.find_last_of('\\');
00029   if(p == std::string::npos)
00030     return "./";
00031 
00032   return filename.substr(0, p+1);
00033 }

std::string FileSystem::normalize ( const std::string &  filename  ) 

normalize filename so that "blup/bla/blo/../../bar" will become "blup/bar"

Definition at line 55 of file file_system.cpp.

References log_warning.

Referenced by TextureManager::get(), and load_music_file().

00056 {
00057   std::vector<std::string> path_stack;
00058 
00059   const char* p = filename.c_str();
00060 
00061   while(true) {
00062     while(*p == '/' || *p == '\\') {
00063       p++;
00064       continue;
00065     }
00066 
00067     const char* pstart = p;
00068     while(*p != '/' && *p != '\\' && *p != 0) {
00069       ++p;
00070     }
00071 
00072     size_t len = p - pstart;
00073     if(len == 0)
00074       break;
00075 
00076     std::string pathelem(pstart, p-pstart);
00077     if(pathelem == ".")
00078       continue;
00079 
00080     if(pathelem == "..") {
00081       if(path_stack.empty()) {
00082 
00083         log_warning << "Invalid '..' in path '" << filename << "'" << std::endl;
00084         // push it into the result path so that the user sees his error...
00085         path_stack.push_back(pathelem);
00086       } else {
00087         path_stack.pop_back();
00088       }
00089     } else {
00090       path_stack.push_back(pathelem);
00091     }
00092   }
00093 
00094   // construct path
00095   std::ostringstream result;
00096   for(std::vector<std::string>::iterator i = path_stack.begin();
00097       i != path_stack.end(); ++i) {
00098     result << '/' << *i;
00099   }
00100   if(path_stack.empty())
00101     result << '/';
00102 
00103   return result.str();
00104 }

std::string FileSystem::strip_extension ( const std::string &  filename  ) 

remove everything starting from and including the last dot

Definition at line 46 of file file_system.cpp.

00047 {
00048   std::string::size_type p = filename.find_last_of('.');
00049   if(p == std::string::npos)
00050     return filename;
00051 
00052   return filename.substr(0, p);
00053 }


Generated on Mon Jun 9 03:38:38 2014 for SuperTux by  doxygen 1.5.1