ScummVM API documentation
ini_file.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 // IniFile class defines contents of the configuration file.
25 // It serves as a INI parser and plain enumerator of all the sections and items
26 // found in file, or, oppositely, as INI file constructor.
27 // But is not much suitable for regular key/value lookup. It is suggested to
28 // create a proper map to store items, from IniFile contents.
29 //
30 //=============================================================================
31 
32 #ifndef AGS_SHARED_UTIL_INIFILE_H
33 #define AGS_SHARED_UTIL_INIFILE_H
34 
35 #include "common/std/list.h"
36 #include "common/std/utility.h"
37 #include "ags/shared/util/string.h"
38 
39 namespace AGS3 {
40 namespace AGS {
41 namespace Shared {
42 
43 class IniFile {
44 public:
45  // Position of a string in the line of text:
46  // is defined by a pair of first and next-after-last character indices
48  // Location of section in the array of text lines:
49  // is defined by a pair of first and next-after-last line indices
51 
52  inline static bool IsValidStrPos(const StrPos &pos) {
53  return pos.first < pos.second;
54  }
55 
56  // Item definition
57  // Valid key indicates a key-value line; no key means unparsed
58  // line of text, e.g. comment or incorrectly formatted item.
59  class ItemDef {
60  public:
61  ItemDef(const String &key, const String &value);
62  ItemDef(const String &line, const StrPos &key, const StrPos &value, size_t sep_at);
63  String GetLine() const {
64  return Line;
65  }
66  String GetKey() const {
67  return SubString(Line, Key);
68  }
69  String GetValue() const {
70  return SubString(Line, Value);
71  }
72  // Tells if this is a valid key/value item, which means that it has a valid key
73  bool IsKeyValue() const {
74  return IsValidStrPos(Key);
75  }
76  void SetKey(const String &key);
77  void SetValue(const String &value);
78 
79  private:
80  String Line; // actual text
81  StrPos Key; // position of item key
82  size_t SepAt; // position of the separator (assignment) symbol
83  StrPos Value; // position of item value
84  };
85  // Linked list of items
86  typedef std::list<ItemDef> LItems;
89 
90  // Section definition
91  class SectionDef {
92  public:
93  SectionDef(const String &name);
94  SectionDef(const String &line, const StrPos &name);
95  String GetLine() const {
96  return Header;
97  }
98  String GetName() const {
99  return SubString(Header, Name);
100  }
101  size_t GetItemCount() const {
102  return Items.size();
103  }
104  // Tells if this is a "global" section, which means that it has no name
105  bool IsGlobal() const {
106  return !IsValidStrPos(Name);
107  }
108  ItemIterator Begin() {
109  return Items.begin();
110  }
111  ItemIterator End() {
112  return Items.end();
113  }
114  ConstItemIterator CBegin() const {
115  return Items.begin();
116  }
117  ConstItemIterator CEnd() const {
118  return Items.end();
119  }
120  void SetName(const String &sec_name);
121  void Clear();
122  ItemIterator InsertItem(ItemIterator item, const ItemDef &itemdef);
123  void EraseItem(ItemIterator item);
124 
125  private:
126  String Header;// section's heading line
127  StrPos Name; // location of section name in the header line
128  LItems Items; // linked list of items belonging to the section
129  };
130 
131  // Linked list of sections
135 
136 private:
137  inline static String SubString(const String &line, const StrPos &pos) {
138  return line.Mid(pos.first, pos.second - pos.first);
139  }
140 
141 public:
142  IniFile();
143 
144  SectionIterator Begin() {
145  return _sections.begin();
146  }
147  SectionIterator End() {
148  return _sections.end();
149  }
150  ConstSectionIterator CBegin() const {
151  return _sections.begin();
152  }
153  ConstSectionIterator CEnd() const {
154  return _sections.end();
155  }
156 
157  void Read(Stream *in);
158  void Write(Stream *out) const;
159 
160  // Return number of sections
161  size_t GetSectionCount() const {
162  return _sections.size();
163  }
164  // Insert new item *before* existing item
165  ItemIterator InsertItem(SectionIterator sec, ItemIterator item, const String &key, const String &value);
166  // Insert new section *before* existing section
167  SectionIterator InsertSection(SectionIterator sec, const String &name);
168  // Remove a single item
169  void RemoveItem(SectionIterator sec, ItemIterator item);
170  // Completely remove whole section; this removes all lines between section
171  // header and the last item found in that section (inclusive).
172  void RemoveSection(SectionIterator sec);
173 
174 private:
175  LSections _sections;
176 };
177 
178 } // namespace Shared
179 } // namespace AGS
180 } // namespace AGS3
181 
182 #endif
Definition: achievements_tables.h:27
Definition: lobject.h:59
Definition: geometry.h:114
Definition: ini_file.h:59
Definition: string.h:62
Definition: ini_file.h:43
Definition: list_intern.h:48
Definition: list_intern.h:51
Definition: stream.h:52
Definition: ags.h:40