| 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 | #ifndef OPENYAHTZEE_CONFIGURATION_INC |
| 22 | #define OPENYAHTZEE_CONFIGURATION_INC |
| 23 | |
| 24 | #include <string> |
| 25 | #include <fstream> |
| 26 | #include <map> |
| 27 | #include <list> |
| 28 | #include <boost/range/iterator_range.hpp> |
| 29 | |
| 30 | namespace configuration { |
| 31 | |
| 32 | struct HighscoreItem; |
| 33 | |
| 34 | typedef std::list<HighscoreItem> HighscoresList; |
| 35 | |
| 36 | static const char* DEFAULT_HIGHSCORE_SIZE = "20"; |
| 37 | |
| 38 | class Configuration { |
| 39 | public: |
| 40 | Configuration(std::string file); |
| 41 | /** |
| 42 | * Reads Open Yahtzee configurations file and parses it. |
| 43 | */ |
| 44 | void load(std::string file); |
| 45 | |
| 46 | /** |
| 47 | * Saves the configurations (setting and highscores) to the file |
| 48 | * specified when the class was constructed |
| 49 | */ |
| 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, std::string default_value = ""); |
| 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); |
| 63 | |
| 64 | /** |
| 65 | * Checks if a given score quallifies to the high score list |
| 66 | */ |
| 67 | bool isHighscore(int score); |
| 68 | |
| 69 | /** |
| 70 | * Adds a given entry to the highscore list. |
| 71 | * \return the place in the highscore list for the new entry, or |
| 72 | * zero if it didn't quallify. |
| 73 | */ |
| 74 | int submitHighscore(int score, std::string name, std::string date); |
| 75 | |
| 76 | const HighscoresList* getHighscores() const; |
| 77 | |
| 78 | void clearHighscores(); |
| 79 | |
| 80 | void setHighscoresSize(size_t size); |
| 81 | private: |
| 82 | typedef std::list<std::string> lines_container; |
| 83 | typedef lines_container::iterator lines_iterator; |
| 84 | |
| 85 | lines_iterator parseSettings(boost::iterator_range<lines_iterator> range); |
| 86 | lines_iterator parseHighscores(boost::iterator_range<lines_iterator> range); |
| 87 | |
| 88 | void saveSettings(std::ofstream *file); |
| 89 | void saveHighscores(std::ofstream *file); |
| 90 | |
| 91 | /** |
| 92 | * Loads default settings |
| 93 | */ |
| 94 | void loadDefaultSettings(); |
| 95 | |
| 96 | std::map<std::string, std::string> m_settings; |
| 97 | HighscoresList m_highscores; |
| 98 | |
| 99 | std::string m_file; |
| 100 | }; |
| 101 | |
| 102 | struct HighscoreItem { |
| 103 | int score; |
| 104 | std::string name; |
| 105 | std::string date; |
| 106 | }; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | |
| 111 | #endif |