Changeset 159


Ignore:
Timestamp:
09/26/2008 11:32:18 AM (4 years ago)
Author:
guyru
Message:
  • Import settings and highscores to new configuration class.
  • Fix segementation fault caused by trying to close an unopened db in DBwrapper destructor.
Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/DBwrapper.cpp

    r73 r159  
    4141{ 
    4242        m_errmsg=0; //set the pointer to null 
     43        db_loaded = false; 
    4344         
    4445        m_errorcode = sqlite3_open(filename.c_str(), &m_db); 
     
    4647        if (m_errorcode!=SQLITE_OK) 
    4748                cerr<<"ERROR: couldn't open database (DBhandler constructor). filename: "<< filename<<endl; 
     49         
     50        db_loaded = true; 
    4851 
    4952} 
     
    5558*/ 
    5659DBwrapper::DBwrapper(){ 
    57  
     60        db_loaded = false; 
    5861} 
    5962 
     
    6467DBwrapper::~DBwrapper() 
    6568{ 
    66         sqlite3_close(m_db); 
     69        if (db_loaded) { 
     70                sqlite3_close(m_db); 
     71        } 
    6772} 
    6873 
     
    148153int DBwrapper::Open( std::string filename ) 
    149154{ 
     155        db_loaded = false; 
    150156        m_errorcode = sqlite3_open(filename.c_str(), &m_db); 
    151157         
     
    153159                cerr<<"ERROR: couldn't open database (DBhandler constructor). filename: "<< filename<<endl; 
    154160         
     161        db_loaded = true; 
    155162        return m_errorcode; 
    156163} 
  • trunk/src/DBwrapper.h

    r15 r159  
    5656        char *m_errmsg; ///< holds the sqllite error messages 
    5757        int m_errorcode; ///< holds the returned sqllite errorcode 
     58        bool db_loaded; 
    5859}; 
    5960 
  • trunk/src/configuration.cpp

    r158 r159  
    2020 
    2121#include <iostream> 
     22#include <cstdlib> // used in the back-compatibility code 
    2223#include "configuration.h" 
    2324#include "../config.h" 
     
    6465                } 
    6566        } 
     67 
    6668        conf_file.close(); 
    6769} 
    6870 
    69 void Configuration::importOldFile() 
    70 { 
    71  
    72 } 
    73  
    7471void Configuration::parseSettings(ifstream *file) 
    7572{ 
     73        loadDefaultSettings(); 
    7674        cerr<<"parsing settings"<<endl; 
    7775        string temp_line; 
     
    167165        } 
    168166} 
     167 
     168string 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 
     180Configuration* Configuration::set(string key, string value) 
     181{ 
     182        m_settings[key] = value; 
     183        return this; 
     184} 
     185 
     186void 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 
     197void 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 
     209string 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 
     224void 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 
     254void 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  
    2222#define OPENYAHTZEE_CONFIGURATION_INC 
    2323 
     24#include "DBwrapper.h" // used in the back-compatibility code 
    2425#include <string> 
    2526#include <fstream> 
     
    3334typedef std::list<HighscoreItem> HighscoreList; 
    3435 
     36static const char* DEFAULT_HIGHSCORE_SIZE = "20"; 
     37 
    3538class Configuration { 
    3639public: 
     
    4144        void load(std::string file); 
    4245 
     46        /** 
     47         * Saves the configurations (setting and highscores) to the file 
     48         * specified when the class was constructed 
     49         */ 
    4350        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); 
    4463private: 
    4564        /** 
     
    5372        void saveSettings(std::ofstream *file); 
    5473        void saveHighscores(std::ofstream *file); 
     74 
     75        /** 
     76         * Loads default settings 
     77         */ 
     78        void loadDefaultSettings(); 
    5579         
    5680        std::map<std::string, std::string> m_settings; 
     
    5882 
    5983        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(); 
    6092}; 
    6193 
Note: See TracChangeset for help on using the changeset viewer.