~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/xpinstall/wizard/unix/src2/nsINIParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/*
 
3
 * The contents of this file are subject to the Netscape Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/NPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is Mozilla Communicator client code, 
 
14
 * released March 31, 1998. 
 
15
 *
 
16
 * The Initial Developer of the Original Code is Netscape Communications 
 
17
 * Corporation.  Portions created by Netscape are
 
18
 * Copyright (C) 1998 Netscape Communications Corporation. All
 
19
 * Rights Reserved.
 
20
 *
 
21
 * Contributor(s): 
 
22
 *     Samir Gehani <sgehani@netscape.com>
 
23
 */
 
24
 
 
25
 
 
26
#include "nsINIParser.h"
 
27
 
 
28
nsINIParser::nsINIParser(char *aFilename)
 
29
{
 
30
    FILE    *fd = NULL;
 
31
    long    eofpos = 0;
 
32
    int     rd = 0;
 
33
 
 
34
    mFileBuf = NULL;
 
35
    mFileBufSize = 0;
 
36
    mError = OK;
 
37
    DUMP("nsINIParser");
 
38
 
 
39
    /* param check */
 
40
    if (!aFilename)
 
41
    {
 
42
        mError = E_PARAM;
 
43
        return;
 
44
    }
 
45
 
 
46
    /* open the file */
 
47
    fd = fopen(aFilename, "r");
 
48
    if (!fd)
 
49
        goto bail;
 
50
    
 
51
    /* get file size */
 
52
    if (fseek(fd, 0, SEEK_END) != 0)
 
53
        goto bail;
 
54
    eofpos = ftell(fd);
 
55
    if (eofpos == 0)
 
56
        goto bail;
 
57
 
 
58
    /* malloc an internal buf the size of the file */
 
59
    mFileBuf = (char *) malloc((eofpos+1) * sizeof(char));
 
60
    if (!mFileBuf)
 
61
    {
 
62
        mError = E_MEM;
 
63
        return;
 
64
    }
 
65
    mFileBufSize = eofpos;
 
66
 
 
67
    /* read the file in one swoop */
 
68
    if (fseek(fd, 0, SEEK_SET) != 0)
 
69
        goto bail;
 
70
    rd = fread((void *)mFileBuf, 1, eofpos, fd);
 
71
    if (!rd)
 
72
        goto bail;
 
73
    mFileBuf[mFileBufSize] = '\0';
 
74
 
 
75
    /* close file */
 
76
    fclose(fd);
 
77
 
 
78
    return;
 
79
 
 
80
bail:
 
81
    mError = E_READ;
 
82
    return;
 
83
}
 
84
 
 
85
nsINIParser::~nsINIParser()
 
86
{
 
87
    DUMP("~nsINIParser");
 
88
}
 
89
 
 
90
int
 
91
nsINIParser::GetString( char *aSection, char *aKey, 
 
92
                        char *aValBuf, int *aIOValBufSize )
 
93
{
 
94
    char *secPtr = NULL;
 
95
    mError = OK;
 
96
    DUMP("GetString");
 
97
 
 
98
    /* param check */
 
99
    if ( !aSection || !aKey || !aValBuf || 
 
100
         !aIOValBufSize || (*aIOValBufSize <= 0) )
 
101
        return E_PARAM;
 
102
 
 
103
    /* find the section if it exists */
 
104
    mError = FindSection(aSection, &secPtr);
 
105
    if (mError != OK)
 
106
        return mError;
 
107
 
 
108
    /* find the key if it exists in the valid section we found */
 
109
    mError = FindKey(secPtr, aKey, aValBuf, aIOValBufSize);
 
110
 
 
111
    return mError;
 
112
}
 
113
 
 
114
int
 
115
nsINIParser::GetStringAlloc( char *aSection, char *aKey,
 
116
                             char **aOutBuf, int *aOutBufSize )
 
117
{
 
118
    char buf[MAX_VAL_SIZE];
 
119
    int bufsize = MAX_VAL_SIZE;
 
120
    mError = OK;
 
121
    DUMP("GetStringAlloc");
 
122
 
 
123
    mError = GetString(aSection, aKey, buf, &bufsize);
 
124
    if (mError != OK)
 
125
        return mError;
 
126
 
 
127
    *aOutBuf = (char *) malloc(bufsize + 1);
 
128
    strncpy(*aOutBuf, buf, bufsize);
 
129
    *(*aOutBuf + bufsize) = 0;
 
130
    *aOutBufSize = bufsize + 1;
 
131
 
 
132
    return mError;
 
133
}
 
134
 
 
135
int
 
136
nsINIParser::GetError()
 
137
{
 
138
    DUMP("GetError");
 
139
    return mError;
 
140
}
 
141
 
 
142
char *
 
143
nsINIParser::ResolveName(char *aINIRoot)
 
144
{
 
145
    char *resolved = NULL;
 
146
    char *locale = NULL;
 
147
    struct stat st_exists;
 
148
 
 
149
    /* param check */
 
150
    if (!aINIRoot)
 
151
        return NULL;
 
152
 
 
153
    locale = setlocale(LC_CTYPE, NULL);
 
154
    if (!locale) 
 
155
        return NULL;
 
156
 
 
157
    /* resolved string: "<root>.ini.<locale>\0" */
 
158
    resolved = (char *) malloc(strlen(aINIRoot) + 5 + strlen(locale) + 1);
 
159
    if (!resolved)
 
160
        return NULL;
 
161
 
 
162
    /* locale specific ini file name */
 
163
    sprintf(resolved, "%s.ini.%s", aINIRoot, locale);
 
164
    if (0 == stat(resolved, &st_exists))
 
165
        return resolved;
 
166
 
 
167
    /* fallback to general ini file name */
 
168
    sprintf(resolved, "%s.ini", aINIRoot);
 
169
    if (0 == stat(resolved, &st_exists))
 
170
        return resolved;
 
171
    
 
172
    /* neither existed so error returned */
 
173
    return NULL;
 
174
}
 
