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))