PPL7-Icon Patrick's Programming Library Version 7.0.0 - Dokumentation
ppl7::ConfigParser Klassenreferenz

Lesen und Schreiben von Konfigurationsdateien. Mehr ...

Öffentliche Methoden

 ConfigParser ()
 
 ConfigParser (const String &filename)
 
 ConfigParser (FileObject &file)
 
 ~ConfigParser ()
 
void add (const String &section, const String &key, const String &value)
 
void add (const String &section, const String &key, const char *value)
 
void add (const String &key, const String &value)
 
void add (const String &key, const char *value)
 
void add (const String &key, int value)
 
void add (const String &key, bool value)
 
void add (const String &section, const String &key, int value)
 
void add (const String &section, const String &key, bool value)
 
void copySection (AssocArray &target, const String &section) const
 Inhalt einer Sektion in einem AssocArray speichern. Mehr ...
 
void createSection (const String &name)
 
void deleteKey (const String &key)
 
void deleteKey (const String &section, const String &key)
 
void deleteSection (const String &name)
 
int firstSection ()
 
String get (const String &key, const String &defaultvalue=String()) const
 
bool getBool (const String &key, bool defaultvalue=false) const
 
bool getBoolFromSection (const String &section, const String &key, bool defaultvalue=false) const
 
bool getFirst (String &key, String &value)
 
String getFromSection (const String &section, const String &key, const String &defaultvalue=String()) const
 
int getInt (const String &key, int defaultvalue=0) const
 
int getIntFromSection (const String &section, const String &key, int defaultvalue=0) const
 
bool getNext (String &key, String &value)
 
const StringgetSection (const String &name) const
 Inhalt einer Sektion als String. Mehr ...
 
const StringgetSectionName () const
 
const StringgetSeparator () const
 
void load (const String &filename)
 Konfiguration aus einer Datei laden. Mehr ...
 
void load (FileObject &file)
 Konfiguration aus einem FileObject-Objekt laden. Mehr ...
 
void loadFromMemory (const void *buffer, size_t bytes)
 Konfiguration aus dem Speicher laden. Mehr ...
 
void loadFromMemory (const ByteArrayPtr &ptr)
 Konfiguration aus dem Speicher laden. Mehr ...
 
void loadFromString (const String &string)
 Konfiguration aus einem String laden. Mehr ...
 
int nextSection ()
 
void print () const
 Konfiguration auf STDOUT ausgeben. Mehr ...
 
void reset ()
 
void save (const String &filename)
 Konfiguration in eine Datei speichern. Mehr ...
 
void save (FileObject &file)
 Konfiguration in ein FileObject speichern. Mehr ...
 
void selectSection (const String &section)
 
void setSeparator (const String &string)
 
void unload ()
 

Private Methoden

void * findSection (const String &sectionname) const
 

Private Attribute

void * first
 
AssocArray::Iterator it
 
void * last
 
void * section
 
AssocArray sections
 
String separator
 

Ausführliche Beschreibung

Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Klasse können Konfigurationsdateien mit mehreren Sektionen gelesen und geschrieben werden.
Beispiel:
Beispiel einer Konfigurationsdatei:
[server]
ip=127.0.0.1
port=8080
path=/var/tmp
[user]
# In dieser Sektion werden Username und Passwort gespeichert
root=42ee4d3cde1583d93b48923067696142
patrick=690669aaf38453f0c5676a4f16fe9d20
[allowedhosts]
# In dieser Sektion befinden sich IP-Adressen oder Hostnamen von den Rechnern,
# denen der Zugriff auf das Programm erlaubt ist.
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
Die Klasse kann folgendermassen verwendet werden:
#include <ppl7.h>
int main(int argc, char **argv) {
ppl7::String ip, path;
int port;
try {
conf.load("my.conf");
conf.selectSection("server");
ip=conf.get("ip");
port=conf.getInt("port",8080);
// alternativ: port=conf.get("port","8080").toInt();
path=conf.get("path");
} catch (const ppl7::Exception &e) {
cout << "Ein Fehler ist aufgetreten: " << e << "\n";
}
return 0;
}

Beschreibung der Konstruktoren und Destruktoren

ppl7::ConfigParser::ConfigParser ( )
ppl7::ConfigParser::ConfigParser ( const String filename)
ppl7::ConfigParser::ConfigParser ( FileObject file)
ppl7::ConfigParser::~ConfigParser ( )

Dokumentation der Elementfunktionen

