IniFile
Lade ...
Suche ...
Keine Treffer
ini::File Klassenreferenz

Eine INI-Datei beinhaltet Informationen für Programme. Mehr ...

#include <IniFile.h>

Öffentliche Methoden

 File (const std::string &rsFullFileName, TdOpenInifileMode openMode)
 
 File ()
 
virtual ~File ()
 
int store ()
 
int refresh ()
 
void setFullFileName (const std::string &rsFullFileName)
 
std::string getFileName ()
 
int getLine (Line *&prRetVal, std::size_t nPosition, ptl::SeekMode eMode)
 
int insertLine (Line *pIniLine, ptl::InsertPosition eInsertPosition=ptl::InsertCURRENT, ptl::VH eBeforeBehind=ptl::BEHIND)
 
int removeLine (ptl::ElementPosition eFirstLastCurrent=ptl::CURRENT_POS)
 
int getLine (Line *&prRetVal, std::string *psSection, std::string *psKey=0)
 
int getKeyValue (std::string &rsValue, std::string *psSection, std::string *psKey)
 
int setKeyValue (const std::string &rsSection, const std::string &rsKey, const std::string &rsValue)
 
int removeKey (const std::string &rsSection, const std::string &rsKey)
 
int getAsBlob (eut::Blob *&prBlob)
 
int makeLines (eut::Blob *pBlob)
 

Private Methoden

 File (const File &)
 
Fileoperator= (const File &)
 

Private Attribute

std::string m_sFileName
 
ptl::TDVList< Linem_LineList
 
LineFactory m_LineFactory
 

Ausführliche Beschreibung

Eine INI-Datei beinhaltet Informationen für Programme.

Eine INI-Datei kann Sektionen mit Variablen und Werten sowie Kommentare enthalten. Die Informationen können mit ini::File angelegt, gelesen und gepflegt werden können.

Codeschnipsel

Im folgenden wird eine Ini-Datei "test.ini" angelegt:

#include "IniFile.h"
#include "EuException.h"
#include <iostream>
int main(int argc, char *argv[])
{
int t_iErr = 0;
ini::File * t_pIniFile = 0;
//--------------------------------------------------------------------------
// New Ini-File "test.ini"
try
{
t_pIniFile = new ini::File( "test.ini", ini::bCreateInifile );
}
catch( eut::ErrorNException ERR )
{
std::cout << "Error :" << ERR.what() << "ErrNo.:" << ERR.error() << std::endl;
return ERR.error();
}
// new sections with new keys
if ( ! t_iErr ) t_iErr = t_pIniFile->setKeyValue( "[Section 1]", "Key 1.1", "Value 1.1" );
if ( ! t_iErr ) t_iErr = t_pIniFile->setKeyValue( "[Section 1]", "Key 1.2", "Value 1.2" );
if ( ! t_iErr ) t_iErr = t_pIniFile->setKeyValue( "[Section 2]", "Key 2.1", "Value 2.1" );
if ( ! t_iErr ) t_iErr = t_pIniFile->setKeyValue( "[Section 2]", "Key 2.2", "Value 2.2" );
// a comment
ini::Line * t_pCommentLine = 0;
try
{
t_pCommentLine = new ini::Line( "; comment 1" );
}
catch( eut::ErrorNException ERR )
{
std::cout << "Error :" << ERR.what() << "ErrNo.:" << ERR.error() << std::endl;
t_iErr = ERR.error();
}
if ( ! t_iErr )
{
t_iErr = t_pIniFile->insertLine( t_pCommentLine );
}
// a lonly key
if ( ! t_iErr ) t_iErr = t_pIniFile->setKeyValue( "", "Lonly Key 1", "Value 1" );
// and store the Ini-File
if ( ! t_iErr ) t_iErr = t_pIniFile->store();
delete t_pIniFile; t_pIniFile = 0;
}
Header für ErrorNException
Header für File
Diese Exception-Klasse hat zusätzlich zur Message ein Integer-Attribute für eine Fehlernummer.
Definition EuException.h:58
int error() const
Definition EuException.h:261
Eine INI-Datei beinhaltet Informationen für Programme.
Definition IniFile.h:298
int insertLine(Line *pIniLine, ptl::InsertPosition eInsertPosition=ptl::InsertCURRENT, ptl::VH eBeforeBehind=ptl::BEHIND)
int store()
int setKeyValue(const std::string &rsSection, const std::string &rsKey, const std::string &rsValue)
Ist eine Kommentartzeile, aber auch Oberklasse für Section und Key.
Definition IniLine.h:62
const TdOpenInifileMode bCreateInifile
Definition IniFileTypes.h:199

