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
178
184
188 std::string getSection(size_type p = npos) const;
189
194
199 size_type findSection(const std::string& section) const;
200
207 bool sectionExists(const std::string& section) const;
208
215 bool keyExists(const std::string& key) const;
216
220 bool removeSection(const std::string& section);
221
226
230 bool removeKeys(size_type section = npos);
231
235 bool insertComment(const std::string& key, const std::string& comment);
236
240 [[nodiscard]] size_type getEntryCount() const;
241
245 size_type findEntry(const std::string& key);
246
252 std::string getEntryKey(size_type pos);
253
259 string_list getKeys(const std::string& section) const;
260
266 string_list getKeys(size_type section = npos) const;
267
271 typedef std::unordered_map<std::string, std::string> KeyValueMap;
272
278 void setMap(const KeyValueMap& map, size_type section = npos);
279
285 KeyValueMap getMap(size_type section = npos) const;
286
291 std::string getEntryValue(size_type p);
292
296 bool removeKey(const std::string& key);
297
302
309 bool setFilepath(const std::string& path);
310
315 const std::string& getFilepath() const;
316
321 bool exists() const;
322
329 void setWriteOnDirty(bool enable)
330 {
331 _writeOnDirty = enable;
332 }
333
339 bool sync();
340
344 virtual std::ostream& write(std::ostream& os) const;
345
349 std::ostream& writeSection(std::ostream& os) const;
350
354 bool isDirty() const
355 {
356 return _dirty;
357 }
358
362 bool read(std::istream& is);
363
364 protected:
369
373 bool load(const std::string& filepath);
374
378 bool load(std::istream& is);
379
383 class Entry
384 {
385 public:
386 Entry() = default;
387
388 Entry(const std::string& key, const std::string& value);
389
390 explicit Entry(const std::string& comment);
391
392 bool setLine(const std::string&);
393
394 std::ostream& write(std::ostream& os);
395
396 bool read(std::istream& is);
397
398 bool isValid() const;
399
400 private:
402 bool _cmtFlag{false};
404 std::string _key;
406 std::string _value;
407
408 friend class IniProfile;
409 };
410
415 {
416 public:
421 : _entries(0)
422 {}
423
428
434 EntryVector::size_type findEntry(const std::string& key);
435
442 int setEntry(const std::string& key, const std::string& value);
443
450 std::string getEntry(const std::string& key, const std::string& defValue);
451
458
465 bool insertComment(const std::string& key, const std::string& comment);
466
472 std::ostream& write(std::ostream& os);
473
479 bool read(std::istream& is);
480
484 std::string _name;
485
490 };
491
496
501
509 SectionVector::size_type _sectionIndex{npos};
513 std::string _path;
517 bool _dirty{false};
521 bool _flagClearOnRead{true};
525 bool _writeOnDirty{true};
529 std::string _prefix;
530};
531
538inline std::ostream& operator<<(std::ostream& os, const IniProfile& profile)
539{
540 return profile.write(os);
541}
542
543}// namespace sf
Internal storage class for keys.
Definition IniProfile.h:384
std::ostream & write(std::ostream &os)
bool setLine(const std::string &)
bool read(std::istream &is)
Entry(const std::string &comment)
bool isValid() const
Entry(const std::string &key, const std::string &value)
Internal storage class for sections.
Definition IniProfile.h:415
bool insertComment(const std::string &key, const std::string &comment)
Adds comment to before entry specified by the key.
EntryVector::size_type findEntry(const std::string &key)
Finds an entry by key value.
bool read(std::istream &is)
Read the section from an input stream.
std::string _name
Holds the name of the section.
Definition IniProfile.h:484
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.
Section()
Default constructor.
Definition IniProfile.h:420
EntryVector _entries
Holds the key value pairs.
Definition IniProfile.h:489
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.
size_type findSection(const std::string &section) const
Finds current or specific section location in the list.
Entry * getEntry(EntryVector::size_type p)
Gets nullptr if not exist.
SectionVector _sections
Vector holding all sections.
Definition IniProfile.h:505
bool removeSection(const std::string &section)
Removes a section by name.
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 the 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)
Initialize the base from the input 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:329
bool setFloat(const std::string &key, double value)
Gets a boolean value.
bool sync()
Syncs the content to file when the dirty-flag has been set. If the instance was created using a filep...
std::unordered_map< std::string, std::string > KeyValueMap
Type for retrieving key and values.
Definition IniProfile.h:271
IniProfile()
Default constructor for empty instance.
bool setSection(const std::string &section, bool create=true)
Sets the 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.
bool load(const std::string &filepath)
Initialize the base from the filepath.
std::string getSection(size_type p=npos) const
Gets the current or specific section name where p is the index in the 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. When the current dirty-flag has been set, it is first written to disk ...
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.
std::string getEntryKey(size_type pos)
Gets the value in the current section from the given pos position. In the case of a comment-line,...
int getInt(const std::string &key, int defaultInt=0) const
Gets an integer value.
size_type getSectionCount() const
Gets the section count.
std::string _prefix
olds the prefix for all entered 'key's'.
Definition IniProfile.h:529
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:495
size_type getEntryCount() const
Gets the number of entries in the current section.
bool isDirty() const
Tells if there write functions are used or not.
Definition IniProfile.h:354
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 exists in the profile.
virtual std::ostream & write(std::ostream &os) const
read to std::ostream 'os' current position.
std::ostream & writeSection(std::ostream &os) const
Writes the current section to an 'ostream'.
bool setSection(size_type index)
Sets the current section by index.
bool keyExists(const std::string &key) const
Returns whether the passed key exists in the current selected in the section of the profile.
string_list getKeys(const std::string &section) const
Gets all keys from the given section.
std::string getEntryValue(size_type p)
Gets entry value string in the current section by the given pos position.
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:513
virtual ~IniProfile()
Virtual destructor writes to filepath 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
#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