Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
sf::IniProfile Class Reference

Class for reading and writing ini-profiles. More...

#include <IniProfile.h>

Collaboration diagram for sf::IniProfile:

Classes

class  Entry
 Internal storage class for keys. More...
 
class  Section
 Internal storage class for sections. More...
 

Public Types

typedef TVector< Entry * > EntryVector
 Internally used type.
 
typedef EntryVector::size_type size_type
 Type definition for the used size.
 
typedef std::map< std::string, std::string > KeyValueMap
 Type for retrieving key and values.
 

Public Member Functions

 IniProfile ()
 Default constructor for empty instance.
 
 IniProfile (const std::string &path)
 Initializing constructor.
 
 IniProfile (const IniProfile &)=delete
 Copying this class is not possible.
 
 IniProfile (const IniProfile &&)=delete
 No move constructor either.
 
IniProfileoperator= (IniProfile &)=delete
 No copy operator either.
 
IniProfileoperator= (IniProfile &&)=delete
 No move operator either.
 
 IniProfile (const std::string &section, const std::string &path)
 Initializing constructor. If filename is empty the startup path is selected.
 
 IniProfile (const std::string_view &data)
 Initializing constructor using string. Create by reading from std::istream current position.
 
 IniProfile (std::istream &is)
 Initializing constructor. Create by reading from std::istream current position.
 
virtual ~IniProfile ()
 Virtual destructor writes to file path when dirty.
 
int getInt (const std::string &key, int defaultInt=0) const
 Works the same as TProfile from OWL.
 
bool getString (const std::string &key, std::string &value, const std::string &defaultString={}) const
 Works the same as TProfile from OWL.
 
void flush ()
 Flushes/Removes all sections from this instance.
 
void setKeyPrefix (const std::string &prefix={})
 Prepends prefix to key if prefix not null.
 
std::string getString (const std::string &key, const std::string &defaultString={}) const
 Gets a string class value.
 
bool setString (const std::string &key, const std::string &st)
 Sets a string class value making the Dirty flag true.
 
bool setInt (const std::string &key, int value)
 Sets an (int) value and sets the Dirty flag to true.
 
bool setSection (const std::string &section, bool create=true)
 Sets current section, returns true if section exist and creates one if 'create' is true.
 
bool selectSection (const std::string &section) const
 
bool setSection (size_type index)
 Sets current section by index.
 
size_type getSectionCount () const
 Gets the section count.
 
std::string getSection (size_type p=npos) const
 Get current or specific section name p is index in section list.
 
strings getSections () const
 Get all section names in a std::string vector.
 
size_type findSection (const std::string &section) const
 Finds current or specific section location in list.
 
bool sectionExists (const std::string &section) const
 Returns whether the passed section exist in the profile.
 
bool keyExists (const std::string &key) const
 Returns whether the passed key exist in the current selected in the section of the profile.
 
bool removeSection (const std::string &section)
 Removes a section by name.
 
bool removeSection (size_type p)
 Removes a section by position.
 
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 insertComment (const std::string &key, const std::string &comment)
 Inserts comment before existing key.
 
size_type getEntryCount () const
 Returns amount of entries in the current section.
 
size_type findEntry (const std::string &key)
 Finds in the current section the key, on failure it returns npos.
 
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 of zero length p is index in entry (key) list (includes comments !!)
 
strings getKeys (size_type section=npos) const
 Gets the keys of the current section or of the passed section index.
 
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 getEntryValue (size_type p)
 Get entry key in current section, returns key or comment.
 
bool removeEntry (const std::string &key)
 Removes entry by key name.
 
bool removeEntry (size_type p)
 Removes entry or comment by position.
 
bool setFilepath (const std::string &path)
 Set the path of this instance.
 
const std::string & getFilepath () const
 
void setWriteOnDirty (bool enable)
 Set the write-on-dirty flag so changes are written on destruction. The default is true.
 
bool sync ()
 Syncs the content to file when dirty.
 
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 isDirty () const
 Tells if there write functions are used or not.
 
bool read (std::istream &is)
 Add sections from other std::istream.
 

Static Public Attributes

static constexpr size_t npos = EntryVector::npos
 Index not found return value.
 

Protected Types

typedef TVector< Section * > SectionVector
 Internally used type.
 

Protected Member Functions

void initialize ()
 Function called in any constructor.
 
bool load (const std::string &path)
 InitializeBase from file path.
 
bool load (std::istream &is)
 InitializeBase from stream.
 
EntrygetEntry (EntryVector::size_type p)
 Gets nullptr if not exist.
 

Protected Attributes

class _MISC_CLASS Entry
 Forward declaration and also having export attributes.
 
class _MISC_CLASS Section
 Forward declaration and also having export attributes.
 
SectionVector _sections
 Vector holding all sections.
 
SectionVector::size_type _sectionIndex {npos}
 Current index to section.
 
std::string _path
 Path to file if it was created with one otherwise 'length' is zero.
 
