Changeset 159
- Timestamp:
- 26/09/08 11:32:18 (5 years ago)
- Location:
- trunk/src
- Files:
-
- 4 edited
-
DBwrapper.cpp (modified) (6 diffs)
-
DBwrapper.h (modified) (1 diff)
-
configuration.cpp (modified) (3 diffs)
-
configuration.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/DBwrapper.cpp
r73 r159 41 41 { 42 42 m_errmsg=0; //set the pointer to null 43 db_loaded = false; 43 44 44 45 m_errorcode = sqlite3_open(filename.c_str(), &m_db); … … 46 47 if (m_errorcode!=SQLITE_OK) 47 48 cerr<<"ERROR: couldn't open database (DBhandler constructor). filename: "<< filename<<endl; 49 50 db_loaded = true; 48 51 49 52 } … … 55 58 */ 56 59 DBwrapper::DBwrapper(){ 57 60 db_loaded = false; 58 61 } 59 62 … … 64 67 DBwrapper::~DBwrapper() 65 68 { 66 sqlite3_close(m_db); 69 if (db_loaded) { 70 sqlite3_close(m_db); 71 } 67 72 } 68 73 … … 148 153 int DBwrapper::Open( std::string filename ) 149 154 { 155 db_loaded = false; 150 156 m_errorcode = sqlite3_open(filename.c_str(), &m_db); 151 157 … … 153 159 cerr<<"ERROR: couldn't open database (DBhandler constructor). filename: "<< filename<<endl; 154 160 161 db_loaded = true; 155 162 return m_errorcode; 156 163 } -
trunk/src/DBwrapper.h
r15 r159 56 56 char *m_errmsg; ///< holds the sqllite error messages 57 57 int m_errorcode; ///< holds the returned sqllite errorcode 58 bool db_loaded; 58 59 }; 59 60 -
trunk/src/configuration.cpp
r158 r159 20 20 21 21 #include <iostream> 22 #include <cstdlib> // used in the back-compatibility code 22 23 #include "configuration.h" 23 24 #include "../config.h" … … 64 65 } 65 66 } 67 66 68 conf_file.close(); 67 69 } 68 70 69 void Configuration::importOldFile()70 {71 72 }73 74 71 void Configuration::parseSettings(ifstream *file) 75 72 { 73 loadDefaultSettings(); 76 74 cerr<<"parsing settings"<<endl; 77 75 string temp_line; … … 167 165 } 168 166 } 167 168 string Configuration::get(string key) 169 { 170 map<string,string>::iterator obj; 171 172 obj = m_settings.find(key); 173 if (obj == m_settings.end()) { 174 // key is missing return empty string 175 return ""; 176 } 177 return obj->second; 178 } 179 180 Configuration* Configuration::set(string key, string value) 181 { 182 m_settings[key] = value; 183 return this; 184 } 185 186 void Configuration::loadDefaultSettings() 187 { 188 m_settings["dice-animation"] = "True"; 189 m_settings["calculate-subtotal"] = "True"; 190 m_settings["calculate-subtotal"] = "True"; 191 m_settings["horizontal-layout"] = "True"; 192 m_settings["score-hints"] = "True"; 193 m_settings["highscore-list-size"] = DEFAULT_HIGHSCORE_SIZE; 194 } 195 196 197 void Configuration::importOldFile() 198 { 199 loadDefaultSettings(); 200 if (old_db.Open(m_file)!=SQLITE_OK) { 201 return; 202 } 203 importSettings(); 204 // importHighscores is called after importSettings() so the 205 // highscore size settings will be known 206 importHighscores(); 207 } 208 209 string Configuration::getKeyFromDb(string key) 210 { 211 string tmp_query; 212 std::list<string> tmp_value; 213 214 tmp_query = "SELECT value FROM settings WHERE key = \"" + key + "\""; 215 216 tmp_value = old_db.Query(tmp_query); 217 if (tmp_value.empty()) { //if there was no result return an empty string 218 tmp_query = ""; //use tmp_query for holding a tmp_string 219 return tmp_query; 220 } 221 return *(tmp_value.begin()); 222 } 223 224 void Configuration::importSettings() 225 { 226 string temp_value; 227 228 temp_value = getKeyFromDb("animate"); 229 if (temp_value=="No") { 230 set("dice-animation","False"); 231 } 232 233 temp_value = getKeyFromDb("calculatesubtotal"); 234 if (temp_value=="No") { 235 set("calculate-subtotal","False"); 236 } 237 238 temp_value = getKeyFromDb("horizontalayout"); 239 if (temp_value=="No") { 240 set("horizontal-layout","False"); 241 } 242 243 temp_value = getKeyFromDb("score_hints"); 244 if (temp_value=="No") { 245 set("score-hints","False"); 246 } 247 248 temp_value = getKeyFromDb("highscoresize"); 249 if (temp_value=="") { 250 set("highscore-list-size",DEFAULT_HIGHSCORE_SIZE); 251 } 252 } 253 254 void Configuration::importHighscores() 255 { 256 string tmp_query = "SELECT name,date,score FROM highscore LIMIT " 257 + m_settings["highscore-list-size"]; 258 259 list<string> res = old_db.Query(tmp_query); 260 HighscoreItem temp_item; 261 262 list<string>::iterator it = res.begin(); 263 size_t pos; 264 while(it!=res.end()) { 265 temp_item.name = (*it++); 266 267 // the database saved the the hour before the date, 268 // we need to reverse this 269 pos = it->find(' '); 270 temp_item.date = it->substr(pos+1); // date 271 temp_item.date += " " + it->substr(0,pos-3); // time, without seconds 272 it++; 273 274 temp_item.score = atoi((*it++).c_str()); 275 276 m_highscores.push_back(temp_item); 277 } 278 } -
trunk/src/configuration.h
r158 r159 22 22 #define OPENYAHTZEE_CONFIGURATION_INC 23 23 24 #include "DBwrapper.h" // used in the back-compatibility code 24 25 #include <string> 25 26 #include <fstream> … … 33 34 typedef std::list<HighscoreItem> HighscoreList; 34 35 36 static const char* DEFAULT_HIGHSCORE_SIZE = "20"; 37 35 38 class Configuration { 36 39 public: … … 41 44 void load(std::string file); 42 45 46 /** 47 * Saves the configurations (setting and highscores) to the file 48 * specified when the class was constructed 49 */ 43 50 void save(); 51 52 /** 53 * \return the value associated with the specified key, or empty 54 * string if that key is missing 55 */ 56 std::string get(std::string key); 57 /** 58 * Associates the given value with the given key. You must call 59 * save() in order to make the change permanent. 60 * \return pointer to self. 61 */ 62 Configuration *set(std::string key, std::string value); 44 63 private: 45 64 /** … … 53 72 void saveSettings(std::ofstream *file); 54 73 void saveHighscores(std::ofstream *file); 74 75 /** 76 * Loads default settings 77 */ 78 void loadDefaultSettings(); 55 79 56 80 std::map<std::string, std::string> m_settings; … … 58 82 59 83 std::string m_file; 84 85 /* The following functions and variables are used for legacy SQLite 86 * configuration file, and could be dropped. 87 */ 88 DBwrapper old_db; 89 std::string getKeyFromDb(std::string key); 90 void importSettings(); 91 void importHighscores(); 60 92 }; 61 93
Note: See TracChangeset
for help on using the changeset viewer.
