00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "game.h"
00028 #include <stdlib.h>
00029 #include <sys/types.h>
00030 #include <dirent.h>
00031
00032
00033 string game::User_data_dir;
00034 string game::Global_data_dir;
00035 string game::Game_data_dir;
00036
00037
00038 void game::init (string game_dir)
00039 {
00040 Global_data_dir = game_dir;
00041 #ifndef SINGLE_DIR_INST
00042 User_data_dir = getenv ("HOME");
00043 User_data_dir += "/.adonthell";
00044 #else
00045 User_data_dir = Global_data_dir;
00046 #endif
00047 }
00048
00049 void game::set_game_data_dir(string game_dir)
00050 {
00051 Game_data_dir = game_dir;
00052 }
00053
00054 bool game::directory_exist (const string & dirname)
00055 {
00056 DIR * dir = opendir (dirname.c_str ());
00057
00058 if (dir)
00059 {
00060 closedir (dir);
00061 return true;
00062 }
00063
00064 return false;
00065 }
00066
00067 bool game::file_exist (const string & fname)
00068 {
00069 FILE * file = fopen (fname.c_str (), "r");
00070
00071 if (file)
00072 {
00073 fclose (file);
00074 return true;
00075 }
00076
00077 return false;
00078 }
00079
00080 string game::find_file (const string & fname)
00081 {
00082 string ret;
00083
00084
00085 if (fname[0] == '/') return fname;
00086
00087
00088 if ((ret = game_data_dir () + "/") != "/" && file_exist (ret + fname))
00089 ret += fname;
00090
00091 else if (file_exist ((ret = global_data_dir () + "/") + fname))
00092 ret += fname;
00093
00094 else if (file_exist ((ret = user_data_dir () + "/") + fname))
00095 ret += fname;
00096
00097 else ret = "";
00098
00099 return ret;
00100 }
00101
00102 string game::find_directory (const string & dirname)
00103 {
00104 string ret;
00105
00106
00107 if (dirname[0] == '/') return dirname;
00108
00109
00110 if ((ret = game_data_dir () + "/") != "/" && directory_exist (ret + dirname))
00111 ret += dirname;
00112
00113 else if (directory_exist ((ret = global_data_dir () + "/") + dirname))
00114 ret += dirname;
00115
00116 else if (directory_exist ((ret = user_data_dir () + "/") + dirname))
00117 ret += dirname;
00118
00119 else ret = "";
00120
00121 return ret;
00122 }