bool _dirty {false}
 Is Set to true if an entry has been changed.
 
bool _flagClearOnRead {true}
 If this flag is Set all fields read with 'ReadType' will be cleared.
 
bool _writeOnDirty {true}
 When true the profile is written to the file path that was used to read it.
 
std::string _prefix
 olds the prefix for all entered 'key's'.
 

Detailed Description

Class for reading and writing ini-profiles.

Member Typedef Documentation

◆ EntryVector

Internally used type.

◆ KeyValueMap

typedef std::map<std::string, std::string> sf::IniProfile::KeyValueMap

Type for retrieving key and values.

◆ SectionVector

Internally used type.

◆ size_type

Type definition for the used size.

Constructor & Destructor Documentation

◆ IniProfile() [1/7]

sf::IniProfile::IniProfile ( )

Default constructor for empty instance.

◆ IniProfile() [2/7]

sf::IniProfile::IniProfile ( const std::string &  path)
explicit

Initializing constructor.

Parameters
pathFilepath to the ini-file.

◆ IniProfile() [3/7]

sf::IniProfile::IniProfile ( const IniProfile )
delete

Copying this class is not possible.

Todo:
Need to make this class copyable.

◆ IniProfile() [4/7]

sf::IniProfile::IniProfile ( const IniProfile &&  )
delete

No move constructor either.

Todo:
Need to make this class copyable.

◆ IniProfile() [5/7]

sf::IniProfile::IniProfile ( const std::string &  section,
const std::string &  path 
)

Initializing constructor. If filename is empty the startup path is selected.

◆ IniProfile() [6/7]

sf::IniProfile::IniProfile ( const std::string_view &  data)
explicit

Initializing constructor using string. Create by reading from std::istream current position.

◆ IniProfile() [7/7]

sf::IniProfile::IniProfile ( std::istream &  is)
explicit

Initializing constructor. Create by reading from std::istream current position.

◆ ~IniProfile()

virtual sf::IniProfile::~IniProfile ( )
virtual

Virtual destructor writes to file path when dirty.

Member Function Documentation

◆ findEntry()

size_type sf::IniProfile::findEntry ( const std::string &  key)

Finds in the current section the key, on failure it returns npos.

◆ findSection()

size_type sf::IniProfile::findSection ( const std::string &  section) const

Finds current or specific section location in list.

Returns
npos on not found.

◆ flush()

void sf::IniProfile::flush ( )

Flushes/Removes all sections from this instance.

◆ getEntry()

Entry * sf::IniProfile::getEntry ( EntryVector::size_type  p)
protected

Gets nullptr if not exist.

◆ getEntryCount()

size_type sf::IniProfile::getEntryCount ( ) const

Returns amount of entries in the current section.

◆ getEntryKey()

std::string sf::IniProfile::getEntryKey ( size_type  p)

Returns the entry value in current section, returns key in case of a comment line it returns a string of zero length p is index in entry (key) list (includes comments !!)

◆ getEntryValue()

std::string sf::IniProfile::getEntryValue ( size_type  p)

Get entry key in current section, returns key or comment.

◆ getFilepath()

const std::string & sf::IniProfile::getFilepath ( ) const

Gets the file path when it is set.

Returns
The ini filepath.

◆ getInt()

int sf::IniProfile::getInt ( const std::string &  key,
int  defaultInt = 0 
) const

Works the same as TProfile from OWL.

◆ getKeys()

strings sf::IniProfile::getKeys ( size_type  section = npos) const

Gets the keys of the current section or of the passed section index.

Parameters
sectionSection index where npos indicates the current selected one.
Returns
Vector of std::string of keys.

◆ getMap()

KeyValueMap sf::IniProfile::getMap ( size_type  section = npos) const

Gets the key value map from the current selected section by default or the section provided.

Parameters
sectionIndex of the section (default 'npos' = current)
Returns
Map of keys and values.

◆ getSection()

std::string sf::IniProfile::getSection ( size_type  p = npos) const

Get current or specific section name p is index in section list.

◆ getSectionCount()

IniProfile::size_type sf::IniProfile::getSectionCount ( ) const
inline

Gets the section count.

Returns
Amount of sections.

◆ getSections()

strings sf::IniProfile::getSections ( ) const

Get all section names in a std::string vector.

◆ getString() [1/2]

std::string sf::IniProfile::getString ( const std::string &  key,
const std::string &  defaultString = {} 
) const

Gets a string class value.

◆ getString() [2/2]

bool sf::IniProfile::getString ( const std::string &  key,
std::string &  value,
const std::string &  defaultString = {} 
) const

Works the same as TProfile from OWL.

◆ initialize()

void sf::IniProfile::initialize ( )
protected

Function called in any constructor.

◆ insertComment()

bool sf::IniProfile::insertComment ( const std::string &  key,
const std::string &  comment 
)

Inserts comment before existing key.

◆ isDirty()

bool sf::IniProfile::isDirty ( ) const
inline

