| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (C) 2006-2008 by Guy Rutenberg * |
|---|
| 3 | * guyrutenberg@gmail.com * |
|---|
| 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 | |
|---|
| 29 | namespace configuration { |
|---|
| 30 | |
|---|
| 31 | struct HighscoreItem; |
|---|
| 32 | |
|---|
| 33 | typedef std::list<HighscoreItem> HighscoresList; |
|---|
| 34 | |
|---|
| 35 | static const char* DEFAULT_HIGHSCORE_SIZE = "20"; |
|---|
| 36 | |
|---|
| 37 | class Configuration { |
|---|
| 38 | public: |
|---|
| 39 | Configuration(std::string file); |
|---|
| 40 | /** |
|---|
| 41 | * Reads Open Yahtzee configurations file and parses it. |
|---|
| 42 | */ |
|---|
| 43 | void load(std::string file); |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Saves the configurations (setting and highscores) to the file |
|---|
| 47 | * specified when the class was constructed |
|---|
| 48 | */ |
|---|
| 49 | void save(); |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * \return the value associated with the specified key, or empty |
|---|
| 53 | * string if that key is missing |
|---|
| 54 | */ |
|---|
| 55 | std::string get(std::string key); |
|---|
| 56 | /** |
|---|
| 57 | * Associates the given value with the given key. You must call |
|---|
| 58 | * save() in order to make the change permanent. |
|---|
| 59 | * \return pointer to self. |
|---|
| 60 | */ |
|---|
| 61 | Configuration *set(std::string key, std::string value); |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Checks if a given score quallifies to the high score list |
|---|
| 65 | */ |
|---|
| 66 | bool isHighscore(int score); |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Adds a given entry to the highscore list. |
|---|
| 70 | * \return the place in the highscore list for the new entry, or |
|---|
| 71 | * zero if it didn't quallify. |
|---|
| 72 | */ |
|---|
| 73 | int submitHighscore(int score, std::string name, std::string date); |
|---|
| 74 | |
|---|
| 75 | const HighscoresList* getHighscores() const; |
|---|
| 76 | |
|---|
| 77 | void clearHighscores(); |
|---|
| 78 | |
|---|
| 79 | void setHighscoresSize(size_t size); |
|---|
| 80 | private: |
|---|
| 81 | void parseSettings(std::ifstream *file); |
|---|
| 82 | void parseHighscores(std::ifstream *file); |
|---|
| 83 | |
|---|
| 84 | void saveSettings(std::ofstream *file); |
|---|
| 85 | void saveHighscores(std::ofstream *file); |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Loads default settings |
|---|
| 89 | */ |
|---|
| 90 | void loadDefaultSettings(); |
|---|
| 91 | |
|---|
| 92 | std::map<std::string, std::string> m_settings; |
|---|
| 93 | HighscoresList m_highscores; |
|---|
| 94 | |
|---|
| 95 | std::string m_file; |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | struct HighscoreItem { |
|---|
| 99 | int score; |
|---|
| 100 | std::string name; |
|---|
| 101 | std::string date; |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | #endif |
|---|