| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006-2012 by Guy Rutenberg * |
| 3 | * [email protected] * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include <cstdlib> // used in the back-compatibility code |
| 22 | #include <sstream> |
| 23 | #include <iostream> |
| 24 | #include <boost/algorithm/string.hpp> |
| 25 | #include <boost/format.hpp> |
| 26 | #include "configuration.h" |
| 27 | #include "../config.h" |
| 28 | |
| 29 | using namespace std; |
| 30 | using namespace configuration; |
| 31 | using namespace boost; |
| 32 | |
| 33 | |
| 34 | Configuration::Configuration(string file) |
| 35 | { |
| 36 | load(file); |
| 37 | } |
| 38 | |
| 39 | void Configuration::load(string file) |
| 40 | { |
| 41 | m_file = file; |
| 42 | |
| 43 | ifstream conf_file(file.c_str()); |
| 44 | if (!conf_file.is_open()) { |
| 45 | // file couldn't be opened, this is due to missing file or |
| 46 | // permission error. |
| 47 | loadDefaultSettings(); |
| 48 | save(); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | lines_container conf_lines; |
| 53 | string line; |
| 54 | while(getline(conf_file, line).good()) { |
| 55 | conf_lines.push_back(line); |
| 56 | } |
| 57 | |
| 58 | if (conf_file.is_open()) |
| 59 | conf_file.close(); |
| 60 | |
| 61 | // check it's an Open Yahtzee configuration file |
| 62 | if (conf_lines.begin()->substr(0,11) != "openyahtzee") { |
| 63 | /* The file might be an old configuration file or |
| 64 | * corrupted, anyway re-create it |
| 65 | */ |
| 66 | loadDefaultSettings(); |
| 67 | save(); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | lines_iterator conf_line = conf_lines.begin(); |
| 72 | while (conf_line != conf_lines.end()) { |
| 73 | if (*conf_line == "[settings]") { |
| 74 | conf_line = parseSettings(make_iterator_range(conf_line, conf_lines.end())); |
| 75 | } else if (*conf_line == "[highscores]") { |
| 76 | conf_line = |
| 77 | parseHighscores(make_iterator_range(conf_line, conf_lines.end())); |
| 78 | } else { |
| 79 | ++conf_line; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Configuration::lines_iterator Configuration::parseSettings(iterator_range<Configuration::lines_iterator> range) |
| 85 | { |
| 86 | loadDefaultSettings(); |
| 87 | auto start = range.begin(); |
| 88 | while (++start != range.end()) { |
| 89 | // check if we reached the start of a new section |
| 90 | if (algorithm::starts_with(*start, "[") && |
| 91 | algorithm::ends_with(*start, "]")) |
| 92 | break; |
| 93 | size_t pos = start->find('='); |
| 94 | if (pos == std::string::npos) |
| 95 | continue; |
| 96 | |
| 97 | m_settings[start->substr(0,pos)] = |
| 98 | start->substr(pos + 1); |
| 99 | } |
| 100 | return start; |
| 101 | } |
| 102 | |
| 103 | Configuration::lines_iterator Configuration::parseHighscores(iterator_range<Configuration::lines_iterator> range) |
| 104 | { |
| 105 | |
| 106 | HighscoreItem temp_item; |
| 107 | auto start = range.begin(); |
| 108 | |
| 109 | while (++start != range.end()) { |
| 110 | // check if we reached the start of a new section |
| 111 | if (algorithm::starts_with(*start, "[") && |
| 112 | algorithm::ends_with(*start, "]")) |
| 113 | break; |
| 114 | vector<string> line_parts; |
| 115 | boost::split(line_parts, *start, boost::is_any_of(" ")); |
| 116 | if (line_parts.size() < 4) { |
| 117 | // The line has missing parts, ignore it |
| 118 | cerr << boost::format("%s:%s Couldn't parse: %s") % __FILE__ % __LINE__ % *start << endl; |
| 119 | continue; |
| 120 | } |
| 121 | auto line_parts_itr = line_parts.begin(); |
| 122 | temp_item.score = atoi((line_parts_itr++)->c_str()); |
| 123 | temp_item.date = *line_parts_itr + " " + *(line_parts_itr+1); |
| 124 | line_parts_itr += 2; |
| 125 | temp_item.name = algorithm::join(make_iterator_range(line_parts_itr, |
| 126 | line_parts.end()), |
| 127 | " "); |
| 128 | |
| 129 | m_highscores.push_back(temp_item); |
| 130 | } |
| 131 | |
| 132 | return start; |
| 133 | } |
| 134 | |
| 135 | void Configuration::save() |
| 136 | { |
| 137 | ofstream conf_file (m_file.c_str()); |
| 138 | if (!conf_file.is_open()) { |
| 139 | // file couldn't be opened, probably due to permission error |
| 140 | return; |
| 141 | } |
| 142 | conf_file<<"openyahtzee="<<VERSION<<endl; |
| 143 | |
| 144 | conf_file<<"[settings]\n"; |
| 145 | saveSettings(&conf_file); |
| 146 | conf_file<<"[highscores]\n"; |
| 147 | saveHighscores(&conf_file); |
| 148 | |
| 149 | conf_file.close(); |
| 150 | } |
| 151 | |
| 152 | void Configuration::saveSettings(ofstream *file) |
| 153 | { |
| 154 | for (auto &it : m_settings) { |
| 155 | (*file) << it.first << "=" << it.second << endl; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void Configuration::saveHighscores(ofstream *file) |
| 160 | { |
| 161 | for (auto &it : m_highscores) { |
| 162 | (*file) << it.score << " "; |
| 163 | (*file) << it.date << " "; |
| 164 | (*file) << it.name << endl; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | string Configuration::get(string key, string default_value) |
| 169 | { |
| 170 | auto obj = m_settings.find(key); |
| 171 | |
| 172 | if (obj == m_settings.end()) { |
| 173 | // key is missing return empty string |
| 174 | return default_value; |
| 175 | } |
| 176 | return obj->second; |
| 177 | } |
| 178 | |
| 179 | Configuration* Configuration::set(string key, string value) |
| 180 | { |
| 181 | m_settings[key] = value; |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | void Configuration::loadDefaultSettings() |
| 186 | { |
| 187 | m_settings["dice-animation"] = "True"; |
| 188 | m_settings["calculate-subtotal"] = "True"; |
| 189 | m_settings["horizontal-layout"] = "True"; |
| 190 | m_settings["score-hints"] = "True"; |
| 191 | m_settings["highscore-list-size"] = DEFAULT_HIGHSCORE_SIZE; |
| 192 | } |
| 193 | |
| 194 | bool Configuration::isHighscore(int score) { |
| 195 | const unsigned int highscore_list_size = atoi(m_settings["highscore-list-size"].c_str()); |
| 196 | if (m_highscores.size() < highscore_list_size) { |
| 197 | // we have extra room in the highscore list |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | auto it= m_highscores.end(); |
| 202 | if ((--it)->score < score) { |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | int Configuration::submitHighscore(int score, string name, string date) { |
| 210 | const unsigned int highscore_list_size = atoi(m_settings["highscore-list-size"].c_str()); |
| 211 | unsigned int place = 1; |
| 212 | HighscoreItem temp_item; |
| 213 | |
| 214 | //create sentinel in end of list, we'll remove when we finish |
| 215 | temp_item.score = -1; // this is lower than any valid score |
| 216 | m_highscores.push_back(temp_item); |
| 217 | |
| 218 | temp_item.score = score; |
| 219 | temp_item.date = date; |
| 220 | temp_item.name = name; |
| 221 | |
| 222 | HighscoresList::iterator it = m_highscores.begin(); |
| 223 | while (it!=m_highscores.end()) { |
| 224 | if (it->score < temp_item.score) { |
| 225 | m_highscores.insert(it, temp_item); |
| 226 | break; |
| 227 | } |
| 228 | place++; |
| 229 | it++; |
| 230 | } |
| 231 | m_highscores.pop_back(); // remove the sentinel; |
| 232 | |
| 233 | if (m_highscores.size()>highscore_list_size) { |
| 234 | m_highscores.pop_back(); |
| 235 | } |
| 236 | |
| 237 | if (place>highscore_list_size) { |
| 238 | return 0; |
| 239 | } |
| 240 | save(); |
| 241 | return place; |
| 242 | } |
| 243 | |
| 244 | const HighscoresList* Configuration::getHighscores() const |
| 245 | { |
| 246 | const HighscoresList * ptr = &m_highscores; |
| 247 | return ptr; |
| 248 | } |
| 249 | |
| 250 | void Configuration::clearHighscores() |
| 251 | { |
| 252 | m_highscores.clear(); |
| 253 | save(); |
| 254 | } |
| 255 | |
| 256 | void Configuration::setHighscoresSize(size_t size) |
| 257 | { |
| 258 | |
| 259 | std::ostringstream o; |
| 260 | o << size; |
| 261 | m_settings["highscore-list-size"] = o.str(); |
| 262 | |
| 263 | while (m_highscores.size()>size) { |
| 264 | m_highscores.pop_back(); |
| 265 | } |
| 266 | |
| 267 | save(); |
| 268 | } |