Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
IniProfile.h
Go to the documentation of this file.
1#pragma once
2#include <fstream>
3#include <map>
4#include <misc/gen/TStrings.h>
5#include <misc/gen/TVector.h>
6#include <misc/global.h>
7#include <string>
8#include <unordered_map>
9
10namespace sf
11{
12
17{
18 protected:
27
28 public:
33
37 static constexpr size_t npos = EntryVector::npos;
38
43
48
53 explicit IniProfile(const std::string& path);
54
59 IniProfile(const IniProfile&) = delete;
60
65 IniProfile(const IniProfile&&) = delete;
66
72
78
83 IniProfile(const std::string& section, const std::string& path);
84
89 explicit IniProfile(const std::string_view& data);
90
95 explicit IniProfile(std::istream& is);
96
100 virtual ~IniProfile();
101
105 bool getStringByRef(const std::string& key, std::string& value, const std::string& defaultString = {}) const;
106
110 void flush();
111
115 void setKeyPrefix(const std::string& prefix = {});
116
120 std::string getString(const std::string& key, const std::string& defaultString = {}) const;
121
125 bool setString(const std::string& key, const std::string& value);
126
130 bool setInt(const std::string& key, int value);
131
135 int getInt(const std::string& key, int defaultInt = 0) const;
136
140 bool setFloat(const std::string& key, double value);
141
145 double getFloat(const std::string& key, double defaultFloat = 0.0) const;
146
150 bool setBool(const std::string& key, bool value);
151
155 bool getBool(const std::string& key, bool defaultBool = false) const;
156
163 bool setSection(const std::string& section, bool create = true);
164
170 bool selectSection(const std::string& section) const;
171
179
185 [[nodiscard]] inline size_type getSectionCount() const;
186
190 [[nodiscard]] std::string getSection(size_type p = npos) const;
191
195 [[nodiscard]] string_list getSections() const;
196
201 [[nodiscard]] size_type findSection(const std::string& section) const;
202
209 [[nodiscard]] bool sectionExists(const std::string& section) const
210 {
211 return findSection(section) != npos;
212 }
213
220 bool keyExists(const std::string& key) const
221 {
222 // Lookup the key in current section and set the entry
223 return _sections.empty() ? npos : _sections[_sectionIndex]->findEntry(key) != npos;
224 }
225
229 inline bool removeSection(const std::string& section);
230
235
239 bool removeKeys(size_type section = npos);
240
244 bool insertComment(const std::string& key, const std::string& comment);
245
249 [[nodiscard]] size_type getEntryCount() const;
250
254 size_type findEntry(const std::string& key);
255
261 std::string getEntryKey(size_type p);
262 string_list getKeys(const std::string& section) const;
263
269 string_list getKeys(size_type section = npos) const;
270
274 typedef std::unordered_map<std::string, std::string> KeyValueMap;
275
281 void setMap(const KeyValueMap& map, size_type section = npos);
282
288 KeyValueMap getMap(size_type section = npos) const;
289
293 std::string getEntryValue(size_type p);
294
298 bool removeKey(const std::string& key);
299
304
312 bool setFilepath(const std::string& path);
313
318 const std::string& getFilepath() const;
319
324 bool exists() const;
325
332 void setWriteOnDirty(bool enable)
333 {
334 _writeOnDirty = enable;
335 }
336
343 bool sync();
344
348 virtual std::ostream& write(std::ostream& os) const;
349
353 std::ostream& writeSection(std::ostream& os) const;
354
358 bool isDirty() const
359 {
360 return _dirty;
361 }
362
366 bool read(std::istream& is);
367
368 protected:
373
377 bool load(const std::string& path);
378
382 bool load(std::istream& is);
383
387 class Entry
388 {
389 public:
390 Entry() = default;
391
392 Entry(const std::string& key, const std::string& value);
393
394 explicit Entry(const std::string& comment);
395
396 bool setLine(const std::string&);
397
398 std::ostream& write(std::ostream& os);
399
400 bool read(std::istream& is);
401
402 [[nodiscard]] inline bool isValid() const;
403
404 private:
405 bool _cmtFlag{false};
406 std::string _key;
407 std::string _value;
408
409 friend class IniProfile;
410 };
411
416 {
417 public:
422 : _entries(0)
423 {}
424
429
436
443 int setEntry(const std::string& key, const std::string& value);
444
451 std::string getEntry(const std::string& key, const std::string& defValue);
452
459
466 bool insertComment(const std::string& key, const std::string& comment);
467
473 std::ostream& write(std::ostream& os);
474
480 bool read(std::istream& is);
481
485 std::string _name;
486
491 };
492
497
502
510 SectionVector::size_type _sectionIndex{npos};
514 std::string _path;
518 bool _dirty{false};
522 bool _flagClearOnRead{true};
526 bool _writeOnDirty{true};
530 std::string _prefix;
531};
532
537
538inline bool IniProfile::Entry::isValid() const
539{
540 return !_key.empty();
541}
542
543inline bool IniProfile::removeSection(const std::string& section)
544{
545 return removeSection(findSection(section));
546}
547
548inline std::ostream& operator<<(std::ostream& os, const IniProfile& profile)
549{
550 return profile.write(os);
551}
552
553}// namespace sf
Internal storage class for keys.
Definition IniProfile.h:388
std::ostream & write(std::ostream &os)
bool setLine(const std::string &)
bool read(std::istream &is)
Entry(const std::string &comment)
bool isValid() const
Definition IniProfile.h:538
Entry(const std::string &key, const std::string &value)
Internal storage class for sections.
Definition IniProfile.h:416
bool insertComment(const std::string &key, const std::string &comment)
Adds comment to before entry specified by the key.
bool read(std::istream &is)
Read section from an input stream.
std::string _name
Holds the name of the section.
Definition IniProfile.h:485
bool removeEntry(EntryVector::size_type index)
Removes an entry from an index position.
std::ostream & write(std::ostream &os)
Streams section to an output stream.
IniProfile::EntryVector::size_type findEntry(const std::string &key)
Finds an entry by key value.
Section()
Default constructor.
Definition IniProfile.h:421
EntryVector _entries
Holds the key value pairs.
Definition IniProfile.h:490
int setEntry(const std::string &key, const std::string &value)
Sets sections value by key name.
std::string getEntry(const std::string &key, const std::string &defValue)
Gets an entry string and returns a default when it does not exist.
Class for reading and writing ini-profiles.
Definition IniProfile.h:17
bool exists() const
Checks the existence of the associated file.
std::string getEntryKey(size_type p)
Returns the entry value in current section, returns key in case of a comment line it returns a string...
size_type findSection(const std::string &section) const
Finds current or specific section location in list.
Entry * getEntry(EntryVector::size_type p)
Gets nullptr if not exist.
SectionVector _sections
Vector holding all sections.
Definition IniProfile.h:506
bool removeSection(const std::string &section)
Removes a section by name.
Definition IniProfile.h:543
bool selectSection(const std::string &section) const
EntryVector::size_type size_type
Type definition for the used size.
Definition IniProfile.h:42
bool removeSection(size_type p)
Removes a section by position.
string_list getKeys(size_type section=npos) const
Gets the keys of the current section or of the passed section index.
IniProfile(const std::string_view &data)
Initializing constructor using string. Create by reading from std::istream current position.
IniProfile(const std::string &section, const std::string &path)
Initializing constructor. If filename is empty the startup path is selected.
IniProfile(const std::string &path)
Initializing constructor.
string_list getSections() const
Get all section names in a std::string vector.
bool load(std::istream &is)
InitializeBase from stream.
bool getBool(const std::string &key, bool defaultBool=false) const
Gets a boolean value.
void setWriteOnDirty(bool enable)
Set the write-on-dirty flag so changes are written on destruction. The default is true.
Definition IniProfile.h:332
bool setFloat(const std::string &key, double value)
Gets a boolean value.
bool sync()
Syncs the content to file when dirty.
bool load(const std::string &path)
InitializeBase from file path.
std::unordered_map< std::string, std::string > KeyValueMap
Type for retrieving key and values.
Definition IniProfile.h:274
IniProfile()
Default constructor for empty instance.
size_type getSectionCount() const
Gets the section count.
Definition IniProfile.h:533
bool setSection(const std::string &section, bool create=true)
Sets current section.
KeyValueMap getMap(size_type section=npos) const
Gets the key value map from the current selected section by default or the section provided.
std::string getSection(size_type p=npos) const
Get current or specific section name p is index in section list.
void flush()
Flushes/Removes all sections from this instance.
bool setString(const std::string &key, const std::string &value)
Sets a string class value making the Dirty flag true.
bool setFilepath(const std::string &path)
Set the path of this instance.
bool setBool(const std::string &key, bool value)
Sets a boolean value and sets the Dirty flag to true.
bool insertComment(const std::string &key, const std::string &comment)
Inserts comment before existing key.
bool removeKey(const std::string &key)
Removes entry by key name.
size_type findEntry(const std::string &key)
Finds in the current section the key, on failure it returns npos.
bool setInt(const std::string &key, int value)
Sets an integer value and sets the Dirty flag to true.
void setKeyPrefix(const std::string &prefix={})
Prepends prefix to key if prefix not null.
void initialize()
Function called in any constructor.
int getInt(const std::string &key, int defaultInt=0) const
Gets a integer value.
std::string _prefix
olds the prefix for all entered 'key's'.
Definition IniProfile.h:530
double getFloat(const std::string &key, double defaultFloat=0.0) const
Sets a double value and sets the Dirty flag to true.
TVector< Section * > SectionVector
Internally used type.
Definition IniProfile.h:496
size_type getEntryCount() const
Returns amount of entries in the current section.
bool isDirty() const
Tells if there write functions are used or not.
Definition IniProfile.h:358
IniProfile(const IniProfile &)=delete
Copying this class is not possible.
IniProfile & operator=(IniProfile &&)=delete
No move operator either.
const std::string & getFilepath() const
IniProfile(std::istream &is)
Initializing constructor. Create by reading from std::istream current position.
IniProfile & operator=(IniProfile &)=delete
No copy operator either.
bool getStringByRef(const std::string &key, std::string &value, const std::string &defaultString={}) const
Works the same as TProfile from OWL.
bool removeKeys(size_type section=npos)
Removes all keys from a section by position or the current selected one if none has been passed.
bool read(std::istream &is)
Add sections from other std::istream.
TVector< Entry * > EntryVector
Internally used type.
Definition IniProfile.h:32
bool removeEntry(size_type p)
Removes entry or comment by position.
bool sectionExists(const std::string &section) const
Returns whether the passed section exist in the profile.
Definition IniProfile.h:209
virtual std::ostream & write(std::ostream &os) const
read to std::ostream 'os' current position.
std::ostream & writeSection(std::ostream &os) const
read current section to an 'ostream'.
bool setSection(size_type index)
Sets current section by index.
bool keyExists(const std::string &key) const
Returns whether the passed key exist in the current selected in the section of the profile.
Definition IniProfile.h:220
string_list getKeys(const std::string &section) const
std::string getEntryValue(size_type p)
Get entry key in current section, returns key or comment.
void setMap(const KeyValueMap &map, size_type section=npos)
Replaces the section with the given value map.
IniProfile(const IniProfile &&)=delete
No move constructor either.
std::string _path
Path to file if it was created with one otherwise 'length' is zero.
Definition IniProfile.h:514
virtual ~IniProfile()
Virtual destructor writes to file path when dirty.
std::string getString(const std::string &key, const std::string &defaultString={}) const
Gets a string class value.
Counted vector having additional methods and operators for ease of usage. This template class extends...
Definition TVector.h:19
base_type::size_type size_type
Size type of this template.
Definition TVector.h:28
size_type count() const
Returns the amount of entries in the vector.
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
TStrings< std::string > string_list
Vector of std::strings with additional functionality.
Definition TStrings.h:97