00001 #ifndef __INI_FILE_CPP__
00002 #define __INI_FILE_CPP__
00003
00004 #include <loaders/ini_file.h>
00005 #include <utilities/binary_data.h>
00006 #include <utilities/file_utilities.h>
00007 #include <utilities/string_utilities.h>
00008
00009 namespace nitro
00010 {
00011 INIFile::INIFile( void )
00012 {
00013 try
00014 {
00015 Path = "";
00016 NeedSaveFile = false;
00017 }
00018 catch( nitro::exception e )
00019 {
00020 throw( nitro::exception( std::string( "INIFile::INIFile( void )::" ) + e.what() , e.code() ) );
00021 }
00022 catch( ... )
00023 {
00024 throw( nitro::exception( std::string( "INIFile::INIFile( void )::An error occured" ) , 1 ) );
00025 }
00026 }
00027
00028 INIFile::INIFile( const char * thePath )
00029 {
00030 try
00031 {
00032 NeedSaveFile = false;
00033 LoadINIFile( thePath );
00034 }
00035 catch( nitro::exception e )
00036 {
00037 throw( nitro::exception( std::string( "INIFile::INIFile( const char * thePath )::" ) + e.what() , e.code() ) );
00038 }
00039 catch( ... )
00040 {
00041 throw( nitro::exception( std::string( "INIFile::INIFile( const char * thePath )::An error occured" ) , 1 ) );
00042 }
00043 }
00044
00045 INIFile::INIFile( const std::string & thePath )
00046 {
00047 try
00048 {
00049 NeedSaveFile = false;
00050 LoadINIFile( thePath );
00051 }
00052 catch( nitro::exception e )
00053 {
00054 throw( nitro::exception( std::string( "INIFile::INIFile( const std::string & thePath )::" ) + e.what() , e.code() ) );
00055 }
00056 catch( ... )
00057 {
00058 throw( nitro::exception( std::string( "INIFile::INIFile( const std::string & thePath )::An error occured" ) , 1 ) );
00059 }
00060 }
00061
00062 void INIFile::LoadINIFile( const char * thePath )
00063 {
00064 try
00065 {
00066 FileData.erase( FileData.begin() , FileData.end() );
00067 Path = thePath;
00068 nitro::BinaryData Data;
00069 nitro::File::LoadBinDataFromFile( Data , thePath );
00070 Data.AppendData( "\0" , 1 );
00071 nitro::BinaryData::ReplaceBuffer( Data , "\r\n" , 2 , "\n" , 1 );
00072 nitro::Parsers::ExplodeString( Data.GetBuffer() , FileData , '\n' );
00073 if( FileData.size() == 1 )
00074 {
00075 FileData.clear();
00076 }
00077 }
00078 catch( nitro::exception e )
00079 {
00080 throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::" ) + e.what() , e.code() ) );
00081 }
00082 catch( ... )
00083 {
00084 throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::An error occured" ) , 1 ) );
00085 }
00086 }
00087
00088 void INIFile::LoadINIFile( const std::string & thePath )
00089 {
00090 try
00091 {
00092 LoadINIFile( thePath.c_str() );
00093 }
00094 catch( nitro::exception e )
00095 {
00096 throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::" ) + e.what() , e.code() ) );
00097 }
00098 catch( ... )
00099 {
00100 throw( nitro::exception( std::string( "INIFile::LoadINIFile( const char * thePath )::An error occured" ) , 1 ) );
00101 }
00102 }
00103
00104 void INIFile::SetPath( const char * thePath )
00105 {
00106 try
00107 {
00108 Path = thePath;
00109 }
00110 catch( nitro::exception e )
00111 {
00112 throw( nitro::exception( std::string( "INIFile::SetPath( const char * thePath )::" ) + e.what() , e.code() ) );
00113 }
00114 catch( ... )
00115 {
00116 throw( nitro::exception( std::string( "INIFile::SetPath( const char * thePath )::An error occured" ) , 1 ) );
00117 }
00118 }
00119
00120 void INIFile::SetPath( const std::string & thePath )
00121 {
00122 try
00123 {
00124 Path = thePath;
00125 }
00126 catch( nitro::exception e )
00127 {
00128 throw( nitro::exception( std::string( "INIFile::SetPath( const std::string & thePath )::" ) + e.what() , e.code() ) );
00129 }
00130 catch( ... )
00131 {
00132 throw( nitro::exception( std::string( "INIFile::SetPath( const std::string & thePath )::An error occured" ) , 1 ) );
00133 }
00134 }
00135
00136 const char * INIFile::GetPath( void ) const
00137 {
00138 return( Path.c_str() );
00139 }
00140
00141 const char * INIFile::GetString( const char * Section , const char * Key , char * Value , const char * Default ) const
00142 {
00143 try
00144 {
00145 bool SectionStarted( false );
00146
00147 for( std::size_t i( 0 ) ; i < FileData.size() ; i++ )
00148 {
00149 if( FileData[ i ] == std::string( "[" ) + Section + "]" )
00150 {
00151 SectionStarted = true;
00152 continue;
00153 }
00154
00155 if( SectionStarted && FileData[ i ][ 0 ] == '[' )
00156 {
00157 break;
00158 }
00159
00160 if( SectionStarted )
00161 {
00162 std::vector< std::string > Segments;
00163 nitro::Parsers::ExplodeString( FileData[ i ] , Segments , '=' );
00164
00165 if( SectionStarted && Segments[ 0 ] == Key )
00166 {
00167 strcpy( Value , Segments[ 1 ].c_str() );
00168 return( Value );
00169 }
00170 }
00171 }
00172
00173 strcpy( Value , Default );
00174 return( Value );
00175 }
00176 catch( nitro::exception e )
00177 {
00178 throw( nitro::exception( std::string( "INIFile::GetString( const char * Section , const char * Key , const char * Value , const char * Default /* = NULL */ )::" ) + e.what() , e.code() ) );
00179 }
00180 catch( ... )
00181 {
00182 throw( nitro::exception( std::string( "INIFile::GetString( const char * Section , const char * Key , const char * Value , const char * Default /* = NULL */ )::An error occured" ) , 1 ) );
00183 }
00184
00185 return( NULL );
00186 }
00187
00188 void INIFile::SetString( const char * Section , const char * Key, const char * Value )
00189 {
00190 try
00191 {
00192 bool SectionStarted( false );
00193
00194 std::vector< std::string >::iterator iter( FileData.begin() );
00195 for( ; iter != FileData.end() ; ++iter )
00196 {
00197 if( *iter == std::string( "[" ) + Section + "]" )
00198 {
00199 SectionStarted = true;
00200 continue;
00201 }
00202
00203 if( SectionStarted && ( *iter )[ 0 ] == '[' )
00204 {
00205 break;
00206 }
00207
00208 if( SectionStarted )
00209 {
00210 std::vector< std::string > Segments;
00211 nitro::Parsers::ExplodeString( *iter , Segments , '=' );
00212
00213 if( SectionStarted && Segments[ 0 ] == Key )
00214 {
00215 *iter = std::string( Key ) + "=" + Value;
00216 NeedSaveFile = true;
00217 return;
00218 }
00219 }
00220 }
00221
00222 if( SectionStarted )
00223 {
00224 FileData.insert( iter , std::string( Key ) + "=" + Value );
00225 }
00226 else
00227 {
00228 FileData.push_back( std::string( "[" ) + Section + "]" );
00229 FileData.push_back( std::string( Key ) + "=" + Value );
00230 }
00231
00232 NeedSaveFile = true;
00233 }
00234 catch( nitro::exception e )
00235 {
00236 throw( nitro::exception( std::string( "INIFile::SetString( const char * Section , const char * Key, const char * Value )::" ) + e.what() , e.code() ) );
00237 }
00238 catch( ... )
00239 {
00240 throw( nitro::exception( std::string( "INIFile::SetString( const char * Section , const char * Key, const char * Value )::An error occured" ) , 1 ) );
00241 }
00242 }
00243
00244 void INIFile::SaveINIFile( const char * thePath )
00245 {
00246 try
00247 {
00248 nitro::BinaryData Data;
00249
00250 for( std::size_t i( 0 ) ; i < FileData.size() ; i++ )
00251 {
00252 Data.AppendData( FileData[ i ] );
00253
00254 if( i + 1 != FileData.size() )
00255 {
00256 Data.AppendData( "\r\n" );
00257 }
00258 }
00259
00260 if( NULL == thePath || std::string( "" ) == thePath )
00261 {
00262 nitro::File::SaveBinDataToFile( Data , Path );
00263 }
00264 else
00265 {
00266 nitro::File::SaveBinDataToFile( Data , thePath );
00267 }
00268 }
00269 catch( nitro::exception e )
00270 {
00271 throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::" ) + e.what() , e.code() ) );
00272 }
00273 catch( ... )
00274 {
00275 throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::An error occured" ) , 1 ) );
00276 }
00277 }
00278
00279 void INIFile::SaveINIFile( std::string & thePath )
00280 {
00281 try
00282 {
00283 SaveINIFile( thePath.c_str() );
00284 }
00285 catch( nitro::exception e )
00286 {
00287 throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::" ) + e.what() , e.code() ) );
00288 }
00289 catch( ... )
00290 {
00291 throw( nitro::exception( std::string( "INIFile::SaveINIFile( std::string & thePath )::An error occured" ) , 1 ) );
00292 }
00293 }
00294
00295 INIFile::~INIFile()
00296 {
00297 try
00298 {
00299 }
00300 catch( nitro::exception e )
00301 {
00302 }
00303 catch( ... )
00304 {
00305 }
00306 }
00307
00308 BEGIN_TESTING_SECTION()
00309
00310 ENABLE_CLASS_TESTING( INIFile )
00311
00312 CLASS_MEMBER_FUNCTION_TESTING_1( INIFile , aliasLoadINIFile , tstLoadINIFile , const char * , void , NO_RET )
00313 CLASS_MEMBER_FUNCTION_TESTING_1( INIFile , aliasSaveINIFile , tstSaveINIFile , const char * , void , NO_RET )
00314 CLASS_MEMBER_FUNCTION_TESTING_4( INIFile , GetString , tstGetString , const char * , const char * , char * , const char * , const char * , const char * )
00315 CLASS_MEMBER_FUNCTION_TESTING_3( INIFile , SetString , tstSetString , const char * , const char * , const char * , void , NO_RET )
00316 END_TESTING_SECTION()
00317 };
00318
00319 #endif