File: ConfPars.cpp

package info (click to toggle)
capi4hylafax 1%3A01.02.03-10sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,856 kB
  • ctags: 2,966
  • sloc: cpp: 20,126; sh: 7,703; makefile: 215; perl: 36
file content (285 lines) | stat: -rw-r--r-- 9,505 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*---------------------------------------------------------------------------*\

    Copyright (C) 2000 AVM GmbH. All rights reserved.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY, without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, and WITHOUT
    ANY LIABILITY FOR ANY DAMAGES arising out of or in connection
    with the use or performance of this software. See the
    GNU General Public License for further details.

\*---------------------------------------------------------------------------*/

#ifndef _CONFPARS_CPP_
#define _CONFPARS_CPP_

#include "ConfPars.h"
#include "dbgSTD.h"

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

enum GrammarParseState {
    ps_SearchName,
    ps_SearchAssign,
    ps_SearchData,
    ps_SearchEnd
};

/*===========================================================================*\
\*===========================================================================*/

CConfigGrammarParser::CConfigGrammarParser (CConfigParserSection *pMainSection) {
    dhead ("CConfigGrammarParser-Constructor", DCON_ConfigParser);
    if (!pMainSection) {
        pMainSection = new CConfigParserSection;
    }
    pCurSec = pMainSec = pMainSection;
}

/*===========================================================================*\
\*===========================================================================*/

CConfigGrammarParser::~CConfigGrammarParser (void) {
    dhead ("CConfigGrammarParser-Destructor", DCON_ConfigParser);
    // TODO?: delete is not always the best idea here
    delete pMainSec;
    pMainSec = 0;
}

/*===========================================================================*\
\*===========================================================================*/

tSInfo CConfigGrammarParser::ParseLine (CDynamicString *StrLine) {
    dhead ("CConfigGrammarParser::ParseLine", DCON_ConfigParser);
    tSInfo fret = i_Okay;
    if ((StrLine) && (CConfigLineParser::ParseLine (StrLine) == vFalse)) {
        return iErr_OutOfMemory;
    }
    CMultiString *wordList = GetWordList();
    dwarning (wordList != 0);
    if (wordList) {
        fret = ParseWords (wordList);
        if ((fret != i_Pending) || (!StrLine)) {
            wordList->RemoveAndDeleteAll();
        }
    }
    return fret;
}

/*===========================================================================*\
\*===========================================================================*/

