Adonthell  0.4
game.cc
Go to the documentation of this file.
1 /*
2  $Id: game.cc,v 1.28 2003/02/20 17:27:41 ksterker Exp $
3 
4  Copyright (C) 1999/2000/2001/2002 Kai Sterker <kaisterker@linuxgames.com>
5  Copyright (C) 2002 Alexandre Courbot <alexandrecourbot@linuxgames.com>
6  Part of the Adonthell Project http://adonthell.linuxgames.com
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file game.cc
18  * @author Kai Sterker <kaisterker@linuxgames.com>
19  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
20  *
21  * @brief Defines the game class.
22  *
23  *
24  */
25 
26 
27 #include "game.h"
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 
32 
33 string game::User_data_dir;
34 string game::Global_data_dir;
35 string game::Game_data_dir;
36 
37 
38 void game::init (string game_dir)
39 {
40  Global_data_dir = game_dir;
41 #ifndef SINGLE_DIR_INST
42  User_data_dir = getenv ("HOME");
43  User_data_dir += "/.adonthell";
44 #else
45  User_data_dir = Global_data_dir;
46 #endif
47 }
48 
49 void game::set_game_data_dir(string game_dir)
50 {
51  Game_data_dir = game_dir;
52 }
53 
54 bool game::directory_exist (const string & dirname)
55 {
56  DIR * dir = opendir (dirname.c_str ());
57 
58  if (dir)
59  {
60  closedir (dir);
61  return true;
62  }
63 
64  return false;
65 }
66 
67 bool game::file_exist (const string & fname)
68 {
69  FILE * file = fopen (fname.c_str (), "r");
70 
71  if (file)
72  {
73  fclose (file);
74  return true;
75  }
76 
77  return false;
78 }
79 
80 string game::find_file (const string & fname)
81 {
82  string ret;
83 
84  // If the name is already absolute, no need to search...
85  if (fname[0] == '/') return fname;
86 
87  // First check in the current game directory
88  if ((ret = game_data_dir () + "/") != "/" && file_exist (ret + fname))
89  ret += fname;
90  // Then check the global data directory
91  else if (file_exist ((ret = global_data_dir () + "/") + fname))
92  ret += fname;
93  // Finally, try the user data directory
94  else if (file_exist ((ret = user_data_dir () + "/") + fname))
95  ret += fname;
96  // Nothing found! So bad...
97  else ret = "";
98 
99  return ret;
100 }
101 
102 string game::find_directory (const string & dirname)
103 {
104  string ret;
105 
106  // If the name is already absolute, no need to search...
107  if (dirname[0] == '/') return dirname;
108 
109  // First check in the current game directory
110  if ((ret = game_data_dir () + "/") != "/" && directory_exist (ret + dirname))
111  ret += dirname;
112  // Then check the global data directory
113  else if (directory_exist ((ret = global_data_dir () + "/") + dirname))
114  ret += dirname;
115  // Finally, try the user data directory
116  else if (directory_exist ((ret = user_data_dir () + "/") + dirname))
117  ret += dirname;
118  // Nothing found! So bad...
119  else ret = "";
120 
121  return ret;
122 }
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().
Definition: game.cc:102
Declares the game class.
static void init(string game_dir)
Initialise the game framework.
Definition: game.cc:38
static string global_data_dir()
Returns the absolute path to the global data directory.
Definition: game.h:86
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().
Definition: game.cc:80
static string game_data_dir()
Returns the absolute path to the current game's directory (if any).
Definition: game.h:97
static void set_game_data_dir(string game_dir)
Specify an additional data directory containing game data.
Definition: game.cc:49
static string user_data_dir()
Returns the absolute path to the user data directory (usually ~/.adonthell).
Definition: game.h:75