ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ini_util.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Functions for exchanging configuration data between key-value tree and
25 // INI file.
26 //
27 //=============================================================================
28 
29 #ifndef AGS_SHARED_UTIL_INI_UTIL_H
30 #define AGS_SHARED_UTIL_INI_UTIL_H
31 
32 #include "common/std/map.h"
33 #include "ags/shared/util/string.h"
34 
35 namespace AGS3 {
36 namespace AGS {
37 namespace Shared {
38 
39 typedef std::map<String, String> StringOrderMap;
40 typedef std::map<String, StringOrderMap> ConfigTree;
41 
42 //
43 // Helper functions for parsing values in a ConfigTree
44 bool CfgReadItem(const ConfigTree &cfg, const String &sectn, const String &item, String &value);
45 int CfgReadInt(const ConfigTree &cfg, const String &sectn, const String &item, int def = 0);
46 int CfgReadInt(const ConfigTree &cfg, const String &sectn, const String &item, int min, int max, int def = 0);
47 inline bool CfgReadBoolInt(const ConfigTree &cfg, const String &sectn, const String &item, bool def = false) {
48  return CfgReadInt(cfg, sectn, item, 0, 1, def) != 0;
49 }
50 float CfgReadFloat(const ConfigTree &cfg, const String &sectn, const String &item, float def = 0.f);
51 float CfgReadFloat(const ConfigTree &cfg, const String &sectn, const String &item, float min, float max, float def = 0.f);
52 String CfgReadString(const ConfigTree &cfg, const String &sectn, const String &item, const String &def = "");
53 // Looks up for a item key in a given section, returns actual key if one exists, or empty string otherwise,
54 // optionally compares item name in case-insensitive way.
55 // NOTE: this is a compatibility hack, in case we cannot enforce key case-sensitivity in some case.
56 String CfgFindKey(const ConfigTree &cfg, const String &sectn, const String &item, bool nocase = false);
57 
58 //
59 // Helper functions for writing values into a ConfigTree
60 void CfgWriteInt(ConfigTree &cfg, const String &sectn, const String &item, int value);
61 inline void CfgWriteBoolInt(ConfigTree &cfg, const String &sectn, const String &item, bool value) {
62  CfgWriteInt(cfg, sectn, item, static_cast<int>(value));
63 }
64 void CfgWriteFloat(ConfigTree &cfg, const String &sectn, const String &item, float value);
65 void CfgWriteFloat(ConfigTree &cfg, const String &sectn, const String &item, float value, unsigned precision);
66 void CfgWriteString(ConfigTree &cfg, const String &sectn, const String &item, const String &value);
67 
68 
69 class IniFile;
70 
71 // Utility functions that exchange data between ConfigTree and INI file.
72 namespace IniUtil {
73 
74 // Parse the contents of given file as INI format and insert values
75 // into the tree. The pre-existing tree items, if any, are NOT erased.
76 // Returns FALSE if the file could not be opened.
77 bool Read(const String &file, ConfigTree &tree);
78 // Serialize given tree to the stream in INI text format.
79 // The INI format suggests only one nested level (group - items).
80 // The first level values are treated as a global section items.
81 // The sub-nodes beyond 2nd level are ignored completely.
82 void Write(const String &file, const ConfigTree &tree);
83 // Serialize given tree to the string in INI text format.
84 // TODO: implement proper memory/string stream compatible with base Stream
85 // class and merge this with Write function.
86 void WriteToString(String &s, const ConfigTree &tree);
87 // Parse the contents of given source stream as INI format and merge
88 // with values of the given tree while doing only minimal replaces;
89 // write the result into destination stream.
90 // If item already exists, only value is overwrited, if section exists,
91 // new items are appended to the end of it; completely new sections are
92 // appended to the end of text.
93 // Source and destination streams may refer either to different objects,
94 // or same stream opened for both reading and writing.
95 // Returns FALSE if the file could not be opened for writing.
96 bool Merge(const String &file, const ConfigTree &tree);
97 
98 } // namespace IniUtil
99 
100 } // namespace Shared
101 } // namespace AGS
102 } // namespace AGS3
103 
104 #endif
Definition: achievements_tables.h:27
Definition: map.h:40
Definition: ags.h:40