summaryrefslogtreecommitdiff
path: root/transex3/source/inireader.cxx
blob: 1ff34fad8e95d41f691035cf9ba0bb0f0df98188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <unicode/regex.h>
#include <unicode/unistr.h>
#include <string>
#include <fstream>
#include <iostream>
#include "inireader.hxx"

using namespace std;
namespace transex3
{

bool INIreader::read( INImap& myMap , string& filename )
{
    ifstream aFStream( filename.c_str() );
    if( aFStream && aFStream.is_open())
    {
        string line;
        string section;
        string param_key;
        string param_value;
        stringmap* myvalues = 0;

        while( std::getline( aFStream , line ) )
        {
            trim( line );
            if( line.empty() ){
            }
            else if( is_section( line , section ) )
            {
                //cerr << "[" << section << "]\n";
                myvalues = new stringmap();
                myMap[ section ] = myvalues ;
            }
            else if ( is_parameter( line , param_key , param_value ) )
            {
                //cerr << "" << param_key << " = " << param_value << "\n";
                if( myvalues )
                {
                    (*myvalues)[ param_key ] = param_value ;
                }
                else
                {
                    cerr << "ERROR: The INI file " << filename << " appears to be broken ... parameters without a section?!?\n";
                    if( aFStream.is_open() ) aFStream.close();
                    return false;
                }
            }
        }

        if( aFStream.is_open() )
            aFStream.close();

        return true;
    }
    else
    {
        cerr << "ERROR: Can't open file '" << filename << "'\n";
    }
    return false;
}

bool INIreader::is_section( string& line , string& section_str )
{
    // Error in regex ?
    check_status( section_status );
    UnicodeString target( line.c_str() , line.length() );

    section_match->reset( target );
    check_status( section_status );

    if( section_match->find() )
    {
        check_status( section_status );
        UnicodeString result(  section_match->group( 1 , section_status) );
        check_status( section_status );
        toStlString( result , section_str );

        return true;
    }
    return false;
}

bool INIreader::is_parameter( string& line , string& parameter_key , string& parameter_value )
{
    // Error in regex ?
    check_status( parameter_status );
    UnicodeString target( line.c_str() , line.length() );

    parameter_match->reset( target );
    check_status( parameter_status );

    if( parameter_match->find() )
    {
        check_status( parameter_status );

        UnicodeString result1(  parameter_match->group( 1 , parameter_status) );
        check_status( parameter_status );
        toStlString( result1 , parameter_key );
        UnicodeString result2(  parameter_match->group( 2 , parameter_status) );
        check_status( parameter_status );
        toStlString( result2 , parameter_value );

        return true;
    }
    return false;
}

void INIreader::check_status( UErrorCode status )
{
    if( U_FAILURE( status) )
    {
        cerr << "Error in or while using regex: " << u_errorName( status ) << "\n";
        exit(-1);
    }
}

void INIreader::toStlString( const UnicodeString& str , string& stl_str)
{
         // convert to string
        char* buffer = new char[ str.length()*3 ];
        str.extract( 0 , str.length() , buffer );
        stl_str = string( buffer );
        delete[] buffer;
}

void INIreader::trim( string& str )
{
    string str1 = str.substr( 0 , str.find_last_not_of(' ') + 1 );
    str = str1.empty() ? str1 : str1.substr( str1.find_first_not_of(' ') );
}

}