1*cdf0e10cSrcweir #include <unicode/regex.h> 2*cdf0e10cSrcweir #include <unicode/unistr.h> 3*cdf0e10cSrcweir #include <string> 4*cdf0e10cSrcweir #include <fstream> 5*cdf0e10cSrcweir #include <iostream> 6*cdf0e10cSrcweir #include "inireader.hxx" 7*cdf0e10cSrcweir 8*cdf0e10cSrcweir using namespace std; 9*cdf0e10cSrcweir namespace transex3 10*cdf0e10cSrcweir { 11*cdf0e10cSrcweir 12*cdf0e10cSrcweir bool INIreader::read( INImap& myMap , string& filename ) 13*cdf0e10cSrcweir { 14*cdf0e10cSrcweir ifstream aFStream( filename.c_str() ); 15*cdf0e10cSrcweir if( aFStream && aFStream.is_open()) 16*cdf0e10cSrcweir { 17*cdf0e10cSrcweir string line; 18*cdf0e10cSrcweir string section; 19*cdf0e10cSrcweir string param_key; 20*cdf0e10cSrcweir string param_value; 21*cdf0e10cSrcweir stringmap* myvalues = 0; 22*cdf0e10cSrcweir 23*cdf0e10cSrcweir while( std::getline( aFStream , line ) ) 24*cdf0e10cSrcweir { 25*cdf0e10cSrcweir trim( line ); 26*cdf0e10cSrcweir if( line.empty() ){ 27*cdf0e10cSrcweir } 28*cdf0e10cSrcweir else if( is_section( line , section ) ) 29*cdf0e10cSrcweir { 30*cdf0e10cSrcweir //cerr << "[" << section << "]\n"; 31*cdf0e10cSrcweir myvalues = new stringmap(); 32*cdf0e10cSrcweir myMap[ section ] = myvalues ; 33*cdf0e10cSrcweir } 34*cdf0e10cSrcweir else if ( is_parameter( line , param_key , param_value ) ) 35*cdf0e10cSrcweir { 36*cdf0e10cSrcweir //cerr << "" << param_key << " = " << param_value << "\n"; 37*cdf0e10cSrcweir if( myvalues ) 38*cdf0e10cSrcweir { 39*cdf0e10cSrcweir (*myvalues)[ param_key ] = param_value ; 40*cdf0e10cSrcweir } 41*cdf0e10cSrcweir else 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir cerr << "ERROR: The INI file " << filename << " appears to be broken ... parameters without a section?!?\n"; 44*cdf0e10cSrcweir if( aFStream.is_open() ) aFStream.close(); 45*cdf0e10cSrcweir return false; 46*cdf0e10cSrcweir } 47*cdf0e10cSrcweir } 48*cdf0e10cSrcweir } 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir if( aFStream.is_open() ) 51*cdf0e10cSrcweir aFStream.close(); 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir return true; 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir else 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir cerr << "ERROR: Can't open file '" << filename << "'\n"; 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir return false; 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir bool INIreader::is_section( string& line , string& section_str ) 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir // Error in regex ? 65*cdf0e10cSrcweir check_status( section_status ); 66*cdf0e10cSrcweir UnicodeString target( line.c_str() , line.length() ); 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir section_match->reset( target ); 69*cdf0e10cSrcweir check_status( section_status ); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir if( section_match->find() ) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir check_status( section_status ); 74*cdf0e10cSrcweir UnicodeString result( section_match->group( 1 , section_status) ); 75*cdf0e10cSrcweir check_status( section_status ); 76*cdf0e10cSrcweir toStlString( result , section_str ); 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir return true; 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir return false; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir bool INIreader::is_parameter( string& line , string& parameter_key , string& parameter_value ) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir // Error in regex ? 86*cdf0e10cSrcweir check_status( parameter_status ); 87*cdf0e10cSrcweir UnicodeString target( line.c_str() , line.length() ); 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir parameter_match->reset( target ); 90*cdf0e10cSrcweir check_status( parameter_status ); 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir if( parameter_match->find() ) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir check_status( parameter_status ); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir UnicodeString result1( parameter_match->group( 1 , parameter_status) ); 97*cdf0e10cSrcweir check_status( parameter_status ); 98*cdf0e10cSrcweir toStlString( result1 , parameter_key ); 99*cdf0e10cSrcweir UnicodeString result2( parameter_match->group( 2 , parameter_status) ); 100*cdf0e10cSrcweir check_status( parameter_status ); 101*cdf0e10cSrcweir toStlString( result2 , parameter_value ); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir return true; 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir return false; 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir void INIreader::check_status( UErrorCode status ) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir if( U_FAILURE( status) ) 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir cerr << "Error in or while using regex: " << u_errorName( status ) << "\n"; 113*cdf0e10cSrcweir exit(-1); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir void INIreader::toStlString( const UnicodeString& str , string& stl_str) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir // convert to string 120*cdf0e10cSrcweir char* buffer = new char[ str.length()*3 ]; 121*cdf0e10cSrcweir str.extract( 0 , str.length() , buffer ); 122*cdf0e10cSrcweir stl_str = string( buffer ); 123*cdf0e10cSrcweir delete [] buffer; 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir void INIreader::trim( string& str ) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir string str1 = str.substr( 0 , str.find_last_not_of(' ') + 1 ); 129*cdf0e10cSrcweir str = str1.empty() ? str1 : str1.substr( str1.find_first_not_of(' ') ); 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir } 133