36 #include <sys/types.h> 40 string game::User_data_dir;
41 string game::Global_data_dir;
42 string game::Game_data_dir;
47 Global_data_dir = game_dir;
55 Game_data_dir = game_dir;
61 #if defined(__APPLE__) 63 result = string (getenv (
"HOME")) +
"/Library/Application Support/Adonthell/";
66 char *appDataDir = getenv (
"APPDATA");
67 if (appDataDir != NULL && strlen (appDataDir) > 0)
68 result = string (getenv(
"APPDATA")) +
"/Adonthell/";
73 const char* xdgEnv = type == USER_DATA ?
"XDG_DATA_HOME" :
"XDG_CONFIG_HOME";
74 char *xdgDir = getenv (xdgEnv);
75 if (xdgDir != NULL && strlen (xdgDir) > 0)
76 result = string (xdgDir) +
"/adonthell";
79 if (type == USER_DATA)
80 result = string (getenv (
"HOME")) +
"/.local/share/adonthell/";
82 result = string (getenv (
"HOME")) +
"/.config/adonthell/";
87 if (!game::directory_exist (result))
90 if (mkdir (result.c_str (), 0700) == -1)
92 if (mkdir (result.c_str ()) == -1)
96 std::cerr <<
"Creating directory '" << result <<
"' failed: " 97 << strerror (ecd) << std::endl;
104 bool game::directory_exist (
const string & dirname)
106 DIR * dir = opendir (dirname.c_str ());
117 bool game::file_exist (
const string & fname)
119 FILE * file = fopen (fname.c_str (),
"r");
135 if (fname[0] ==
'/')
return fname;
138 if ((ret =
game_data_dir () +
"/") !=
"/" && file_exist (ret + fname))
144 else if (file_exist ((ret =
user_data_dir () +
"/") + fname))
157 if (dirname[0] ==
'/')
return dirname;
160 if ((ret =
game_data_dir () +
"/") !=
"/" && directory_exist (ret + dirname))
166 else if (directory_exist ((ret =
user_data_dir () +
"/") + dirname))
static string find_directory(const string &dirname)
Finds a directory in the directories hierarchy, starting searching from game_data_dir(), then global_data_dir() and finally user_data_dir().
static void init(string game_dir)
Initialise the game framework.
static string global_data_dir()
Returns the absolute path to the global data directory.
static string find_file(const string &fname)
Finds a file in the directories hierarchy, starting searching from game_data_dir(), then global_data_dir() and finally user_data_dir().
static string get_system_dir(const sys_dir_type &type)
Return the OS-specific directory of the given type.
static string game_data_dir()
Returns the absolute path to the current game's directory (if any).
static void set_game_data_dir(string game_dir)
Specify an additional data directory containing game data.
static string user_data_dir()
Returns the absolute path to the user data directory (usually ~/.adonthell).