Die INI-Datei "test.ini" hat folgenden Inhalt:

[Section 1]
Key 1.1=Value 1.1
Key 1.2=Value 1.2
[Section 2]
Key 2.1=Value 2.1
Key 2.2=Value 2.2
; comment 1
Lonly Key 1=Value 1

In der INI-Datei "test.ini" wird der Schlüssel "Key 1.2" aus der Sektion "[Section 1]" geholt, angezeigt und auf "new Value" geändert:

#include "IniFile.h"
#include "EuException.h"
#include <iostream>
int main(int argc, char *argv[])
{
int t_iErr = 0;
ini::File * t_pIniFile = 0;
//--------------------------------------------------------------------------
// show and change a key in test.ini
try
{
t_pIniFile = new ini::File( "test.ini", ini::bOpenInifile );
}
catch( eut::ErrorNException ERR )
{
std::cout << "Error :" << ERR.what() << "ErrNo.:" << ERR.error() << std::endl;
return ERR.error();
}
// get the value from "Section 1", "Key 1.2"
std::string t_sValue,
t_sSection2( "[Section 1]" ),
t_sKey( "Key 1.2" );
t_iErr = t_pIniFile->getKeyValue( t_sValue, & t_sSection2, & t_sKey );
if ( ! t_iErr ) std::cout << "The value from 'Section 1', 'Key 1.2' is: " << t_sValue << std::endl;
if ( ! t_iErr ) t_iErr = t_pIniFile->setKeyValue( "[Section 1]", "Key 1.2", "new Value" );
if ( ! t_iErr ) t_iErr = t_pIniFile->store();
delete t_pIniFile; t_pIniFile = 0;
}
int getKeyValue(std::string &rsValue, std::string *psSection, std::string *psKey)
const TdOpenInifileMode bOpenInifile
Definition IniFileTypes.h:213

Die INI-Datei "test.ini" hat jetzt folgenden Inhalt:

[Section 1]
Key 1.1=Value 1.1
Key 1.2=new Value
[Section 2]
Key 2.1=Value 2.1
Key 2.2=Value 2.2
; comment 1
Lonly Key 1=Value 1

Es können alle Einträge ermittelt werden...

