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