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
9namespace sf
10{
11
16{
17 protected:
26
27 public:
32
36 static constexpr size_t npos = EntryVector::npos;
37
42
47
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 [[nodiscard]] int getInt(const std::string& key, int defaultInt = 0) const;
106
110 bool getString(const std::string& key, std::string& value, const std::string& defaultString = {}) const;
111
115 void flush();
116
120 void setKeyPrefix(const std::string& prefix = {});
121
125 [[nodiscard]] std::string getString(const std::string& key, const std::string& defaultString = {}) const;
126
130 bool setString(const std::string& key, const std::string& st);
131
135 bool setInt(const std::string& key, int value);
136
141 bool setSection(const std::string& section, bool create = true);
142
148 [[nodiscard]] bool selectSection(const std::string& section) const;
149
157
163 [[nodiscard]] inline size_type getSectionCount() const;
164
168 [[nodiscard]] std::string getSection(size_type p = npos) const;
169
173 [[nodiscard]] strings getSections() const;
174
179 [[nodiscard]] size_type findSection(const std::string& section) const;
180
187 [[nodiscard]] bool sectionExists(const std::string& section) const
188 {
189 return findSection(section) != npos;
190 }
191
198 [[nodiscard]] bool keyExists(const std::string& key) const
199 {
200 // Lookup the key in current section and set the entry
201 return _sections.empty() ? npos : _sections[_sectionIndex]->findEntry(key) != npos;
202 }
203
207 inline bool removeSection(const std::string& section);
208
213
217 bool removeKeys(size_type section = npos);
218
222 bool insertComment(const std::string& key, const std::string& comment);
223
227 [[nodiscard]] size_type getEntryCount() const;
228
232 size_type findEntry(const std::string& key);
233
239 std::string getEntryKey(size_type p);
240
246 [[nodiscard]] strings getKeys(size_type section = npos) const;
247
251 typedef std::map<std::string, std::string> KeyValueMap;
252
258 [[nodiscard]] KeyValueMap getMap(size_type section = npos) const;
259
263 std::string getEntryValue(size_type p);
264
268 bool removeEntry(const std::string& key);
269
274
282 bool setFilepath(const std::string& path);
283
288 [[nodiscard]] const std::string& getFilepath() const;
289
296 void setWriteOnDirty(bool enable)
297 {
298 _writeOnDirty = enable;
299 }
300
307 bool sync();
308
312 virtual std::ostream& write(std::ostream& os) const;
313
317 std::ostream& writeSection(std::ostream& os) const;
318
322 [[nodiscard]] bool isDirty() const
323 {
324 return _dirty;
325 }
326
330 bool read(std::istream& is);
331
332 protected:
337
341 bool load(const std::string& path);
342
346 bool load(std::istream& is);
347
351 class Entry
352 {
353 public:
354 Entry() = default;
355
356 Entry(const std::string& key, const std::string& value);
357
358 explicit Entry(const std::string& comment);
359
360 bool setLine(const std::string&);
361
362 std::ostream& write(std::ostream& os);
363
364 bool read(std::istream& is);
365
366 [[nodiscard]] inline bool isValid() const;
367
368 private:
369 bool _cmtFlag{false};
370 std::string _key;
371 std::string _value;
372
373 friend class IniProfile;
374 };
375
380 {
381 public:
386 : _entries(0)
387 {}
388
393
400
407 int setEntry(const std::string& key, const std::string& value);
408
415 std::string getEntry(const std::string& key, const std::string& defValue);
416
423
430 bool insertComment(const std::string& key, const std::string& comment);
431
437 std::ostream& write(std::ostream& os);
438
444 bool read(std::istream& is);
445
449 std::string _name;
450
455 };
456
461
466
474 SectionVector::size_type _sectionIndex{npos};
478 std::string _path;
482 bool _dirty{false};
486 bool _flagClearOnRead{true};
490 bool _writeOnDirty{true};
494 std::string _prefix;
495};
496
501
502inline bool IniProfile::Entry::isValid() const
503{
504 return !_key.empty();
505}
506
507inline bool IniProfile::removeSection(const std::string& section)
508{
509 return removeSection(findSection(section));
510}
511
512inline std::ostream& operator<<(std::ostream& os, const IniProfile& profile)
513{
514 return profile.write(os);
515}
516
517}// namespace sf
Internal storage class for keys.
Definition IniProfile.h:352
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:502
Entry(const std::string &key, const std::string &value)
Internal storage class for sections.
Definition IniProfile.h:380
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:449
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:385
EntryVector _entries
Holds the key value pairs.
Definition IniProfile.h:454
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:16
bool setString(const std::string &key, const std::string &st)
Sets a string class value making the Dirty flag true.
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:470
bool removeSection(const std::string &section)
Removes a section by name.
Definition IniProfile.h:507
bool selectSection(const std::string &section) const
EntryVector::size_type size_type
Type definition for the used size.
Definition IniProfile.h:41
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.
void setWriteOnDirty(bool enable)
Set the write-on-dirty flag so changes are written on destruction. The default is true.
Definition IniProfile.h:296
bool sync()
Syncs the content to file when dirty.
bool load(const std::string &path)
InitializeBase from file path.
IniProfile()
Default constructor for empty instance.
size_type getSectionCount() const
Gets the section count.
Definition IniProfile.h:497
bool setSection(const std::string &section, bool create=true)
Sets current section, returns true if section exist and creates one if 'create' is true.
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 setFilepath(const std::string &path)
Set the path of this instance.
bool insertComment(const std::string &key, const std::string &comment)
Inserts comment before existing key.
size_type findEntry(const std::string &key)
Finds in the current section the key, on failure it returns npos.
bool removeEntry(const std::string &key)
Removes entry by key name.
bool setInt(const std::string &key, int value)
Sets an (int) 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
Works the same as TProfile from OWL.
std::string _prefix
olds the prefix for all entered 'key's'.
Definition IniProfile.h:494
strings getSections() const
Get all section names in a std::string vector.
TVector< Section * > SectionVector
Internally used type.
Definition IniProfile.h:460
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:322
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 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:31
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:187
virtual std::ostream & write(std::ostream &os) const
read to std::ostream 'os' current position.
bool getString(const std::string &key, std::string &value, const std::string &defaultString={}) const
Works the same as TProfile from OWL.
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:198
std::string getEntryValue(size_type p)
Get entry key in current section, returns key or comment.
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:478
std::map< std::string, std::string > KeyValueMap
Type for retrieving key and values.
Definition IniProfile.h:251
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 of strings.
Definition TStrings.h:12
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:25
base_type::size_type size_type
Size type of this template.
Definition TVector.h:34
size_type count() const
Returns the amount of entries in the vector.
Definition TVector.h:625
#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.