#include "IniFile.h"
#include "EuException.h"
#include <iostream>
int main(int argc, char *argv[])
{
int t_iErr = 0;
ini::File * t_pIniFile = 0;
//--------------------------------------------------------------------------
// show complete test.ini
try
{
t_pIniFile = new ini::File( "test.ini", ini::bOpenInifile );
}
catch( eut::ErrorNException ERR )
{
std::cout << "Error :" << ERR.what() << "ErrNo.:" << ERR.error() << std::endl;
return ERR.error();
}
ini::Line * t_pShowLine = 0;
std::cout << "############# " << t_pIniFile->getFileName() << " #############" << std::endl;
t_iErr = t_pIniFile->getLine( t_pShowLine, 0, ptl::START );
while ( t_pShowLine )
{
std::cout << t_pShowLine->getText() << std::endl;
// if section, show sectionlines
if ( t_pShowLine->getType() == ini::LINE_TYPE::IniSECTION )
{
ini::Section * t_pCastSectionLine = dynamic_cast<ini::Section*>(t_pShowLine);
ini::Line * t_pShowSectionLine = 0;
t_iErr = t_pCastSectionLine->getSectionLine( t_pShowSectionLine, 0, ptl::START );
while ( t_pShowSectionLine )
{
std::cout << t_pShowSectionLine->getText() << std::endl;
t_iErr = t_pCastSectionLine->getSectionLine( t_pShowSectionLine, 1, ptl::CURRENT );
}
t_iErr = ( t_iErr == eut::ERR_RANGE ? 0 : t_iErr );
}
t_iErr = t_pIniFile->getLine( t_pShowLine, 1, ptl::CURRENT );
}
t_iErr = ( t_iErr == ptl::RANGE ? 0 : t_iErr );
std::cout << "####################################" << std::endl;
std::cout << "ErrNo.:" << t_iErr << std::endl;
delete t_pIniFile; t_pIniFile = 0;
}
std::string getFileName()
int getLine(Line *&prRetVal, std::size_t nPosition, ptl::SeekMode eMode)
LINE_TYPE getType() const
std::string getText() const
Eine Sektion kann nur einmalig in einer INI-Datei vorkommen, steht in eckigen Klammern und hat in der...
Definition IniSection.h:71
int getSectionLine(Line *&prRetVal, std::size_t nPosition, ptl::SeekMode eMode)
@ IniSECTION
Definition IniFileTypes.h:140
@ RANGE
Definition PtlListErrors.h:199
@ CURRENT
Definition PtlListTypes.h:388
@ START
Definition PtlListTypes.h:366

... und ergibt folgende Anzeige:

############# test.ini #############
[Section 1]
Key 1.1=Value 1.1
Key 1.2=new Value
[Section 2]
Key 2.1=Value 2.1
Key 2.2=Value 2.2
; comment 1
Lonly Key 1=Value 1
####################################
ErrNo.:0
Autor
Helmut Jakoby

Beschreibung der Konstruktoren und Destruktoren

◆ File() [1/3]

ini::File::File ( const std::string & rsFullFileName,
TdOpenInifileMode openMode )

Konstruktor mit Parameterübergabe.

Parameter
[in]rsFullFileNameDie vollständige Dateibezeichnung wie z.B.: "c:\windows\system.ini".
[in]openModeWenn openMode auf ini::bCreateInifile, dann wird versucht, eine neue INI-Datei zu generieren.
Wenn openMode auf ini::bOpenInifile, dann wird versucht, die INI-Datei mit der übergebenen Bezeichnung einzulesen.
Ausnahmebehandlung
eut::ErrorNExceptionBei einem Fehler wird eine Exception geworfen.

◆ File() [2/3]

ini::File::File ( )

Standard-Konstruktor.

◆ ~File()

virtual ini::File::~File ( )
virtual

Destruktor, Speicher wird freigegeben.

◆ File() [3/3]

ini::File::File ( const File & )
private

Der Copy-Konstruktor steht nicht zur Verfügung.

Dokumentation der Elementfunktionen

◆ getAsBlob()

int ini::File::getAsBlob ( eut::Blob *& prBlob)

Liefert alle Einträge der Datei in einem Blob.

Parameter
[in,out]prBlobDas File als Blob.
Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.
Achtung
Die aufrufende Instanz muss das gelieferten Blob aus den Speicher entfernen.

◆ getFileName()

std::string ini::File::getFileName ( )

Liefert die Bezeichnung der INI-Datei.

Rückgabe
Die Bezeichnung.

◆ getKeyValue()

int ini::File::getKeyValue ( std::string & rsValue,
std::string * psSection,
std::string * psKey )

Liefert ein KeyValue.