Tells if there write functions are used or not.

◆ keyExists()

bool sf::IniProfile::keyExists ( const std::string &  key) const
inline

Returns whether the passed key exist in the current selected in the section of the profile.

Parameters
keyKey name.
Returns
True when it exists.

◆ load() [1/2]

bool sf::IniProfile::load ( const std::string &  path)
protected

InitializeBase from file path.

◆ load() [2/2]

bool sf::IniProfile::load ( std::istream &  is)
protected

InitializeBase from stream.

◆ operator=() [1/2]

IniProfile & sf::IniProfile::operator= ( IniProfile &&  )
delete

No move operator either.

Todo:
Need to make this class copyable.

◆ operator=() [2/2]

IniProfile & sf::IniProfile::operator= ( IniProfile )
delete

No copy operator either.

Todo:
Need to make this class copyable.

◆ read()

bool sf::IniProfile::read ( std::istream &  is)

Add sections from other std::istream.

◆ removeEntry() [1/2]

bool sf::IniProfile::removeEntry ( const std::string &  key)

Removes entry by key name.

◆ removeEntry() [2/2]

bool sf::IniProfile::removeEntry ( size_type  p)

Removes entry or comment by position.

◆ removeKeys()

bool sf::IniProfile::removeKeys ( size_type  section = npos)

Removes all keys from a section by position or the current selected one if none has been passed.

◆ removeSection() [1/2]

bool sf::IniProfile::removeSection ( const std::string &  section)
inline

Removes a section by name.

◆ removeSection() [2/2]

bool sf::IniProfile::removeSection ( size_type  p)

Removes a section by position.

◆ sectionExists()

bool sf::IniProfile::sectionExists ( const std::string &  section) const
inline

Returns whether the passed section exist in the profile.

Parameters
sectionSection name.
Returns
True when it exists.

◆ selectSection()

bool sf::IniProfile::selectSection ( const std::string &  section) const

Selects section without creating.

Parameters
section
Returns

◆ setFilepath()

bool sf::IniProfile::setFilepath ( const std::string &  path)

Set the path of this instance.

When the current file is dirty it is first written to disk.

Parameters
pathFilepath to the ini-file.
Returns
True on successful loading of the passed file.

◆ setInt()

bool sf::IniProfile::setInt ( const std::string &  key,
int  value 
)

Sets an (int) value and sets the Dirty flag to true.

◆ setKeyPrefix()

void sf::IniProfile::setKeyPrefix ( const std::string &  prefix = {})

Prepends prefix to key if prefix not null.

◆ setSection() [1/2]

bool sf::IniProfile::setSection ( const std::string &  section,
bool  create = true 
)

Sets current section, returns true if section exist and creates one if 'create' is true.

◆ setSection() [2/2]

bool sf::IniProfile::setSection ( size_type  index)

Sets current section by index.

Parameters
indexSection index.
Returns
true if section exist.

◆ setString()

bool sf::IniProfile::setString ( const std::string &  key,
const std::string &  st 
)

Sets a string class value making the Dirty flag true.

◆ setWriteOnDirty()

void sf::IniProfile::setWriteOnDirty ( bool  enable)
inline

Set the write-on-dirty flag so changes are written on destruction. The default is true.

See also
write()
Parameters
enable

◆ sync()

bool sf::IniProfile::sync ( )

Syncs the content to file when dirty.

If the instance was created using a filepath the content is written when the dirty flag was set.

Returns
True on success or when no filepath assigned.

◆ write()

virtual std::ostream & sf::IniProfile::write ( std::ostream &  os) const
virtual

read to std::ostream 'os' current position.

◆ writeSection()

std::ostream & sf::IniProfile::writeSection ( std::ostream &  os) const

read current section to an 'ostream'.

Member Data Documentation

◆ _dirty

bool sf::IniProfile::_dirty {false}
protected

Is Set to true if an entry has been changed.

◆ _flagClearOnRead

bool sf::IniProfile::_flagClearOnRead {true}
protected

If this flag is Set all fields read with 'ReadType' will be cleared.

◆ _path

std::string sf::IniProfile::_path
protected

Path to file if it was created with one otherwise 'length' is zero.

◆ _prefix

std::string sf::IniProfile::_prefix
protected

olds the prefix for all entered 'key's'.

H

◆ _sectionIndex

SectionVector::size_type sf::IniProfile::_sectionIndex {npos}
protected

Current index to section.

◆ _sections

SectionVector sf::IniProfile::_sections
protected

Vector holding all sections.

◆ _writeOnDirty

bool sf::IniProfile::_writeOnDirty {true}
protected

When true the profile is written to the file path that was used to read it.

◆ Entry

Forward declaration and also having export attributes.

◆ npos

constexpr size_t sf::IniProfile::npos = EntryVector::npos
staticconstexpr

Index not found return value.

◆ Section

Forward declaration and also having export attributes.


The documentation for this class was generated from the following file: