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{
45 {
46 public:
47 explicit INIOption();
48 virtual ~INIOption() {}
49 virtual void readOptionAscii(String option) = 0;
50 virtual void readOptionAscii(Scanner& option) = 0;
51 virtual String writeOptionAscii() const = 0;
52 virtual void writeOptionBinary(FILE* stream) const = 0;
53 virtual void readOptionBinary(const char* bytes) = 0;
54 virtual INIOption* copy() const = 0;
55 };
57 {
58 private:
59 template<class t_type>
60 class INIObject : public INIOption
61 {
62 public:
63 INIObject(Resource<t_type>& value)
64 : INIOption()
65 , m_type_pointer_shared(&value)
66 , m_type_pointer(nullptr)
67 {}
68 INIObject(t_type& value)
69 : INIOption()
70 , m_type_pointer_shared(nullptr)
71 , m_type_pointer(&value)
72 {}
73 INIObject(const INIObject& other)
74 : INIOption()
75 , m_type_pointer_shared(other.m_type_pointer_shared)
76 , m_type_pointer(other.m_type_pointer)
77 {}
78 ~INIObject() {}
79 void readOptionAscii(Scanner& scan) override
80 {
81 readOptionAscii(scan.getNext<String>());
82 }
83 void readOptionAscii(String option) override
84 {
85 INIFactory::ConvertFromINIString(option);
86 if (option.size() > 0)
87 {
88 if (m_type_pointer_shared != nullptr)
89 m_type_pointer_shared->set(option.getAs<t_type>());
90 else
91 *m_type_pointer = option.getAs<t_type>();
92 }
93 }
94 String writeOptionAscii() const override
95 {
96 String s;
97 if (m_type_pointer_shared != nullptr)
98 s = String(m_type_pointer_shared->get());
99 else
100 s = String(*m_type_pointer);
101 INIFactory::ConvertToINIString(s);
102 return s;
103 }
104 void readOptionBinary(const char* bytes) override
105 {
107 {
108 t_type value;
109 memcpy(&value, bytes, sizeof(t_type));
110 if (m_type_pointer_shared != nullptr)
111 m_type_pointer_shared->set(value);
112 else
113 *m_type_pointer = value;
114 }
115 else
116 {
117 readOptionAscii(String(bytes));
118 }
119 }
120 void writeOptionBinary(FILE* stream) const override
121 {
123 {
124 uint04 size = sizeof(t_type);
125 fwrite(&size, sizeof(uint04), 1, stream);
126 if (m_type_pointer_shared != nullptr)
127 fwrite(&(m_type_pointer_shared->get()), sizeof(t_type), 1, stream);
128 else
129 fwrite(m_type_pointer, sizeof(t_type), 1, stream);
130 }
131 else
132 {
133 const String value = writeOptionAscii();
134 const uint04 value_size = value.size();
135 fwrite(&value_size, sizeof(uint04), 1, stream);
136 fwrite(value.c_str(), sizeof(char), value_size, stream);
137 }
138 }
139 INIObject<t_type>* copy() const override
140 {
141 return new INIObject<t_type>(*this);
142 }
143 protected:
144 Resource<t_type>* const m_type_pointer_shared;
145 t_type* const m_type_pointer;
146 };
147 template<class t_type>
148 class INIObject<Angle<t_type>> : public INIOption
149 {
150 public:
151 INIObject(Resource<Angle<t_type>>& value)
152 : INIOption()
153 , m_type_pointer_shared(&value)
154 , m_type_pointer(nullptr)
155 {}
156 INIObject(Angle<t_type>& value)
157 : INIOption()
158 , m_type_pointer_shared(nullptr)
159 , m_type_pointer(&value)
160 {}
161 INIObject(const INIObject& other)
162 : INIOption()
163 , m_type_pointer_shared(other.m_type_pointer_shared)
164 , m_type_pointer(other.m_type_pointer)
165 {}
166 ~INIObject() {}
167 void readOptionAscii(Scanner& scan) override
168 {
169 readOptionAscii(scan.getNext<String>());
170 }
171 void readOptionAscii(String option) override
172 {
173 INIFactory::ConvertFromINIString(option);
174 if (option.size() > 0)
175 {
176 if (m_type_pointer_shared != nullptr)
177 m_type_pointer_shared->set(Angle<t_type>(DEGREES, option.getAs<fltp08>()));
178 else
179 *m_type_pointer = Angle<t_type>(DEGREES, option.getAs<fltp08>());
180 }
181 }
182 String writeOptionAscii() const override
183 {
184 String s;
185 if (m_type_pointer_shared != nullptr)
186 s = String(m_type_pointer_shared->get().template as<DEGREES>());
187 else
188 s = String(m_type_pointer->template as<DEGREES>());
189 INIFactory::ConvertToINIString(s);
190 return s;
191 }
192 void readOptionBinary(const char* bytes) override
193 {
194 Angle<t_type> value;
195 memcpy(&value, bytes, sizeof(Angle<t_type>));
196 if (m_type_pointer_shared != nullptr)
197 m_type_pointer_shared->set(value);
198 else
199 *m_type_pointer = value;
200 }
201 void writeOptionBinary(FILE* stream) const override
202 {
203 uint04 size = sizeof(Angle<t_type>);
204 fwrite(&size, sizeof(uint04), 1, stream);
205 if (m_type_pointer_shared != nullptr)
206 fwrite(&(m_type_pointer_shared->get()), sizeof(Angle<t_type>), 1, stream);
207 else
208 fwrite(m_type_pointer, sizeof(Angle<t_type>), 1, stream);
209 }
210 INIObject<Angle<t_type>>* copy() const override
211 {
212 return new INIObject<Angle<t_type>>(*this);
213 }
214 protected:
215 Resource<Angle<t_type>>* const m_type_pointer_shared;
216 Angle<t_type>* const m_type_pointer;
217 };
218 public:
219 INIFactory();
220 INIFactory(const INIFactory& reader) = delete;
221 INIFactory(INIFactory&& reader) noexcept;
222 virtual ~INIFactory();
223
224 template<class t_type>
225 static INIOption* ToOption(t_type& mem_loc)
226 {
227 return new INIObject<t_type>(mem_loc);
228 }
229 template<class t_type>
231 {
232 return new INIObject<t_type>(value);
233 }
234 void addINIOption(const String& label, INIOption* option);
235 template<uint01 t_dims, class t_type>
236 void addOption(const String& label, Resource<Vector<t_dims, t_type>>& mem_loc)
237 {
238 addINIOption(label, new INIObject<Vector<t_dims, t_type>>(mem_loc));
239 }
240
241 template<uint01 t_dims, class t_type>
242 void addOption(const String& label, Vector<t_dims, t_type>& mem_loc)
243 {
244 addINIOption(label, new INIObject<Vector<t_dims, t_type>>(mem_loc));
245 }
246
247 template<class t_type>
248 void addOption(const String& label, const Resource<t_type>& mem_loc) = delete;
249
250 template<class t_type>
251 void addOption(const String& label, Resource<t_type>& mem_loc)
252 {
253 addINIOption(label, new INIObject<t_type>(mem_loc));
254 }
255 template<class t_type>
256 void addOption(const String& label, const t_type& mem_loc) = delete;
257
258 template<class t_type>
259 void addOption(const String& label, t_type& mem_loc)
260 {
261 addINIOption(label, new INIObject<t_type>(mem_loc));
262 }
263 template<class t_type, uint01 t_row_dims, uint01 t_col_dims>
265 {
266 addINIOption(label, new INIObject<Matrix<t_type, t_row_dims, t_col_dims>>(mem_loc));
267 }
268 void clear();
269 bool addManagedOption(const String& option_label, const String& option, bool replace = true);
270 bool hasOption(const String& option) const;
271 bool hasOption(uint08 hash_option) const;
272 void setDelimiter(char delimiter);
273 void setComment(char comment) { m_comment = comment; };
274 void read(Scanner& file);
275 void readAsciiFile(File& file);
276 void readBinaryFile(File& file);
277 void setPreserveOrder(bool preserve_order) { m_preserve_order = preserve_order; }
278 bool preserveOrder() const { return m_preserve_order; }
279 void writeToAsciiFile(File& file, bool include_end_comment = false);
280 void writeToBinaryFile(File& file);
281 void writeToLog(const String& title, ProgressInfo* log, uint01 log_level = 2);
282 void setUseHashLabels(bool use_hash_labels);
283 Dictionary<String, String> extraOptions() const;
284 Dictionary<String, String*>& extraOptionsRef() { return m_extra_options; };
285 String getOption(const String& option, const String& default_value = String()) const;
286 static void ConvertToINIString(String& s);
287 static void ConvertFromINIString(String& s);
288 private:
290 Dictionary<uint08, INIOption*> m_hashed_options;
291 Dictionary<String, String*> m_extra_options;
292 Buffer<String> m_ordered_options;
293 char m_delimiter;
294 char m_comment;
295 bool m_preserve_order = false;
296 bool m_use_hash_labels = false;
297 };
298
299}
#define NDEVR_BASE_API
Definition DLLInfo.h:78
Stores an angle in an optimized format.
Definition StringStream.h:352
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
constexpr t_index_type size() const
Definition Buffer.hpp:1461
Definition Dictionary.h:48
Definition File.h:47
Definition INIReader.h:57
void addOption(const String &label, Resource< Matrix< t_type, t_row_dims, t_col_dims > > &mem_loc)
Definition INIReader.h:264
void addOption(const String &label, const t_type &mem_loc)=delete
void addOption(const String &label, Resource< Vector< t_dims, t_type > > &mem_loc)
Definition INIReader.h:236
bool preserveOrder() const
Definition INIReader.h:278
void setComment(char comment)
Definition INIReader.h:273
static INIOption * ToOption(t_type &mem_loc)
Definition INIReader.h:225
void addOption(const String &label, const Resource< t_type > &mem_loc)=delete
INIFactory(const INIFactory &reader)=delete
static INIOption * ToOption(Resource< t_type > &value)
Definition INIReader.h:230
Dictionary< String, String * > & extraOptionsRef()
Definition INIReader.h:284
void addOption(const String &label, t_type &mem_loc)
Definition INIReader.h:259
void addOption(const String &label, Vector< t_dims, t_type > &mem_loc)
Definition INIReader.h:242
void addOption(const String &label, Resource< t_type > &mem_loc)
Definition INIReader.h:251
void setPreserveOrder(bool preserve_order)
Definition INIReader.h:277
Definition INIReader.h:45
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:48
Definition Matrix.hpp:173
Definition ProgressInfo.hpp:43
Definition Toggle.h:41
Definition Scanner.h:41
t_type getNext()
Definition String.h:40
NDEVR_BASE_API const char * c_str() const
Definition String.cpp:490
t_type getAs() const
Definition String.h:334
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
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:98
@ DEGREES
Definition Angle.h:66
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer -Can represent exact integer values 0 thro...
Definition BaseValues.hpp:132
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
Information about the object.
Definition ObjectInfo.h:56