tSInfo CConfigGrammarParser::ParseWords (CMultiString *WordList) {
    dhead ("CConfigGrammarParser::ParseWordsX", DCON_ConfigParser);
    dassert (WordList != 0);
    COneMultiString *pPStr;

    // Test for line concernator '\\'
    pPStr = WordList->GetLast();
    if (!pPStr) {
        dprint (" WordList is EMPTY ");
        RETURN ('x', i_Okay);
    }
    dassert (pPStr->GetLen() > 0);
    while (pPStr->GetChar (0) == '#') {
        pPStr = pPStr->GetPrev();
        if (!pPStr) {
            RETURN ('x', i_Okay);
        }
    }
    if ((pPStr->GetLen() == 1) && (pPStr->GetChar (0) == '\\')) {
        // found line concernator and all later comments
        COneMultiString *delPStr;
        while (pPStr) {
            delPStr = pPStr;
            pPStr   = pPStr->GetNext();
            delete delPStr;
        }
        RETURN ('x', i_Pending);
    }

    // parse words
    tBool               InRange    = vFalse;
    CConfigParserValue *pCurValue  = 0;
    GrammarParseState   ParseState = ps_SearchName;

    // parse current string list
    while ((pPStr = WordList->GetFirst()) != 0) {
        dassert (pPStr->GetLen() > 0);
        dassert (pCurSec != 0);
        //dprint ("CurWord=%s ", pPStr->GetPointer());
        switch (pPStr->GetChar (0)) {
        case '{': {
            dprint ("Found: Section OPEN\n");
            dassert (pPStr->GetLen() == 1);
            if (pCurValue) {
                if (pCurValue->DataList.IsEmpty() == vFalse) {
                    pCurSec->ValueList.AddLast (pCurValue);
                } else  {
                    delete pCurValue;
                }
                pCurValue = 0;
            }
            CConfigParserSection *newSec = new CConfigParserSection;
            if (!newSec) {
                return iErr_OutOfMemory;
            }
            newSec->pUpperSection = pCurSec;
            pCurSec->SectionList.AddLast (newSec);
            pCurSec = newSec;
            InRange = vFalse;
            break;
        }
        case '}':
            dprint ("Found: Section CLOSE\n");
            dassert (pPStr->GetLen() == 1);
            if (pCurValue) {
                if (pCurValue->DataList.IsEmpty() == vFalse) {
                    pCurSec->ValueList.AddLast (pCurValue);
                } else  {
                    delete pCurValue;
                }
                pCurValue = 0;
            }
            if (pCurSec->pUpperSection == 0) {
                return iErr_IniFile_TwoMuchSectionEnds;
            }
            pCurSec = pCurSec->pUpperSection;
            InRange = vFalse;
            break;

        case '"':
            // Remove "
            if ((pPStr->GetLen() < 2) || (pPStr->GetLastChar() != '"')) {
                // do nothing !!!!

                //delete pCurValue;
                //return i_Err_QuotationError;
            } else {
                pPStr->CutLen (1);
                pPStr->Remove (0, 1);
            }
            // TODO?: Remove \ from \" ?
            // no break!

        default:
            if (!pCurValue) {
                pCurValue = new CConfigParserValue;
                if (!pCurValue) {
                    delete pCurValue;
                    return iErr_OutOfMemory;
                }
                dprint ("Name=%s ", pPStr->GetPointer());
                pCurValue->Name.Set (pPStr);
                ParseState = ps_SearchAssign;
            } else {
                switch (ParseState) {
                case ps_SearchName:
                case ps_SearchAssign:
                case ps_SearchEnd:
                    ParseState = ps_SearchName;
                    delete pCurValue;
                    pCurValue = 0;
                    return iErr_IniFile_WrongAssignCoding;
                case ps_SearchData:
                    if (InRange == vTrue) {
                        COneMultiString *pLast = pCurValue->DataList.GetLast();
                        dassert (pLast != 0);
                        pLast->Append (pPStr);
                        InRange = vFalse;
                    } else {
                        dprint ("Data=%s ", pPStr->GetPointer());
                        pPStr->RemoveFromList();
                        pCurValue->DataList.AddLast (pPStr);
                        pPStr = 0;
                    }
                    break;
                }
            }
            break;

        case '=':
        case ':':
            dassert (pPStr->GetLen() == 1);
            InRange = vFalse;
            switch (ParseState) {
            case ps_SearchData:
                dwarning (0);
                break;
            case ps_SearchAssign:
                ParseState = ps_SearchData;
                break;
            default:
                delete pCurValue;
                return iErr_IniFile_WrongAssignCoding;
            }
            break;

        case ',':
            if (ParseState != ps_SearchData) {
                delete pCurValue;
                return iErr_IniFile_WrongAssignCoding;
            }
            InRange = vFalse;
            break;

        case '-':
            // Range found
            if (ParseState != ps_SearchData) {
                delete pCurValue;
                return iErr_IniFile_WrongAssignCoding;
            }
            if (InRange == vFalse) {
                InRange = vTrue;
                if (pCurValue->DataList.IsEmpty()) {
                    pPStr->RemoveFromList();
                    pCurValue->DataList.AddLast (pPStr);
                    pPStr = 0;
                } else {
                    COneMultiString *pLast = pCurValue->DataList.GetLast();
                    dassert (pLast != 0);
                    pLast->Append ("-", 1);
                }
            }
            break;
        //case ';':
        //    if (pCurValue) {
        //        if (pCurValue->DataList.IsEmpty() == vFalse) {
        //            pCurSec->ValueList.AddLast (pCurValue);
        //        } else  {
        //            delete pCurValue;
        //        }
        //        pCurValue = 0;
        //    }
        //    ParseState = ps_SearchName;
        //    break;

        case '#':
            InRange = vFalse;
            break;
        }
        delete pPStr;
    }
    if (pCurValue) {
        if (pCurValue->DataList.IsEmpty() == vFalse) {
            pCurSec->ValueList.AddLast (pCurValue);
        } else  {
            delete pCurValue;
        }
    }
    RETURN ('x', i_Okay);
}


/*===========================================================================*\
\*===========================================================================*/

#endif