void ppl7::ConfigParser::add ( const String section,
const String key,
const String value 
)
void ppl7::ConfigParser::add ( const String section,
const String key,
const char *  value 
)
void ppl7::ConfigParser::add ( const String key,
const String value 
)
void ppl7::ConfigParser::add ( const String key,
const char *  value 
)
void ppl7::ConfigParser::add ( const String key,
int  value 
)
void ppl7::ConfigParser::add ( const String key,
bool  value 
)
void ppl7::ConfigParser::add ( const String section,
const String key,
int  value 
)
void ppl7::ConfigParser::add ( const String section,
const String key,
bool  value 
)
void ppl7::ConfigParser::copySection ( AssocArray target,
const String section 
) const
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird der Inhalt einer Sektion in ein AssocArray kopiert.
Parameter
targetAssoziatives Array, in dem die Sektion gespeichert werden soll
sectionDer Name der Sektion ohne Eckige Klammern
void ppl7::ConfigParser::createSection ( const String name)
void ppl7::ConfigParser::deleteKey ( const String key)
void ppl7::ConfigParser::deleteKey ( const String section,
const String key 
)
void ppl7::ConfigParser::deleteSection ( const String name)
void * ppl7::ConfigParser::findSection ( const String sectionname) const
private
int ppl7::ConfigParser::firstSection ( )
String ppl7::ConfigParser::get ( const String key,
const String defaultvalue = String() 
) const
bool ppl7::ConfigParser::getBool ( const String key,
bool  defaultvalue = false 
) const
bool ppl7::ConfigParser::getBoolFromSection ( const String section,
const String key,
bool  defaultvalue = false 
) const
bool ppl7::ConfigParser::getFirst ( String key,
String value 
)
String ppl7::ConfigParser::getFromSection ( const String section,
const String key,
const String defaultvalue = String() 
) const
int ppl7::ConfigParser::getInt ( const String key,
int  defaultvalue = 0 
) const
int ppl7::ConfigParser::getIntFromSection ( const String section,
const String key,
int  defaultvalue = 0 
) const
bool ppl7::ConfigParser::getNext ( String key,
String value 
)
const String & ppl7::ConfigParser::getSection ( const String name) const
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird der komplette Inhalt einer Sektion als String zurückgegeben, wie sie beim Laden der Konfiguration vorgefunden wurde, einschließlich Kommentare. Zwischenzeitlich erfolgte Veränderungen, z.B. mit ConfigParser::add, sind nicht enthalten.
Die Funktion eignet sich gut, um Sektionen der Konfigdatei zu lesen, die keine Key-Value-Paare enthält.
Parameter
sectionDer Name der Sektion ohne Eckige Klammern
Rückgabe
Im Erfolgsfall liefert die Funktion einen String mit dem Inhalt der Sektion zurück. Im Fehlerfall wird eine Exception geworfen.
const String & ppl7::ConfigParser::getSectionName ( ) const
const String & ppl7::ConfigParser::getSeparator ( ) const
void ppl7::ConfigParser::load ( const String filename)
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird eine Konfiguration aus einer Datei geladen.
Parameter
filenameDateiname
void ppl7::ConfigParser::load ( FileObject file)
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird eine Konfiguration aus einem FileObject-Objekt geladen. Die Funktion wird intern von den anderen Load-Funktionen verwendet.
Parameter
fileReferenz auf eine FileObject-Klasse
Ausnahmebehandlung
File::FileNotOpenExceptionWird geworfen, wenn das FileObject /p file keine geöffnete Datei enthält
DivereseEs können alle Exceptions auftreten, File::gets werfen kann
Siehe auch
loadFromString(const String &string)
load(const String &filename);
loadFromMemory(const void *buffer, size_t bytes)
void ppl7::ConfigParser::loadFromMemory ( const void *  buffer,
size_t  bytes 
)
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird eine bereits im Speicher befindliche Konfiguration in das ConfigParser-Objekt geladen.
Parameter
bufferEin Pointer auf den Beginn des Speicherbereichs
bytesDie Größe des Speicherbereichs
void ppl7::ConfigParser::loadFromMemory ( const ByteArrayPtr ptr)
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird eine bereits im Speicher befindliche Konfiguration in das ConfigParser-Objekt geladen.
Parameter
ptrReferenz auf ein ByteArray oder ByteArrayPtr Objekt
void ppl7::ConfigParser::loadFromString ( const String string)
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird eine in einem String enthaltene Konfiguration in das ConfigParser-Objekt geladen.
Parameter
stringEin String, der die zu parsende Konfiguration enthält
int ppl7::ConfigParser::nextSection ( )
void ppl7::ConfigParser::print ( ) const
Include:
#include <ppl7.h>
Beschreibung:
Mit dieser Funktion wird die derzeit vorhandene Konfiguration auf STDOUT ausgegeben.
void ppl7::ConfigParser::reset ( )
void ppl7::ConfigParser::save ( const String filename)
Include:
#include <ppl7.h>
Beschreibung:
Der Inhalt des ConfigParser wird in die Datei filename gespeichert.
Parameter
filenameDer Dateiname, unter dem die Konfiguration gespeichert werden soll
Ausnahmebehandlung
DivereseEs können alle Exceptions auftreten, die File::open, File::puts und File::putsf werfen kann.
Zu beachten
Eine eventuell schon vorhandene Datei wird überschrieben. Der ConfigParser speichert nur die Sektionen mit den jeweiligen Key-Value-Paaren in alphabetischer Reihenfolge, ohne Kommentare.
void ppl7::ConfigParser::save ( FileObject file)
Include:
#include <ppl7.h>
Beschreibung:
Der Inhalt des ConfigParser wird in das FileObject file gespeichert.
Parameter
filenameDer Dateiname, unter dem die Konfiguration gespeichert werden soll
Ausnahmebehandlung
DivereseEs können alle Exceptions auftreten, File::puts und File::putsf werfen kann.
File::FileNotOpenExceptionWird geworfen, wenn das FileObject /p file keine geöffnete Datei enthält
void ppl7::ConfigParser::selectSection ( const String section)
void ppl7::ConfigParser::setSeparator ( const String string)
void ppl7::ConfigParser::unload ( )

Dokumentation der Datenelemente

void* ppl7::ConfigParser::first
private
AssocArray::Iterator ppl7::ConfigParser::it
private
void * ppl7::ConfigParser::last
private
void * ppl7::ConfigParser::section
private
AssocArray ppl7::ConfigParser::sections
private
String ppl7::ConfigParser::separator
private

Die Dokumentation für diese Klasse wurde erzeugt aufgrund der Dateien: