API Documentation
Loading...
Searching...
No Matches
INIReader.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: INIReader
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/String.h>
35#include <NDEVR/ProgressInfo.h>
36#include <NDEVR/Resource.h>
37#include <NDEVR/Scanner.h>
38#include <NDEVR/Buffer.h>
39#include <NDEVR/Dictionary.h>
40#include <NDEVR/Matrix.h>
41#include <stdio.h>
42namespace NDEVR
43{
44 /**--------------------------------------------------------------------------------------------------
45 \brief A class used with INIFactory to store a reference to an object in the program that can be
46 inherited to provide read and write functionality to the factory.
47 **/
49 {
50 public:
51 explicit INIOption();
52 virtual ~INIOption() {}
53 virtual void readOptionAscii(String option) = 0;
54 virtual void readOptionAscii(Scanner& option) = 0;
55 virtual String writeOptionAscii() const = 0;
56 virtual void writeOptionBinary(FILE* stream) const = 0;
57 virtual void readOptionBinary(const char* bytes) = 0;
58 virtual INIOption* copy() const = 0;
59 };
60 /**----------------------------------------------------------------------------
61 \brief Contains methods for easily reading and writing to an INI file including
62 efficient casting, callback, and variable storage methods.
63
64 addOption functions take a REFERENCE to an object and will modify that reference
65 when option is read in.
66 ----------------------------------------------------------------------------*/
68 {
69 public:
71 INIFactory(const INIFactory& reader) = delete;
72 INIFactory(INIFactory&& reader) noexcept;
73 virtual ~INIFactory();
74 void addINIOption(const String& label, INIOption* option);
75 template<uint01 t_dims, class t_type>
76 void addOption(const String& label, Resource<Vector<t_dims, t_type>>& mem_loc)
77 {
78 addINIOption(label, new INIObject<Vector<t_dims, t_type>>(mem_loc));
79 }
80
81 template<uint01 t_dims, class t_type>
82 void addOption(const String& label, Vector<t_dims, t_type>& mem_loc)
83 {
84 addINIOption(label, new INIObject<Vector<t_dims, t_type>>(mem_loc));
85 }
86
87 template<class t_type>
88 void addOption(const String& label, const Resource<t_type>& mem_loc) = delete;
89
90 template<class t_type>
91 void addOption(const String& label, Resource<t_type>& mem_loc)
92 {
93 addINIOption(label, new INIObject<t_type>(mem_loc));
94 }
95 template<class t_type>
96 void addOption(const String& label, const t_type& mem_loc) = delete;
97
98 template<class t_type>
99 void addOption(const String& label, t_type& mem_loc)
100 {
101 addINIOption(label, new INIObject<t_type>(mem_loc));
102 }
103 template<class t_type, uint01 t_row_dims, uint01 t_col_dims>
105 {
106 addINIOption(label, new INIObject<Matrix<t_type, t_row_dims, t_col_dims>>(mem_loc));
107 }
108 void clear();
109 bool addManagedOption(const String& option_label, const String& option, bool replace = true);
110 bool hasOption(const String& option) const;
111 bool hasOption(uint08 hash_option) const;
112 void setDelimiter(char delimiter);
113 void setComment(char comment) { m_comment = comment; };
114 void read(Scanner& file);
115 void readAsciiFile(File& file);
116 void readBinaryFile(File& file);
117 void setPreserveOrder(bool preserve_order) { m_preserve_order = preserve_order; }
118 bool preserveOrder() const { return m_preserve_order; }
119 void writeToAsciiFile(File& file, bool include_end_comment = false);
121 void writeToLog(const String& title, ProgressInfo* log, uint01 log_level = 2);
122 void setUseHashLabels(bool use_hash_labels);
124 Dictionary<String, String*>& extraOptionsRef() { return m_extra_options; };
125 String getOption(const String& option, const String& default_value = String()) const;
126 static void ConvertToINIString(String& s);
128 template<class t_type>
129 static INIOption* ToOption(t_type& mem_loc)
130 {
131 return new INIObject<t_type>(mem_loc);
132 }
133 template<class t_type>
135 {
136 return new INIObject<t_type>(value);
137 }
138 private:
139 template<class t_type>
140 class INIObject : public INIOption
141 {
142 public:
143 INIObject(Resource<t_type>& value)
144 : INIOption()
145 , m_type_pointer_shared(&value)
146 , m_type_pointer(nullptr)
147 {}
148 INIObject(t_type& value)
149 : INIOption()
150 , m_type_pointer_shared(nullptr)
151 , m_type_pointer(&value)
152 {}
153 INIObject(const INIObject& other)
154 : INIOption()
155 , m_type_pointer_shared(other.m_type_pointer_shared)
156 , m_type_pointer(other.m_type_pointer)
157 {}
158 ~INIObject() {}
159 void readOptionAscii(Scanner& scan) override
160 {
161 readOptionAscii(scan.getNext<String>());
162 }
163 void readOptionAscii(String option) override
164 {
165 INIFactory::ConvertFromINIString(option);
166 if (option.size() > 0)
167 {
168 if (m_type_pointer_shared != nullptr)
169 m_type_pointer_shared->set(option.getAs<t_type>());
170 else
171 *m_type_pointer = option.getAs<t_type>();
172 }
173 }
174 String writeOptionAscii() const override
175 {
176 String s;
177 if (m_type_pointer_shared != nullptr)
178 s = String(m_type_pointer_shared->get());
179 else
180 s = String(*m_type_pointer);
181 INIFactory::ConvertToINIString(s);
182 return s;
183 }
184 void readOptionBinary(const char* bytes) override
185 {
186 if (ObjectInfo<t_type>::Primitive)
187 {
188 t_type value;
189 memcpy(&value, bytes, sizeof(t_type));
190 if (m_type_pointer_shared != nullptr)
191 m_type_pointer_shared->set(value);
192 else
193 *m_type_pointer = value;
194 }
195 else
196 {
197 readOptionAscii(String(bytes));
198 }
199 }
200 void writeOptionBinary(FILE* stream) const override
201 {
202 if (ObjectInfo<t_type>::Primitive)
203 {
204 uint04 size = sizeof(t_type);
205 fwrite(&size, sizeof(uint04), 1, stream);
206 if (m_type_pointer_shared != nullptr)
207 fwrite(&(m_type_pointer_shared->get()), sizeof(t_type), 1, stream);
208 else
209 fwrite(m_type_pointer, sizeof(t_type), 1, stream);
210 }
211 else
212 {
213 const String value = writeOptionAscii();
214 const uint04 value_size = value.size();
215 fwrite(&value_size, sizeof(uint04), 1, stream);
216 fwrite(value.c_str(), sizeof(char), value_size, stream);
217 }
218 }
219 INIObject<t_type>* copy() const override
220 {
221 return new INIObject<t_type>(*this);
222 }
223 protected:
224 Resource<t_type>* const m_type_pointer_shared;
225 t_type* const m_type_pointer;
226 };
227 template<class t_type>
228 class INIObject<Angle<t_type>> : public INIOption
229 {
230 public:
231 INIObject(Resource<Angle<t_type>>& value)
232 : INIOption()
233 , m_type_pointer_shared(&value)
234 , m_type_pointer(nullptr)
235 {}
236 INIObject(Angle<t_type>& value)
237 : INIOption()
238 , m_type_pointer_shared(nullptr)
239 , m_type_pointer(&value)
240 {}
241 INIObject(const INIObject& other)
242 : INIOption()
243 , m_type_pointer_shared(other.m_type_pointer_shared)
244 , m_type_pointer(other.m_type_pointer)
245 {}
246 ~INIObject() {}
247 void readOptionAscii(Scanner& scan) override
248 {
249 readOptionAscii(scan.getNext<String>());
250 }
251 void readOptionAscii(String option) override
252 {
253 INIFactory::ConvertFromINIString(option);
254 if (option.size() > 0)
255 {
256 if (m_type_pointer_shared != nullptr)
257 m_type_pointer_shared->set(Angle<t_type>(DEGREES, option.getAs<fltp08>()));
258 else
259 *m_type_pointer = Angle<t_type>(DEGREES, option.getAs<fltp08>());
260 }
261 }
262 String writeOptionAscii() const override
263 {
264 String s;
265 if (m_type_pointer_shared != nullptr)
266 s = String(m_type_pointer_shared->get().template as<DEGREES>());
267 else
268 s = String(m_type_pointer->template as<DEGREES>());
269 INIFactory::ConvertToINIString(s);
270 return s;
271 }
272 void readOptionBinary(const char* bytes) override
273 {
274 Angle<t_type> value;
275 memcpy(&value, bytes, sizeof(Angle<t_type>));
276 if (m_type_pointer_shared != nullptr)
277 m_type_pointer_shared->set(value);
278 else
279 *m_type_pointer = value;
280 }
281 void writeOptionBinary(FILE* stream) const override
282 {
283 uint04 size = sizeof(Angle<t_type>);
284 fwrite(&size, sizeof(uint04), 1, stream);
285 if (m_type_pointer_shared != nullptr)
286 fwrite(&(m_type_pointer_shared->get()), sizeof(Angle<t_type>), 1, stream);
287 else
288 fwrite(m_type_pointer, sizeof(Angle<t_type>), 1, stream);
289 }
290 INIObject<Angle<t_type>>* copy() const override
291 {
292 return new INIObject<Angle<t_type>>(*this);
293 }
294 protected:
295 Resource<Angle<t_type>>* const m_type_pointer_shared;
296 Angle<t_type>* const m_type_pointer;
297 };
298 private:
299 Dictionary<String, INIOption*> m_options;
300 Dictionary<uint08, INIOption*> m_hashed_options;
301 Dictionary<String, String*> m_extra_options;
302 Buffer<String> m_ordered_options;
303 char m_delimiter;
304 char m_comment;
305 bool m_preserve_order = false;
306 bool m_use_hash_labels = false;
307 };
308
309}
#define NDEVR_BASE_API
Definition DLLInfo.h:57
A hash-based key-value store, useful for quick associative lookups. Key features include:
Definition Dictionary.h:61
Logic for reading or writing to a file as well as navigating filesystems.
Definition File.h:48
Contains methods for easily reading and writing to an INI file including efficient casting,...
Definition INIReader.h:68
void addOption(const String &label, Resource< Matrix< t_type, t_row_dims, t_col_dims > > &mem_loc)
Definition INIReader.h:104
void addOption(const String &label, const t_type &mem_loc)=delete
virtual ~INIFactory()
Dictionary< String, String > extraOptions() const
void addOption(const String &label, Resource< Vector< t_dims, t_type > > &mem_loc)
Definition INIReader.h:76
bool preserveOrder() const
Definition INIReader.h:118
void writeToAsciiFile(File &file, bool include_end_comment=false)
bool addManagedOption(const String &option_label, const String &option, bool replace=true)
void setComment(char comment)
Definition INIReader.h:113
bool hasOption(uint08 hash_option) const
static INIOption * ToOption(t_type &mem_loc)
Definition INIReader.h:129
bool hasOption(const String &option) const
void writeToLog(const String &title, ProgressInfo *log, uint01 log_level=2)
void addOption(const String &label, const Resource< t_type > &mem_loc)=delete
void addINIOption(const String &label, INIOption *option)
void readBinaryFile(File &file)
INIFactory(const INIFactory &reader)=delete
void setDelimiter(char delimiter)
static INIOption * ToOption(Resource< t_type > &value)
Definition INIReader.h:134
Dictionary< String, String * > & extraOptionsRef()
Definition INIReader.h:124
void addOption(const String &label, t_type &mem_loc)
Definition INIReader.h:99
void addOption(const String &label, Vector< t_dims, t_type > &mem_loc)
Definition INIReader.h:82
INIFactory(INIFactory &&reader) noexcept
void read(Scanner &file)
void writeToBinaryFile(File &file)
void addOption(const String &label, Resource< t_type > &mem_loc)
Definition INIReader.h:91
void readAsciiFile(File &file)
void setPreserveOrder(bool preserve_order)
Definition INIReader.h:117
static void ConvertToINIString(String &s)
String getOption(const String &option, const String &default_value=String()) const
void setUseHashLabels(bool use_hash_labels)
static void ConvertFromINIString(String &s)
A class used with INIFactory to store a reference to an object in the program that can be inherited t...
Definition INIReader.h:49
virtual String writeOptionAscii() const =0
virtual void readOptionAscii(String option)=0
virtual void readOptionAscii(Scanner &option)=0
virtual INIOption * copy() const =0
virtual void readOptionBinary(const char *bytes)=0
virtual void writeOptionBinary(FILE *stream) const =0
virtual ~INIOption()
Definition INIReader.h:52
Definition Matrix.hpp:176
A light-weight base class for Log that allows processes to update, without the need for additional in...
Definition ProgressInfo.hpp:48
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Toggle.h:41
Contains methods for easily reading objects in an ascii stream.
Definition Scanner.h:45
The core String class for the NDEVR API.
Definition String.h:69
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
Definition ACIColor.h:37
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
Definition BaseValues.hpp:106
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96