/[adonthell]/adonthell/src/base/paths.cc
ViewVC logotype

Diff of /adonthell/src/base/paths.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by ksterker, Mon Aug 2 07:35:28 2004 UTC revision 1.5 by ksterker, Mon Oct 18 07:40:23 2004 UTC
# Line 30  Line 30 
30    
31  #include <cstdlib>  #include <cstdlib>
32  #include <iostream>  #include <iostream>
33    #include <dirent.h>
34    #include <sys/stat.h>
35    
36  #include "base/paths.h"  #include "base/paths.h"
37    
# Line 69  namespace base Line 71  namespace base
71  }  }
72    
73  // initialize data search paths  // initialize data search paths
74  void paths::init (const std::string & game, const std::string & userdatadir)  bool paths::init (const std::string & game, const std::string & userdatadir)
75  {  {
76      // no save game directory unless we actually load a game      // no save game directory unless we actually load a game
77      SaveDataDir = "";      IncludeSaveDir = false;
78        IncludeUserDir = false;
79            
80        // set OS specific directory containing configuration and saved games
81    #if defined(__APPLE__)          // OSX
82        CfgDataDir = string (getenv ("HOME")) + "/Library/Adonthell/";
83    #elif defined (WIN32)           // Windows
84        CfgDataDir = "./";
85    #else                                           // Unix
86        CfgDataDir = string (getenv ("HOME")) + "/.adonthell/";
87    #endif
88    
89    #ifndef WIN32
90        // make sure configuration directory exists, otherwise create is
91        if (!exists (CfgDataDir)) mkdir (CfgDataDir.c_str (), 0700);
92    #endif
93    
94      // user data dir might be optional      // user data dir might be optional
95      UserDataDir = userdatadir;      UserDataDir = userdatadir;
96      if (UserDataDir != "")      if (UserDataDir != "")
97      {      {
98          if (UserDataDir[UserDataDir.size () - 1] != '/') UserDataDir += "/";          if (UserDataDir[UserDataDir.size () - 1] != '/') UserDataDir += "/";
99          UserDataDir += game;          UserDataDir += game + "/";
100            
101            // make sure the given user data dir is actually accessible
102            if (exists (UserDataDir)) IncludeUserDir = true;        
103      }      }
104            
105      // builtin data directory      // builtin data directory
106      GameDataDir = DATA_DIR;      GameDataDir = DATA_DIR;
107      GameDataDir += "/games/";      GameDataDir += "/games/";
108      GameDataDir += game;      GameDataDir += game + "/";
109        
110        // make sure game data dir exists
111        return exists (GameDataDir);
112    }
113    
114    // set path to saved game
115    void paths::set_save_dir (const std::string & dir)
116    {
117        if (dir != "" && exists (CfgDataDir + dir))
118        {
119            SaveDataDir = CfgDataDir + dir;
120            IncludeSaveDir = true;  
121        }
122        else
123        {
124            IncludeSaveDir = false;        
125            SaveDataDir = "";
126        }
127    }
128    
129    // open the specified file
130    bool paths::open (igzstream & file, const std::string & path)
131    {
132        if (file.is_open ()) file.close ();
133        
134        if (IncludeSaveDir && file.open (SaveDataDir + path)) return true;
135        if (IncludeUserDir && file.open (UserDataDir + path)) return true;
136        if (file.open (GameDataDir + path)) return true;
137    
138        fprintf (stderr, "*** paths::open: file '%s' does not exist in search path:\n", path.c_str ());
139        if (IncludeSaveDir) fprintf (stderr, "  - %s\n", SaveDataDir.c_str ());
140        if (IncludeUserDir) fprintf (stderr, "  - %s\n", UserDataDir.c_str ());
141        fprintf (stderr, "  - %s\n", GameDataDir.c_str ());
142    
143        return false;
144    }
145    
146    // check whether given path exists at all
147    bool paths::exists (const std::string & path)
148    {
149        DIR *dir = opendir (path.c_str ());
150        if (dir != NULL)
151        {
152            closedir (dir);
153            return true;
154        }
155        
156        // dir doesn't exist or not enough privileges ...
157        fprintf (stderr, "*** warning: directory '%s' cannot be accessed!\n", path.c_str ());
158        return false;
159  }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26