Adonthell 0.4
|
00001 /* 00002 $Id: game.cc,v 1.28 2003/02/20 17:27:41 ksterker Exp $ 00003 00004 Copyright (C) 1999/2000/2001/2002 Kai Sterker <kaisterker@linuxgames.com> 00005 Copyright (C) 2002 Alexandre Courbot <alexandrecourbot@linuxgames.com> 00006 Part of the Adonthell Project http://adonthell.linuxgames.com 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License. 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY. 00012 00013 See the COPYING file for more details. 00014 */ 00015 00016 /** 00017 * @file game.cc 00018 * @author Kai Sterker <kaisterker@linuxgames.com> 00019 * @author Alexandre Courbot <alexandrecourbot@linuxgames.com> 00020 * 00021 * @brief Defines the game class. 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 // If the name is already absolute, no need to search... 00085 if (fname[0] == '/') return fname; 00086 00087 // First check in the current game directory 00088 if ((ret = game_data_dir () + "/") != "/" && file_exist (ret + fname)) 00089 ret += fname; 00090 // Then check the global data directory 00091 else if (file_exist ((ret = global_data_dir () + "/") + fname)) 00092 ret += fname; 00093 // Finally, try the user data directory 00094 else if (file_exist ((ret = user_data_dir () + "/") + fname)) 00095 ret += fname; 00096 // Nothing found! So bad... 00097 else ret = ""; 00098 00099 return ret; 00100 } 00101 00102 string game::find_directory (const string & dirname) 00103 { 00104 string ret; 00105 00106 // If the name is already absolute, no need to search... 00107 if (dirname[0] == '/') return dirname; 00108 00109 // First check in the current game directory 00110 if ((ret = game_data_dir () + "/") != "/" && directory_exist (ret + dirname)) 00111 ret += dirname; 00112 // Then check the global data directory 00113 else if (directory_exist ((ret = global_data_dir () + "/") + dirname)) 00114 ret += dirname; 00115 // Finally, try the user data directory 00116 else if (directory_exist ((ret = user_data_dir () + "/") + dirname)) 00117 ret += dirname; 00118 // Nothing found! So bad... 00119 else ret = ""; 00120 00121 return ret; 00122 }