Public Member Functions | Private Attributes

nitro::INIFile Class Reference

Class allows reading data from INI files. More...

#include <loaders/ini_file.h>

Collaboration diagram for nitro::INIFile:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 INIFile (void)
 INIFile (const char *thePath)
 INIFile (const std::string &thePath)
void LoadINIFile (const char *thePath)
void LoadINIFile (const std::string &thePath)
void SetPath (const char *thePath)
void SetPath (const std::string &thePath)
const char * GetPath (void) const
const char * GetString (const char *Section, const char *Key, char *Value, const char *Default=NULL) const
void SetString (const char *Section, const char *Key, const char *Value)
void SaveINIFile (const char *thePath=NULL)
void SaveINIFile (std::string &thePath)
virtual ~INIFile ()

Private Attributes

std::string Path
std::vector< std::string > FileData
bool NeedSaveFile

Detailed Description

Class allows reading data from INI files.

Author:
Dodonov A.A.

Definition at line 50 of file ini_file.h.


Constructor & Destructor Documentation

nitro::INIFile::INIFile ( void   ) 

Default constructor.

Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 11 of file ini_file.cpp.

References nitro::exception::code(), NeedSaveFile, Path, and nitro::exception::what().

        {
                try
                {
                        Path = "";
                        NeedSaveFile = false;
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::INIFile( void )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::INIFile( void )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

nitro::INIFile::INIFile ( const char *  thePath  ) 

Constructor.

Parameters:
thePath - Path to the loading file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 28 of file ini_file.cpp.

References nitro::exception::code(), LoadINIFile(), NeedSaveFile, and nitro::exception::what().

        {
                try
                {
                        NeedSaveFile = false;
                        LoadINIFile( thePath );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::INIFile( const char * thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::INIFile( const char * thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

nitro::INIFile::INIFile ( const std::string &  thePath  ) 

Constructor.

Parameters:
thePath - Path to the loading file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 45 of file ini_file.cpp.

References nitro::exception::code(), LoadINIFile(), NeedSaveFile, and nitro::exception::what().

        {
                try
                {
                        NeedSaveFile = false;
                        LoadINIFile( thePath );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::INIFile( const std::string & thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::INIFile( const std::string & thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

nitro::INIFile::~INIFile (  )  [virtual]

Destructor.

Author:
Dodonov A.A.

Definition at line 295 of file ini_file.cpp.

        {
                try
                {
                }
                catch( nitro::exception e )
                {
                }
                catch( ... )
                {
                }
        }


Member Function Documentation

const char * nitro::INIFile::GetPath ( void   )  const

Function returns path to the file.

Returns:
Path to the loaded file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 136 of file ini_file.cpp.

References Path.

        {
                return( Path.c_str() );
        }

const char * nitro::INIFile::GetString ( const char *  Section,
const char *  Key,
char *  Value,
const char *  Default = NULL 
) const

Function returns data from the loaded file.

Parameters:
Section - Section.
Key - Key.
Value - Data from the file.
Default - Default value if the key was not found.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 141 of file ini_file.cpp.

References nitro::exception::code(), nitro::Parsers::ExplodeString(), FileData, and nitro::exception::what().

        {
                try
                {
                        bool                            SectionStarted( false );
                        
                        for( std::size_t i( 0 ) ; i < FileData.size() ; i++ )
                        {
                                if( FileData[ i ] == std::string( "[" ) + Section + "]" )
                                {
                                        SectionStarted = true;
                                        continue;
                                }
                                
                                if( SectionStarted && FileData[ i ][ 0 ] == '[' )
                                {
                                        break;
                                }
                                
                                if( SectionStarted )
                                {
                                        std::vector< std::string >              Segments;
                                        nitro::Parsers::ExplodeString( FileData[ i ] , Segments , '=' );

                                        if( SectionStarted && Segments[ 0 ] == Key )
                                        {
                                                strcpy( Value , Segments[ 1 ].c_str() );
                                                return( Value );
                                        }
                                }
                        }
                        
                        strcpy( Value , Default );
                        return( Value );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::GetString( const char * Section , const char * Key , const char * Value , const char * Default /* = NULL */ )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::GetString( const char * Section , const char * Key , const char * Value , const char * Default /* = NULL */ )::An error occured" ) , 1 ) );
                }

                return( NULL );
        }

Here is the call graph for this function:

void nitro::INIFile::LoadINIFile ( const char *  thePath  ) 

Function loads INI files.

Parameters:
thePath - Path to the loading file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 62 of file ini_file.cpp.

References nitro::BinaryData::AppendData(), nitro::exception::code(), nitro::Parsers::ExplodeString(), FileData, nitro::BinaryData::GetBuffer(), nitro::File::LoadBinDataFromFile(), Path, nitro::BinaryData::ReplaceBuffer(), and nitro::exception::what().

Referenced by INIFile(), and LoadINIFile().

        {
                try
                {
                        FileData.erase( FileData.begin() , FileData.end() );
                        Path = thePath;
                        nitro::BinaryData               Data;
                        nitro::File::LoadBinDataFromFile( Data , thePath );
                        Data.AppendData( "\0" , 1 );
                        nitro::BinaryData::ReplaceBuffer( Data , "\r\n" , 2 , "\n" , 1 );
                        nitro::Parsers::ExplodeString( Data.GetBuffer() , FileData , '\n' );
                        if( FileData.size() == 1 )
                        {
                                FileData.clear();
                        }
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

void nitro::INIFile::LoadINIFile ( const std::string &  thePath  ) 

Function loads INI files.

Parameters:
thePath - Path to the loading file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 88 of file ini_file.cpp.

References nitro::exception::code(), LoadINIFile(), and nitro::exception::what().

        {
                try
                {
                        LoadINIFile( thePath.c_str() );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

void nitro::INIFile::SaveINIFile ( const char *  thePath = NULL  ) 

Function saves file.

Parameters:
thePath - Path to the saving file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 244 of file ini_file.cpp.

References nitro::BinaryData::AppendData(), nitro::exception::code(), FileData, Path, nitro::File::SaveBinDataToFile(), and nitro::exception::what().

Referenced by SaveINIFile().

        {
                try
                {
                        nitro::BinaryData               Data;

                        for( std::size_t i( 0 ) ; i < FileData.size() ; i++ )
                        {
                                Data.AppendData( FileData[ i ] );

                                if( i + 1 != FileData.size() )
                                {
                                        Data.AppendData( "\r\n" );
                                }
                        }

                        if( NULL == thePath || std::string( "" ) == thePath )
                        {
                                nitro::File::SaveBinDataToFile( Data , Path );
                        }
                        else
                        {
                                nitro::File::SaveBinDataToFile( Data , thePath );
                        }
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

void nitro::INIFile::SaveINIFile ( std::string &  thePath  ) 

Function saves file.

Parameters:
thePath - Path to the saving file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 279 of file ini_file.cpp.

References nitro::exception::code(), SaveINIFile(), and nitro::exception::what().

        {
                try
                {
                        SaveINIFile( thePath.c_str() );
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

void nitro::INIFile::SetPath ( const char *  thePath  ) 

Function sets new path to the file.

Parameters:
thePath - Path to the loading file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 104 of file ini_file.cpp.

References nitro::exception::code(), Path, and nitro::exception::what().

        {
                try
                {
                        Path = thePath;
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::SetPath( const char * thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::SetPath( const char * thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

void nitro::INIFile::SetPath ( const std::string &  thePath  ) 

Function sets new path to the file.

Parameters:
thePath - Path to the loading file.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 120 of file ini_file.cpp.

References nitro::exception::code(), Path, and nitro::exception::what().

        {
                try
                {
                        Path = thePath;
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::SetPath( const std::string & thePath )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::SetPath( const std::string & thePath )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:

void nitro::INIFile::SetString ( const char *  Section,
const char *  Key,
const char *  Value 
)

Function saves value.

Parameters:
Section - Section.
Key - Key.
Value - Value.
Exceptions:
nitro::exception Throws an exception of that type with the error description.
Author:
Dodonov A.A.

Definition at line 188 of file ini_file.cpp.

References nitro::exception::code(), nitro::Parsers::ExplodeString(), FileData, NeedSaveFile, and nitro::exception::what().

        {
                try
                {
                        bool                            SectionStarted( false );
                        
                        std::vector< std::string >::iterator iter( FileData.begin() );
                        for( ; iter != FileData.end() ; ++iter )
                        {
                                if( *iter == std::string( "[" ) + Section + "]" )
                                {
                                        SectionStarted = true;
                                        continue;
                                }

                                if( SectionStarted && ( *iter )[ 0 ] == '[' )
                                {
                                        break;
                                }
                                
                                if( SectionStarted )
                                {
                                        std::vector< std::string >      Segments;
                                        nitro::Parsers::ExplodeString( *iter , Segments , '=' );

                                        if( SectionStarted && Segments[ 0 ] == Key )
                                        {
                                                *iter = std::string( Key ) + "=" + Value;
                                                NeedSaveFile = true;
                                                return;
                                        }
                                }
                        }

                        if( SectionStarted )
                        {
                                FileData.insert( iter , std::string( Key ) + "=" + Value );
                        }
                        else
                        {
                                FileData.push_back( std::string( "[" ) + Section + "]" );
                                FileData.push_back( std::string( Key ) + "=" + Value );
                        }
                        
                        NeedSaveFile = true;
                }
                catch( nitro::exception e )
                {
                        throw( nitro::exception( std::string( "INIFile::SetString( const char * Section , const char * Key, const char * Value )::" ) + e.what() , e.code() ) );
                }
                catch( ... )
                {
                        throw( nitro::exception( std::string( "INIFile::SetString( const char * Section , const char * Key, const char * Value )::An error occured" ) , 1 ) );
                }
        }

Here is the call graph for this function:


Member Data Documentation

std::vector< std::string > nitro::INIFile::FileData [private]

INI file data.

Author:
Dodonov A.A.

Definition at line 350 of file ini_file.h.

Referenced by GetString(), LoadINIFile(), SaveINIFile(), and SetString().

Should file be saved.

Author:
Dodonov A.A.

Definition at line 362 of file ini_file.h.

Referenced by INIFile(), and SetString().

std::string nitro::INIFile::Path [private]

Path to the file.

Author:
Dodonov A.A.

Definition at line 338 of file ini_file.h.

Referenced by GetPath(), INIFile(), LoadINIFile(), SaveINIFile(), and SetPath().


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

Generated by  doxygen 1.6.1