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
54 explicit IniProfile(const std::string& path);
55
60 IniProfile(const IniProfile&) = delete;
61
66 IniProfile(const IniProfile&&) = delete;
67
73
79
84 IniProfile(const std::string& section, const std::string& path);
85
90 explicit IniProfile(const std::string_view& data);
91
96 explicit IniProfile(std::istream& is);
97
101 virtual ~IniProfile();
102
106 bool getStringByRef(const std::string& key, std::string& value, const std::string& defaultString = {}) const;
107
111 void flush();
112
116 void setKeyPrefix(const std::string& prefix = {});
117
121 [[nodiscard]] std::string getString(const std::string& key, const std::string& defaultString = {}) const;
122
126 bool setString(const std::string& key, const std::string& value);
127
131 bool setInt(const std::string& key, int value);
132
136 int getInt(const std::string& key, int defaultInt = 0) const;
137
141 bool setFloat(const std::string& key, double value);
142
146 double getFloat(const std::string& key, double defaultFloat = 0.0) const;
147
151 bool setBool(const std::string& key, bool value);
152
156 bool getBool(const std::string& key, bool defaultBool = false) const;
157
164 bool setSection(const std::string& section, bool create = true);
165
171 bool selectSection(const std::string& section) const;
172
180
186 [[nodiscard]] inline size_type getSectionCount() const;
187
191 [[nodiscard]] std::string getSection(size_type p = npos) const;
192
196 [[nodiscard]] strings getSections() const;
197
202 [[nodiscard]] size_type findSection(const std::string& section) const;
203
210 [[nodiscard]] bool sectionExists(const std::string& section) const
211 {
212 return findSection(section) != npos;
213 }
214
221 [[nodiscard]] bool keyExists(const std::string& key) const
222 {
223 // Lookup the key in current section and set the entry
224 return _sections.empty() ? npos : _sections[_sectionIndex]->findEntry(key) != npos;
225 }
226
230 inline bool removeSection(const std::string& section);
231
236
240 bool removeKeys(size_type section = npos);
241
245 bool insertComment(const std::string& key, const std::string& comment);
246
250 [[nodiscard]] size_type getEntryCount() const;
251
255 size_type findEntry(const std::string& key);
256
262 std::string getEntryKey(size_type p);
263 strings getKeys(const std::string& section) const;
264
270 [[nodiscard]] strings getKeys(size_type section = npos) const;
271
275 typedef std::unordered_map<std::string, std::string> KeyValueMap;
276
282 void setMap(const KeyValueMap& map, size_type section = npos);
283
289 [[nodiscard]] KeyValueMap getMap(size_type section = npos) const;
290
294 std::string getEntryValue(size_type p);
295
299 bool removeKey(const std::string& key);
300
305
313 bool setFilepath(const std::string& path);
314
319 [[nodiscard]] const std::string& getFilepath() const;
320
325 bool exists() const;
326
333 void setWriteOnDirty(bool enable)
334 {
335 _writeOnDirty = enable;
336 }
337
344 bool sync();
345
349 virtual std::ostream& write(std::ostream& os) const;
350
354 std::ostream& writeSection(std::ostream& os) const;
355
359 [[nodiscard]] bool isDirty() const
360 {
361 return _dirty;
362 }
363
367 bool read(std::istream& is);
368
369 protected:
374
378 bool load(const std::string& path);
379
383 bool load(std::istream& is);
384
388 class Entry
389 {
390 public:
391 Entry() = default;
392
393 Entry(const std::string& key, const std::string& value);
394
395 explicit Entry(const std::string& comment);
396
397 bool setLine(const std::string&);
398
399 std::ostream& write(std::ostream& os);
400
401 bool read(std::istream& is);
402
403 [[nodiscard]] inline bool isValid() const;
404
405 private:
406 bool _cmtFlag{false};
407 std::string _key;
408 std::string _value;
409
410 friend class IniProfile;
411 };
412
417 {
418 public:
423 : _entries(0)
424 {}
425
430
437
444 int setEntry(const std::string& key, const std::string& value);
445
452 std::string getEntry(const std::string& key, const std::string& defValue);
453
460
467 bool insertComment(const std::string& key, const std::string& comment);
468
474 std::ostream& write(std::ostream& os);
475
481 bool read(std::istream& is);
482
486 std::string _name;
487
492 };
493
498
503
511 SectionVector::size_type _sectionIndex{npos};
515 std::string _path;
519 bool _dirty{false};
523 bool _flagClearOnRead{true};
527 bool _writeOnDirty{true};
531 std::string _prefix;
532};
533
538
539inline bool IniProfile::Entry::isValid() const
540{
541 return !_key.empty();
542}
543
544inline bool IniProfile::removeSection(const std::string& section)
545{
546 return removeSection(findSection(section));
547}
548
549inline std::ostream& operator<<(std::ostream& os, const IniProfile& profile)
550{
551 return profile.write(os);
552}
553
554}// namespace sf
Internal storage class for keys.
Definition IniProfile.h:389
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:539
Entry(const std::string &key, const std::string &value)
Internal storage class for sections.
Definition IniProfile.h:417
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:486
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:422
EntryVector _entries
Holds the key value pairs.
Definition IniProfile.h:491
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:507
bool removeSection(const std::string &section)
Removes a section by name.
Definition IniProfile.h:544
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.
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.
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:333
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:275
IniProfile()
Default constructor for empty instance.
size_type getSectionCount() const
Gets the section count.
Definition IniProfile.h:534
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.
strings getKeys(size_type section=npos) const
Gets the keys of the current section or of the passed section index.
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:531
double getFloat(const std::string &key, double defaultFloat=0.0) const
Sets a double value and sets the Dirty flag to true.
strings getSections() const
Get all section names in a std::string vector.
TVector< Section * > SectionVector
Internally used type.
Definition IniProfile.h:497
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:359
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.
strings getKeys(const std::string &section) const
bool sectionExists(const std::string &section) const
Returns whether the passed section exist in the profile.
Definition IniProfile.h:210
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:221
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:515
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:20
base_type::size_type size_type
Size type of this template.
Definition TVector.h:29
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 > strings
Vector of std::strings with additional functionality.
Definition TStrings.h:193