Back to index

supertuxkart  0.5+dfsg1
Public Member Functions | Private Member Functions | Private Attributes
FileManager Class Reference

#include <file_manager.hpp>

Collaboration diagram for FileManager:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 FileManager ()
 ~FileManager ()
std::string getHomeDir () const
std::string getTrackDir () const
std::string getKartDir () const
std::string getHerringDir () const
std::string getTranslationDir () const
std::vector< std::string > getMusicDirs () const
std::string getTextureFile (const std::string &fname) const
std::string getKartFile (const std::string &fname, const std::string &kart="") const
std::string getTrackFile (const std::string &fname, const std::string &track="") const
std::string getConfigFile (const std::string &fname) const
std::string getHighscoreFile (const std::string &fname) const
std::string getLogFile (const std::string &fname) const
std::string getHerringFile (const std::string &fname) const
std::string getMusicFile (const std::string &fname) const
std::string getSFXFile (const std::string &fname) const
std::string getFontFile (const std::string &fname) const
std::string getModelFile (const std::string &fname) const
void listFiles (std::set< std::string > &result, const std::string &dir, bool is_full_path=false, bool make_full_path=false) const
void pushTextureSearchPath (const std::string &path)
void pushModelSearchPath (const std::string &path)
void pushMusicSearchPath (const std::string &path)
void popTextureSearchPath ()
void popModelSearchPath ()
void popMusicSearchPath ()
void initConfigDir ()

Private Member Functions

bool findFile (std::string &full_path, const std::string &fname, const std::vector< std::string > &search_path) const
void makePath (std::string &path, const std::string &dir, const std::string &fname) const

Private Attributes

bool m_is_full_path
std::string m_root_dir
std::vector< std::string > m_texture_search_path
std::vector< std::string > m_model_search_path
std::vector< std::string > m_music_search_path

Detailed Description

Definition at line 28 of file file_manager.hpp.


Constructor & Destructor Documentation

Definition at line 85 of file file_manager.cpp.

{
    m_is_full_path = false;
    
    if ( getenv ( "SUPERTUXKART_DATADIR" ) != NULL )
        m_root_dir= getenv ( "SUPERTUXKART_DATADIR" ) ;
#ifdef __APPLE__
    else if( macSetBundlePathIfRelevant( m_root_dir ) ) { /* nothing to do */ }
#endif
    else if ( access ( "data/stk_config.data", F_OK ) == 0 )
        m_root_dir = "." ;
    else if ( access ( "../data/stk_config.data", F_OK ) == 0 )
        m_root_dir = ".." ;
    else
#ifdef SUPERTUXKART_DATADIR
        m_root_dir = SUPERTUXKART_DATADIR ;
#else
        m_root_dir = "/usr/local/share/games/supertuxkart" ;
#endif
    // We can't use _() here, since translations will only be initalised
    // after the filemanager (to get the path to the tranlsations from it)
    fprintf(stderr, "Data files will be fetched from: '%s'\n", 
            m_root_dir.c_str() );

    pushTextureSearchPath(m_root_dir+"/data/textures");
    pushModelSearchPath  (m_root_dir+"/data/models"  );
    pushMusicSearchPath  (m_root_dir+"/data/music"   );
    // Add more paths from the STK_MUSIC_PATH environment variable
    if(getenv("SUPERTUXKART_MUSIC_PATH")!=NULL)
    {
        std::string path=getenv("SUPERTUXKART_MUSIC_PATH");
        std::vector<std::string> dirs=StringUtils::split(path,':');
        for(int i=(int)dirs.size()-1; i>=0; i--)
        {
            // Remove '/' at the end of paths, since this can cause
            // problems with windows when using stat()
            while(dirs[i].size()>=1 && dirs[i][dirs[i].size()-1]=='/')
            {
                dirs[i]=dirs[i].substr(0, dirs[i].size()-1);
            }
            // remove empty entries
            if(dirs[i].size()==0)
            {
                dirs.erase(dirs.begin()+i);
                continue;
            }
        }
#ifdef WIN32
        // Handle filenames like d:/dir, which becomes ["d","/dir"]
        for(int i=(int)dirs.size()-1; i>=0; i--)
        {
            if(dirs[i].size()>1) continue;
            if(i==dirs.size()-1)    // last element
            {
                dirs[i]+=":";      // turn "c" back into "c:"
            }
            else
            {
                dirs[i]+=":"+dirs[i+1]; // restore "d:/dir" back 
                dirs.erase(dirs.begin()+i+1);
            }
        }
#endif
        for(int i=0;i<(int)dirs.size(); i++)
            pushMusicSearchPath(dirs[i]);
    }
}  // FileManager

