| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2012 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | https://blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | *This is the common.h |
| 26 | *It contains some basic functions that nearly all multi platform games are going |
| 27 | *to need. |
| 28 | */ |
| 29 | |
| 30 | #ifndef _COMMON_H |
| 31 | #define _COMMON_H |
| 32 | |
| 33 | #include <string> |
| 34 | #include <iostream> |
| 35 | #include <map> |
| 36 | #include <stdlib.h> |
| 37 | #include <libintl.h> |
| 38 | #include <ctime> |
| 39 | |
| 40 | |
| 41 | #define _(String) gettext (String) |
| 42 | |
| 43 | struct commonTime |
| 44 | { |
| 45 | unsigned int days = 0; |
| 46 | unsigned int hours = 0; |
| 47 | unsigned int minutes = 0; |
| 48 | unsigned int seconds = 0; |
| 49 | }; |
| 50 | |
| 51 | bool strequals(const char* a, const char* b); |
| 52 | |
| 53 | /** |
| 54 | * str2int parses a string and returns an int with the value of the string. |
| 55 | * if the string is not an int then 0 is returned instead of throwing an error |
| 56 | * in that way this function will always return a usable value. |
| 57 | */ |
| 58 | int str2int(const std::string &str2parse) __attribute__((const)); |
| 59 | |
| 60 | void dieOnNullptr(bool, const char* msg); |
| 61 | |
| 62 | /** |
| 63 | * str2double parses a string and returns a double with the value of the string. |
| 64 | * if the string is not a double then 0.0 is returned instead of throwing an error |
| 65 | * in that way this function will always return a usable value. |
| 66 | */ |
| 67 | double str2double(const std::string &str2parse) __attribute__((const)); |
| 68 | |
| 69 | class TimeHandler |
| 70 | { |
| 71 | public: |
| 72 | static commonTime ms2ct(unsigned int milliseconds); |
| 73 | |
| 74 | static commonTime getTime(const std::string &name); |
| 75 | |
| 76 | static commonTime peekTime(const std::string &name, const commonTime &toAdd); |
| 77 | |
| 78 | static commonTime addTime(const std::string &name, const commonTime &toAdd); |
| 79 | }; |
| 80 | |
| 81 | #define MAX_VAR_LENGTH 1024 |
| 82 | |
| 83 | /*This is the Config class it is a map to hold config variables. |
| 84 | *It is inspired by Quake 3's CVAR system although a lot simpler. |
| 85 | *All variables have keys "varName" that is used to access a variable. |
| 86 | * |
| 87 | *This class is a singleton |
| 88 | */ |
| 89 | class Config |
| 90 | { |
| 91 | private: |
| 92 | std::map<std::string, std::string> configMap; |
| 93 | |
| 94 | static Config *instance; |
| 95 | |
| 96 | void load(); |
| 97 | |
| 98 | /* tells if the user has requested a shutdown */ |
| 99 | long shuttingDown = 0; |
| 100 | |
| 101 | Config(); |
| 102 | |
| 103 | |
| 104 | public: |
| 105 | /*Config is a singleton. |
| 106 | *It is accessed like this: |
| 107 | *Config::getInstance()->method2call(parameters); |
| 108 | */ |
| 109 | static Config* getInstance(); |
| 110 | |
| 111 | /*save() |
| 112 | *forces the config to be written to disk. This will also happened if the |
| 113 | *program terminates normally. |
| 114 | */ |
| 115 | void save(); |
| 116 | |
| 117 | /*getString(varName) |
| 118 | *Looks in the config file and returns the string that matches the key "varName" |
| 119 | *Returns an empty string if varName does not exist. |
| 120 | */ |
| 121 | std::string getString(const std::string &varName); |
| 122 | |
| 123 | /*getInt(varName) |
| 124 | *Looks in the config file and returns the int that matches the key "varName" |
| 125 | *Returns "0" if varName does not exist or cannot be parsed. |
| 126 | */ |
| 127 | int getInt(const std::string &varName); |
| 128 | |
| 129 | /** |
| 130 | * Gets an int. If not set will set a default value. |
| 131 | */ |
| 132 | int getInt(const std::string& varName, int defaultValue); |
| 133 | |
| 134 | /*getValue(varName) |
| 135 | *Looks in the config file and returns the double that matches the key "varName" |
| 136 | *Returns "0.0" if varName does not exist or cannot be parsed. |
| 137 | */ |
| 138 | double getValue(const std::string &varName); |
| 139 | |
| 140 | /*setString(varName,content) |
| 141 | *Sets the config variable with key "varName" to the value of "content" |
| 142 | */ |
| 143 | void setString(const std::string &varName, const std::string &content); |
| 144 | |
| 145 | /*setInt(varName,content) |
| 146 | *Sets the config variable with key "varName" to the value of "content" |
| 147 | */ |
| 148 | void setInt(const std::string &varName, int content); |
| 149 | |
| 150 | /** |
| 151 | * Sets a config variable to a given (double) value |
| 152 | * @param varName Name of the variable to set |
| 153 | * @param content Value to give the variable |
| 154 | */ |
| 155 | void setValue(const std::string &varName,double content); |
| 156 | |
| 157 | /** |
| 158 | * returns true if the key varName exists. This is used the first time 1.4.0 |
| 159 | * starts so that it can see that it has to import configs from an earlier |
| 160 | * version. |
| 161 | * @param varName Name of the variable |
| 162 | * @return true if the variable exists |
| 163 | */ |
| 164 | bool exists(const std::string &varName) const; |
| 165 | |
| 166 | /*setDefault(varName,value) |
| 167 | *if the variable "varName" does not exist it will be created with value "value" |
| 168 | *if varName exists then this will have no effect |
| 169 | */ |
| 170 | /** |
| 171 | * Set default value for a variable. If the variable "varName" does not exist it will be created with value "value" |
| 172 | * if varName exists then this will have no effect |
| 173 | * @param varName Name of the variable |
| 174 | * @param content The default value |
| 175 | */ |
| 176 | void setDefault(const std::string &varName, const std::string &content); |
| 177 | |
| 178 | /** |
| 179 | * Should be set if the user has requested the program to shutdown. |
| 180 | * @param shuttingDown value of shutdown command. 5 = default = shutdown but allow saving |
| 181 | */ |
| 182 | void setShuttingDown(long shuttingDown = 5); |
| 183 | |
| 184 | /** |
| 185 | * tells if the user wants to shutdown. This can be used if the exit button is pressed deeply in the program. |
| 186 | * @return |
| 187 | */ |
| 188 | long isShuttingDown() const; |
| 189 | }; |
| 190 | |
| 191 | #endif /* _COMMON_H */ |
| 192 |