Parameter
[in,out]rsValueDer gefundene KeyValue als std::string.
[in]psSection
  • Wenn gesetzt und psKey nicht gesetzt -> Fehler.
  • Wenn gesetzt und psKey auch gesetzt, wird der Key aus der psSection gesucht.
  • Wenn nicht gesetzt und psKey gesetzt, wird der Key ohne Sektion gesucht.
[in]psKeySiehe psSection.

Wenn es folgenden Eintrag in einer INI-Datei geben sollte:

[386enh]
woafont=app850.fon <- Variable mit Wert

liefert diese Funktion beim Aufruf von:

int t_iErr = pMyIniDatei->getKeyValue( sValue, "[386nh]", "woafont");

in rsValue "app850.fon" zurück.

Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.

◆ getLine() [1/2]

int ini::File::getLine ( Line *& prRetVal,
std::size_t nPosition,
ptl::SeekMode eMode )

Liefert einen Line aus der m_LineList in Bezug auf die ÜbergabeParameter.

Parameter
[in,out]prRetValübergebene Referenz in welche das gefundene Objekt "hineinkommt"
[in]nPositionOffset rellativ zur...
[in]eMode...Position

Parameterbeispiele:

  • Liefert erstes Element aus Liste: lPosition = 0; eMode = ptl::START
  • Liefert letztes Element aus Liste: lPosition = 0; eMode = ptl::END
  • Liefert aktuelles Element aus Liste: lPosition = 0; eMode = ptl::CURRENT_POS
  • Liefert nächstes Element aus Liste: lPosition = 1; eMode = ptl::CURRENT_POS
  • Liefert vorgeriges Element aus Liste: lPosition = -1; eMode = ptl::CURRENT_POS
Rückgabe
Eine Rückgabe < 0 zeigt einen Fehler an.
Achtung
Das gelieferte Objekt (in prRetVal) darf NICHT von der aufrufenden Instanz aus dem Speicher entfernt werden!

◆ getLine() [2/2]

int ini::File::getLine ( Line *& prRetVal,
std::string * psSection,
std::string * psKey = 0 )

Liefert eine Sektion oder einen Key.

Parameter
[in,out]prRetValWenn gefunden, in Abhängigkeit der anderen Parameter ein Zeiger auf ein Objekt vom Typ Section oder Key.
[in]psSection
  • Wenn gesetzt und psKey nicht gesetzt, wird die Section gesucht.
  • Wenn gesetzt und psKey auch gesetzt, wird der Key aus der psSection gesucht.
  • Wenn nicht gesetzt und psKey gesetzt, wird der Key ohne Sektion gesucht.
[in]psKeySiehe psSection.
Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.
Achtung
Das gelieferte Objekt in prRetVal darf NICHT von der aufrufenden Instanz aus dem Speicher entfernt werden!

◆ insertLine()

int ini::File::insertLine ( Line * pIniLine,
ptl::InsertPosition eInsertPosition = ptl::InsertCURRENT,
ptl::VH eBeforeBehind = ptl::BEHIND )

Fügt den übergebenen Eintrag in die m_LineList ein.

Parameter
[in]pIniLineDas einzufügende Objekt der Klasse Line
[in]eInsertPositionZeigt an, wohin das Datenobjekt in die Liste eingefügt werden soll. Mögliche Parameter sind ptl::InsertHEAD (am Anfang), ptl::InsertLAST (am Ende) oder ptl::InsertCURRENT (am aktuellen Element).
[in]eBeforeBehindGibt an, ob das neue Element vor oder hinter das, im Parameter eInsertPosition angegebene Element, gesetzt wird. Mögliche Parameter sind BEHIND und BEFORE
Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.
Achtung
ini::File übernimmt die Verantwortung für die in pIniLine übergebene Line. Das bedeutet, die aufrufende Instanz darf das übergebene Objekt nicht aus dem Speicher entfernen.

◆ makeLines()

int ini::File::makeLines ( eut::Blob * pBlob)

Erstellt aus übergebenen Blob die einzelnen Einträge in der INI-Datei.

