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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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} 
Note: See TracChangeset for help on using the changeset viewer.