175
 
 
176
int
 
177
nsINIParser::FindSection(char *aSection, char **aOutSecPtr)
 
178
{
 
179
    char *currChar = mFileBuf;
 
180
    char *nextSec = NULL;
 
181
    char *secClose = NULL;
 
182
    char *nextNL = NULL;
 
183
    int aSectionLen = strlen(aSection);
 
184
    mError = E_NO_SEC;
 
185
    DUMP("FindSection");
 
186
 
 
187
    // param check
 
188
    if (!aSection || !aOutSecPtr)
 
189
    {
 
190
        mError = E_PARAM;
 
191
        return mError;
 
192
    }
 
193
 
 
194
    while (currChar < (mFileBuf + mFileBufSize))
 
195
    {
 
196
        // look for first '['
 
197
        nextSec = NULL;
 
198
        nextSec = strchr(currChar, '[');
 
199
        if (!nextSec)
 
200
            break;
 
201
            
 
202
        currChar = nextSec + 1;
 
203
 
 
204
        // extract section name till first ']'
 
205
        secClose = NULL; nextNL = NULL;
 
206
        secClose = strchr(currChar, ']');
 
207
        nextNL = strchr(currChar, NL);
 
208
        if ((!nextNL) || (nextNL < secClose))
 
209
        {
 
210
            currChar = nextNL;
 
211
            continue;
 
212
        }
 
213
 
 
214
        // if section name matches we succeeded
 
215
        if (strncmp(aSection, currChar, aSectionLen) == 0
 
216
              && secClose-currChar == aSectionLen)
 
217
        {
 
218
            *aOutSecPtr = secClose + 1;
 
219
            mError = OK;
 
220
            break;
 
221
        }
 
222
    }
 
223
 
 
224
    return mError;
 
225
}
 
226
 
 
227
int
 
228
nsINIParser::FindKey(char *aSecPtr, char *aKey, char *aVal, int *aIOValSize)
 
229
{
 
230
    char *nextNL = NULL;
 
231
    char *secEnd = NULL;
 
232
    char *currLine = aSecPtr;
 
233
    char *nextEq = NULL;
 
234
    int  aKeyLen = strlen(aKey); 
 
235
    mError = E_NO_KEY;
 
236
    DUMP("FindKey");
 
237
 
 
238
    // param check
 
239
    if (!aSecPtr || !aKey || !aVal || !aIOValSize || (*aIOValSize <= 0))
 
240
    {
 
241
        mError = E_PARAM;
 
242
        return mError;
 
243
    }
 
244
 
 
245
    // determine the section end
 
246
    secEnd = aSecPtr;
 
247
find_end:
 
248
    if (secEnd)
 
249
        secEnd = strchr(secEnd, '['); // search for next sec start
 
250
    if (!secEnd)
 
251
    {
 
252
        secEnd = strchr(aSecPtr, '\0'); // else search for file end
 
253
        if (!secEnd)
 
254
        {
 
255
            mError = E_SEC_CORRUPT; // else this data is corrupt
 
256
            return mError;
 
257
        }
 
258
    }
 
259
 
 
260
    // handle start section token ('[') in values for i18n
 
261
    if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
 
262
    {
 
263
        secEnd++;
 
264
        goto find_end;
 
265
    }
 
266
 
 
267
    while (currLine < secEnd)
 
268
    {
 
269
        nextNL = NULL;
 
270
        nextNL = strchr(currLine, NL);
 
271
        if (!nextNL)
 
272
            nextNL = mFileBuf + mFileBufSize;
 
273
 
 
274
        // ignore commented lines (starting with ;)
 
275
        if (currLine == strchr(currLine, ';'))
 
276
        {
 
277
            currLine = nextNL + 1;
 
278
            continue;
 
279
        }
 
280
 
 
281
        // extract key before '='
 
282
        nextEq = NULL;
 
283
        nextEq = strchr(currLine, '=');
 
284
        if (!nextEq || nextEq > nextNL) 
 
285
        {
 
286
            currLine = nextNL + 1;
 
287
            continue;
 
288
        }
 
289
 
 
290
        // if key matches we succeeded
 
291
        if (strncmp(currLine, aKey, aKeyLen) == 0
 
292
              && nextEq-currLine == aKeyLen)
 
293
        {
 
294
            // extract the value and return
 
295
            if (*aIOValSize < nextNL - nextEq)
 
296
            {
 
297
                mError = E_SMALL_BUF;
 
298
                *aVal = '\0';
 
299
                *aIOValSize = 0;
 
300
                return mError;
 
301
            }
 
302
                
 
303
            *aIOValSize = nextNL - (nextEq + 1); 
 
304
            strncpy(aVal, (nextEq + 1), *aIOValSize);
 
305
            *(aVal + *aIOValSize) = 0; // null terminate
 
306
            mError = OK;
 
307
            return mError;
 
308
        }
 
309
        else
 
310
        {
 
311
            currLine = nextNL + 1;
 
312
        }
 
313
    }
 
314
 
 
315
    return mError;
 
316
}