TGUI  v0.6.10
ConfigFile.hpp
1 //
3 // TGUI - Texus's Graphical User Interface
4 // Copyright (C) 2012-2015 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_CONFIG_FILE_HPP
27 #define TGUI_CONFIG_FILE_HPP
28 
29 
30 #include <TGUI/Global.hpp>
31 
32 #include <fstream>
33 
35 
36 namespace tgui
37 {
40  // Reads the config files that are used to load widgets.
42  class TGUI_API ConfigFile : public sf::NonCopyable
43  {
44  public:
45 
47  // Destructor. Closes the file if still open.
49  ~ConfigFile();
50 
51 
53  // Open a given file
54  //
55  // Nothing will happen if file is already cached.
56  //
57  // return: true when the file was opened or already cached
58  // false when it couldn't be opened
60  bool open(const std::string& filename);
61 
62 
64  // Reads a specific section from the file and returns the corresponding properties and values.
65  //
66  // return: true when a section was found
67  // false when there was no section with the given name
69  bool read(const std::string& section, std::vector<std::string>& properties, std::vector<std::string>& values);
70 
71 
73  // Reads a bool from a string value.
75  bool readBool(const std::string& value, bool defaultValue) const;
76 
77 
79  // Reads a color from a string value.
81  sf::Color readColor(const std::string& value) const;
82 
83 
85  // Reads an int rectangle from a string value.
87  bool readIntRect(std::string value, sf::IntRect& rect) const;
88 
89 
91  // Loads a texture based on the contents of the string value.
93  bool readTexture(const std::string& value, const std::string& rootPath, Texture& texture) const;
94 
95 
97  // Close the file that was opened by the open function.
99  void close();
100 
101 
103  // Empty the caches and force files to be reloaded.
105  static void flushCache();
106 
107 
109  private:
110 
112  // Read the entire config file and cache its contents
114  bool readAndCache();
115 
116 
118  // Remove the whitespace from the line, starting from the character c.
120  bool removeWhitespace(const std::string& line, std::string::const_iterator& c) const;
121 
122 
124  // Reads the next word, starting from character c.
126  std::string readWord(const std::string& line, std::string::const_iterator& c) const;
127 
128 
130  // Check if line is a section header.
132  bool isSection(const std::string& line, std::string::const_iterator c, std::string& sectionName) const;
133 
134 
136  private:
137 
138  std::ifstream m_File;
139 
140  std::string m_Filename;
141 
142  static std::map<std::string, std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string>>>> m_Cache;
143  };
144 
146 
147 }
148 
150 
151 #endif // TGUI_CONFIG_FILE_HPP
152 
Namespace that contains all TGUI functions and classes.
Definition: AnimatedPicture.hpp:33
Definition: TextureManager.hpp:52
Definition: ConfigFile.hpp:42