1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 #include <unicode/regex.h> 23 #include <unicode/unistr.h> 24 #include <cstdlib> 25 #include <string> 26 #include <fstream> 27 #include <iostream> 28 #include "inireader.hxx" 29 30 using namespace std; 31 namespace transex3 32 { 33 34 bool INIreader::read( INImap& myMap , string& filename ) 35 { 36 ifstream aFStream( filename.c_str() ); 37 if( aFStream && aFStream.is_open()) 38 { 39 string line; 40 string section; 41 string param_key; 42 string param_value; 43 stringmap* myvalues = 0; 44 45 while( std::getline( aFStream , line ) ) 46 { 47 trim( line ); 48 if( line.empty() ){ 49 } 50 else if( is_section( line , section ) ) 51 { 52 //cerr << "[" << section << "]\n"; 53 myvalues = new stringmap(); 54 myMap[ section ] = myvalues ; 55 } 56 else if ( is_parameter( line , param_key , param_value ) ) 57 { 58 //cerr << "" << param_key << " = " << param_value << "\n"; 59 if( myvalues ) 60 { 61 (*myvalues)[ param_key ] = param_value ; 62 } 63 else 64 { 65 cerr << "ERROR: The INI file " << filename << " appears to be broken ... parameters without a section?!?\n"; 66 if( aFStream.is_open() ) aFStream.close(); 67 return false; 68 } 69 } 70 } 71 72 if( aFStream.is_open() ) 73 aFStream.close(); 74 75 return true; 76 } 77 else 78 { 79 cerr << "ERROR: Can't open file '" << filename << "'\n"; 80 } 81 return false; 82 } 83 84 bool INIreader::is_section( string& line , string& section_str ) 85 { 86 // Error in regex ? 87 check_status( section_status ); 88 UnicodeString target( line.c_str() , line.length() ); 89 90 section_match->reset( target ); 91 check_status( section_status ); 92 93 if( section_match->find() ) 94 { 95 check_status( section_status ); 96 UnicodeString result( section_match->group( 1 , section_status) ); 97 check_status( section_status ); 98 toStlString( result , section_str ); 99 100 return true; 101 } 102 return false; 103 } 104 105 bool INIreader::is_parameter( string& line , string& parameter_key , string& parameter_value ) 106 { 107 // Error in regex ? 108 check_status( parameter_status ); 109 UnicodeString target( line.c_str() , line.length() ); 110 111 parameter_match->reset( target ); 112 check_status( parameter_status ); 113 114 if( parameter_match->find() ) 115 { 116 check_status( parameter_status ); 117 118 UnicodeString result1( parameter_match->group( 1 , parameter_status) ); 119 check_status( parameter_status ); 120 toStlString( result1 , parameter_key ); 121 UnicodeString result2( parameter_match->group( 2 , parameter_status) ); 122 check_status( parameter_status ); 123 toStlString( result2 , parameter_value ); 124 125 return true; 126 } 127 return false; 128 } 129 130 void INIreader::check_status( UErrorCode status ) 131 { 132 if( U_FAILURE( status) ) 133 { 134 cerr << "Error in or while using regex: " << u_errorName( status ) << "\n"; 135 exit(-1); 136 } 137 } 138 139 void INIreader::toStlString( const UnicodeString& str , string& stl_str) 140 { 141 // convert to string 142 char* buffer = new char[ str.length()*3 ]; 143 str.extract( 0 , str.length() , buffer ); 144 stl_str = string( buffer ); 145 delete [] buffer; 146 } 147 148 void INIreader::trim( string& str ) 149 { 150 string str1 = str.substr( 0 , str.find_last_not_of(' ') + 1 ); 151 str = str1.empty() ? str1 : str1.substr( str1.find_first_not_of(' ') ); 152 } 153 154 } 155