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