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