Parameter
[in]pBlobDas Blob mit dem Inhalt einer Ini-Datei. Das Blob wird ausgewertet und alle Einträge erstellt.
Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.

◆ operator=()

File & ini::File::operator= ( const File & )
private

Zuweisungsoperator steht nicht zur Verfügung.

◆ refresh()

int ini::File::refresh ( )

Es wird die INI-Datei aus m_sFileName eingelesen.

Rückgabe
Eine Rückgabe < 0 zeigt einen Fehler an.
Warnung
Alle evtl. Änderungen werden verworfen.

◆ removeKey()

int ini::File::removeKey ( const std::string & rsSection,
const std::string & rsKey )

Entfernt einen Eintrag

Parameter
[in]rsSectionSektion
[in]rsKeyEintrag

Entfernt einen Eintrag folgendermassen:

  • Wenn Sektion übergeben, wird diese gesucht. Wenn vorhanden und Eintrag ist nicht gesetzt, wird diese aus der INI-Datei entfernt.
  • Wenn Eintrag übergeben, dann wird in der Sektion bzw. INI-Datei die Variable aus dem Eintrag gesucht. Wenn gefunden wird die Variable aus der Sektion bzw. INI-Datei entfernt.
Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.

◆ removeLine()

int ini::File::removeLine ( ptl::ElementPosition eFirstLastCurrent = ptl::CURRENT_POS)

Entfernt den Eintrag aus der m_LineList.

Wenn ein nachfolgendes Element vorhanden ist, wird dieses zum aktuellen Element, ansonsten das vorherige Element, wenn dieses vorhanden ist. Vom entfernten Eintrag wird der Destruktor aufgerufen.

Parameter
[in]eFirstLastCurrentFolgende Werte sind möglich: ptl::FIRST_POS, ptl::LAST_POS, (default) ptl::CURRENT_POS, ptl::NEXT_POS und ptl::PREVIOUS_POS.
Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.

◆ setFullFileName()

void ini::File::setFullFileName ( const std::string & rsFullFileName)

Setzt die Bezeichnung der INI-Datei.

Parameter
[in]rsFullFileNameDie vollständige Dateibezeichnung wie z.B.: "c:\windows\system.ini".

◆ setKeyValue()

int ini::File::setKeyValue ( const std::string & rsSection,
const std::string & rsKey,
const std::string & rsValue )

Setzt einen Eintrag

Parameter
[in]rsSectionDie Sektion entweder mit '[' und ']' oder ohne diesen beiden Zeichen.
[in]rsKeyDer Eintrag
[in]rsValueDer Wert

Setzt einen Eintrag folgendermassen:

  • Wenn Sektion übergeben wird, wird diese gesucht. Wenn nicht vorhanden, wird die Sektion angelegt. Wenn keine Sektion übergeben wird, ist die INI-Datei die Sektion.
  • Wenn Eintrag und Wert übergeben wird, wird in der Sektion die Variable aus dem Eintrag gesucht. Wenn diese gefundenwird, wird der Wert gesetzt, ansonsten wird die gesamte Variable mit Wert eingefügt.
Rückgabe
Ein Rückgabewert < 0 zeigt einen Fehler an.

◆ store()

int ini::File::store ( )

Es wird die INI-Datei aus m_sFileName gespeichert.

Rückgabe
Eine Rückgabe < 0 zeigt einen Fehler an.

Dokumentation der Datenelemente

◆ m_LineFactory

LineFactory ini::File::m_LineFactory
private

Die LineFactory liefert einen INI_Datei Eintrag typgerecht.

◆ m_LineList

ptl::TDVList<Line> ini::File::m_LineList
private

Liste der Einträge in dieser INI-Datei.

◆ m_sFileName

std::string ini::File::m_sFileName
private

In diesem Attribut wird die Bezeichnung der INI-Datei abgelegt.


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