Here is the call graph for this function:

Definition at line 154 of file file_manager.cpp.

Here is the call graph for this function:


Member Function Documentation

bool FileManager::findFile ( std::string &  full_path,
const std::string &  fname,
const std::vector< std::string > &  search_path 
) const [private]

Definition at line 162 of file file_manager.cpp.

{
    struct stat mystat;
    
    for(std::vector<std::string>::const_iterator i = search_path.begin();
        i != search_path.end(); ++i)
    {
        //full_path=m_root_dir + "/" + *i + "/" + fname;
        full_path = *i + "/" + fname;
        if(stat(full_path.c_str(), &mystat) >= 0) return true;
    }
    full_path="";
    return false;
}   // findFile

Here is the caller graph for this function:

std::string FileManager::getConfigFile ( const std::string &  fname) const

Definition at line 249 of file file_manager.cpp.

{
    return m_root_dir+"/data/"+fname;
}   // getConfigFile

Here is the caller graph for this function:

std::string FileManager::getFontFile ( const std::string &  fname) const

Definition at line 303 of file file_manager.cpp.

{
    return m_root_dir+"/data/fonts/"+fname;
}   // getFontFile

Here is the caller graph for this function:

std::string FileManager::getHerringDir ( ) const

Definition at line 208 of file file_manager.cpp.

{
    return m_root_dir+"/data/herrings";
}   // getHerringDir

Here is the caller graph for this function:

std::string FileManager::getHerringFile ( const std::string &  fname) const

Definition at line 255 of file file_manager.cpp.

{
    return getHerringDir()+"/"+fname;
}   // getConfigFile

Here is the call graph for this function:

std::string FileManager::getHighscoreFile ( const std::string &  fname) const

Definition at line 308 of file file_manager.cpp.

{
    return getHomeDir()+"/"+fname;
}   // getHighscoreFile

Here is the call graph for this function:

Here is the caller graph for this function:

std::string FileManager::getHomeDir ( ) const

Definition at line 261 of file file_manager.cpp.

{
    std::string DIRNAME;
#ifdef WIN32
    // For now the old windows config way is used: store a config file
    // in the current directory (in other OS a special subdirectory is created)
    DIRNAME=".";
#else
    if(getenv("HOME")!=NULL)
    {
        DIRNAME = getenv("HOME");
    }
    else
    {
        DIRNAME = ".";
    }
    DIRNAME += "/";
    DIRNAME += CONFIGDIR;
#endif
    return DIRNAME;
}   // getHomeDir

Here is the caller graph for this function:

std::string FileManager::getKartDir ( ) const

Definition at line 202 of file file_manager.cpp.

{
    return m_root_dir+"/data/karts";
}   // getKartDir

Here is the caller graph for this function:

std::string FileManager::getKartFile ( const std::string &  fname,
const std::string &  kart = "" 
) const

Definition at line 237 of file file_manager.cpp.

{
    // kart file are in data/karts/KARTNAME/KARTNAME.ext
    // but if a kart name is supplied use it (which is necessary
    // e.g. to load a model from a kart directory
    std::string basename = (kart_name!="") ? kart_name 
                           : StringUtils::without_extension(fname);
    return getKartDir()+"/"+basename+"/"+fname;
}   // getKartFile

Here is the call graph for this function:

Here is the caller graph for this function:

std::string FileManager::getLogFile ( const std::string &  fname) const

Definition at line 284 of file file_manager.cpp.

{
    return getHomeDir()+"/"+fname;
}   // getLogFile

Here is the call graph for this function:

Here is the caller graph for this function:

std::string FileManager::getModelFile ( const std::string &  fname) const

Definition at line 188 of file file_manager.cpp.

{
    std::string path;
    findFile(path, FNAME, m_model_search_path);
    return path;
}   // getModelFile

Here is the call graph for this function:

Here is the caller graph for this function:

std::vector< std::string > FileManager::getMusicDirs ( ) const

Definition at line 219 of file file_manager.cpp.

{
    return m_music_search_path;
}   // getMusicDirs

Here is the caller graph for this function:

std::string FileManager::getMusicFile ( const std::string &  fname) const

Definition at line 290 of file file_manager.cpp.

{
    std::string path;
    findFile(path, fname, m_music_search_path);
    return path;
}   // getMusicFile

Here is the call graph for this function:

Here is the caller graph for this function:

std::string FileManager::getSFXFile ( const std::string &  fname) const

Definition at line 298 of file file_manager.cpp.

{
    return m_root_dir+"/data/sfx/"+fname;
}   // getSFXFile
std::string FileManager::getTextureFile ( const std::string &  fname) const

Definition at line 180 of file file_manager.cpp.

{
    std::string path;
    findFile(path, FNAME, m_texture_search_path);
    return path;
}   // makeTexturePath

Here is the call graph for this function:

Here is the caller graph for this function:

std::string FileManager::getTrackDir ( ) const

Definition at line 196 of file file_manager.cpp.

{
    return m_root_dir+"/data/tracks";
}   // getTrackDir

Here is the caller graph for this function:

std::string FileManager::getTrackFile ( const std::string &  fname,
const std::string &  track = "" 
) const

Definition at line 225 of file file_manager.cpp.

{
    // tracks file are in data/tracks/TRACKNAME/TRACKNAME.ext
    // but if a track name is supplied use it (which is necessary
    // e.g. to load a model from a track directory
    std::string basename = (track_name!="") ? track_name 
                           : StringUtils::without_extension(fname);
    return getTrackDir()+"/"+basename+"/"+fname;
}   // getTrackFile

Here is the call graph for this function:

Here is the caller graph for this function:

std::string FileManager::getTranslationDir ( ) const

Definition at line 213 of file file_manager.cpp.

{
    return m_root_dir+"/data/po";
}   // getTranslationDir

Here is the caller graph for this function:

Definition at line 321 of file file_manager.cpp.

{
#ifdef WIN32
    /*nothing*/
#else
    /*if HOME environment variable exists
    create directory $HOME/.supertuxkart*/
    if(getenv("HOME")!=NULL)
    {
        std::string pathname;
        pathname = getenv("HOME");
        pathname += "/.supertuxkart";
        mkdir(pathname.c_str(), 0755);
    }
#endif
}   // initConfigDir
void FileManager::listFiles ( std::set< std::string > &  result,
const std::string &  dir,
bool  is_full_path = false,
bool  make_full_path = false 
) const

Definition at line 339 of file file_manager.cpp.

{
        struct stat mystat;

        // don't list directories with a slash on the end, it'll fail on win32
//        assert(dir[dir.size()-1] != '/');

        result.clear();

        std::string path = is_full_path ? dir : m_root_dir+"/"+dir;

        if(stat(path.c_str(), &mystat) < 0) return;
        if(! S_ISDIR(mystat.st_mode))       return; 

        ulDir* mydir = ulOpenDir(path.c_str());
        if(!mydir) return;

        ulDirEnt* mydirent;
        while( (mydirent = ulReadDir(mydir)) != 0)
        {
            result.insert(make_full_path ? path+"/"+mydirent->d_name
                                         :          mydirent->d_name);
        }
        ulCloseDir(mydir);
}   // listFiles

Here is the call graph for this function:

Here is the caller graph for this function:

void FileManager::makePath ( std::string &  path,
const std::string &  dir,
const std::string &  fname 
) const [private]

Definition at line 77 of file file_manager.hpp.

{m_model_search_path.pop_back();     }

Here is the caller graph for this function:

Definition at line 78 of file file_manager.hpp.

{m_music_search_path.pop_back();     }

Here is the caller graph for this function:

Definition at line 76 of file file_manager.hpp.

{m_texture_search_path.pop_back();   }

Here is the caller graph for this function:

void FileManager::pushModelSearchPath ( const std::string &  path) [inline]

Definition at line 72 of file file_manager.hpp.

                                    { m_model_search_path.push_back(path);  }

Here is the caller graph for this function:

void FileManager::pushMusicSearchPath ( const std::string &  path) [inline]

Definition at line 74 of file file_manager.hpp.

                                    { m_music_search_path.push_back(path);  }

Here is the caller graph for this function:

void FileManager::pushTextureSearchPath ( const std::string &  path) [inline]

Definition at line 70 of file file_manager.hpp.

                                    { m_texture_search_path.push_back(path);}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 31 of file file_manager.hpp.

std::vector<std::string> FileManager::m_model_search_path [private]

Definition at line 33 of file file_manager.hpp.

std::vector<std::string> FileManager::m_music_search_path [private]

Definition at line 33 of file file_manager.hpp.

std::string FileManager::m_root_dir [private]

Definition at line 32 of file file_manager.hpp.

std::vector<std::string> FileManager::m_texture_search_path [private]

Definition at line 33 of file file_manager.hpp.


The documentation for this class was generated from the following files: