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

« back to all changes in this revision

Viewing changes to mozilla/toolkit/mozapps/installer/unix/wizard/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);
 
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
    /* Make sure the buffer is null-terminated. */
 
79
    mFileBuf[eofpos] = '\0';
 
80
 
 
81
    return;
 
82
 
 
83
bail:
 
84
    mError = E_READ;
 
85
    return;
 
86
}
 
87
 
 
88
nsINIParser::~nsINIParser()
 
89
{
 
90
    DUMP("~nsINIParser");
 
91
}
 
92
 
 
93
int
 
94
nsINIParser::GetString( char *aSection, char *aKey, 
 
95
                        char *aValBuf, int *aIOValBufSize )
 
96
{
 
97
    char *secPtr = NULL;
 
98
    mError = OK;
 
99
    DUMP("GetString");
 
100
 
 
101
    /* param check */
 
102
    if ( !aSection || !aKey || !aValBuf || 
 
103
         !aIOValBufSize || (*aIOValBufSize <= 0) )
 
104
        return E_PARAM;
 
105
 
 
106
    /* find the section if it exists */
 
107
    mError = FindSection(aSection, &secPtr);
 
108
    if (mError != OK)
 
109
        return mError;
 
110
 
 
111
    /* find the key if it exists in the valid section we found */
 
112
    mError = FindKey(secPtr, aKey, aValBuf, aIOValBufSize);
 
113
 
 
114
    return mError;
 
115
}
 
116
 
 
117
int
 
118
nsINIParser::GetStringAlloc( char *aSection, char *aKey,
 
119
                             char **aOutBuf, int *aOutBufSize )
 
120
{
 
121
    char buf[MAX_VAL_SIZE];
 
122
    int bufsize = MAX_VAL_SIZE;
 
123
    mError = OK;
 
124
    DUMP("GetStringAlloc");
 
125
 
 
126
    mError = GetString(aSection, aKey, buf, &bufsize);
 
127
    if (mError != OK)
 
128
        return mError;
 
129
 
 
130
    *aOutBuf = (char *) malloc(bufsize + 1);
 
131
    strncpy(*aOutBuf, buf, bufsize);
 
132
    *(*aOutBuf + bufsize) = 0;
 
133
    *aOutBufSize = bufsize + 1;
 
134
 
 
135
    return mError;
 
136
}
 
137
 
 
138
int
 
139
nsINIParser::GetError()
 
140
{
 
141
    DUMP("GetError");
 
142
    return mError;
 
143
}
 
144
 
 
145
char *
 
146
nsINIParser::ResolveName(char *aINIRoot)
 
147
{
 
148
    char *resolved = NULL;
 
149
    char *locale = NULL;
 
150
    struct stat st_exists;
 
151
 
 
152
    /* param check */
 
153
    if (!aINIRoot)
 
154
        return NULL;
 
155
 
 
156
    locale = setlocale(LC_CTYPE, NULL);
 
157
    if (!locale) 
 
158
        return NULL;
 
159
 
 
160
    /* resolved string: "<root>.ini.<locale>\0" */
 
161
    resolved = (char *) malloc(strlen(aINIRoot) + 5 + strlen(locale) + 1);
 
162
    if (!resolved)
 
163
        return NULL;
 
164
 
 
165
    /* locale specific ini file name */
 
166
    sprintf(resolved, "%s.ini.%s", aINIRoot, locale);
 
167
    if (0 == stat(resolved, &st_exists))
 
168
        return resolved;
 
169
 
 
170
    /* fallback to general ini file name */
 
171
    sprintf(resolved, "%s.ini", aINIRoot);
 
172
    if (0 == stat(resolved, &st_exists))
 
173
        return resolved;
 
174
    
 
175
    /* neither existed so error returned */
 
176
    return NULL;
 
177
}
 
178
 
 
179
int
 
180
nsINIParser::FindSection(char *aSection, char **aOutSecPtr)
 
181
{
 
182
    char *currChar = mFileBuf;
 
183
    char *nextSec = NULL;
 
184
    char *secClose = NULL;
 
185
    char *nextNL = NULL;
 
186
    int aSectionLen = strlen(aSection);
 
187
    mError = E_NO_SEC;
 
188
    DUMP("FindSection");
 
189
 
 
190
    // param check
 
191
    if (!aSection || !aOutSecPtr)
 
192
    {
 
193
        mError = E_PARAM;
 
194
        return mError;
 
195
    }
 
196
 
 
197
    while (currChar < (mFileBuf + mFileBufSize))
 
198
    {
 
199
        // look for first '['
 
200
        nextSec = NULL;
 
201
        nextSec = strchr(currChar, '[');
 
202
        if (!nextSec)
 
203
            break;
 
204
            
 
205
        currChar = nextSec + 1;
 
206
 
 
207
        // extract section name till first ']'
 
208
        secClose = NULL; nextNL = NULL;
 
209
        secClose = strchr(currChar, ']');
 
210
        nextNL = strchr(currChar, NL);
 
211
        if ((!nextNL) || (nextNL < secClose))
 
212
        {
 
213
            currChar = nextNL;
 
214
            continue;
 
215
        }
 
216
 
 
217
        // if section name matches we succeeded
 
218
        if (strncmp(aSection, currChar, aSectionLen) == 0
 
219
              && secClose-currChar == aSectionLen)
 
220
        {
 
221
            *aOutSecPtr = secClose + 1;
 
222
            mError = OK;
 
223
            break;
 
224
        }
 
225
    }
 
226
 
 
227
    return mError;
 
228
}
 
229
 
 
230
int
 
231
nsINIParser::FindKey(char *aSecPtr, char *aKey, char *aVal, int *aIOValSize)
 
232
{
 
233
    char *nextNL = NULL;
 
234
    char *secEnd = NULL;
 
235
    char *currLine = aSecPtr;
 
236
    char *nextEq = NULL;
 
237
    int  aKeyLen = strlen(aKey); 
 
238
    mError = E_NO_KEY;
 
239
    DUMP("FindKey");
 
240
 
 
241
    // param check
 
242
    if (!aSecPtr || !aKey || !aVal || !aIOValSize || (*aIOValSize <= 0))
 
243
    {
 
244
        mError = E_PARAM;
 
245
        return mError;
 
246
    }
 
247
 
 
248
    // determine the section end
 
249
    secEnd = aSecPtr;
 
250
find_end:
 
251
    if (secEnd)
 
252
        secEnd = strchr(secEnd, '['); // search for next sec start
 
253
    if (!secEnd)
 
254
    {
 
255
        secEnd = strchr(aSecPtr, '\0'); // else search for file end
 
256
        if (!secEnd)
 
257
        {
 
258
            mError = E_SEC_CORRUPT; // else this data is corrupt
 
259
            return mError;
 
260
        }
 
261
    }
 
262
 
 
263
    // handle start section token ('[') in values for i18n
 
264
    if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
 
265
    {
 
266
        secEnd++;
 
267
        goto find_end;
 
268
    }
 
269
 
 
270
    while (currLine < secEnd)
 
271
    {
 
272
        nextNL = NULL;
 
273
        nextNL = strchr(currLine, NL);
 
274
        if (!nextNL)
 
275
            nextNL = mFileBuf + mFileBufSize;
 
276
 
 
277
        // ignore commented lines (starting with ;)
 
278
        if (currLine == strchr(currLine, ';'))
 
279
        {
 
280
            currLine = nextNL + 1;
 
281
            continue;
 
282
        }
 
283
 
 
284
        // extract key before '='
 
285
        nextEq = NULL;
 
286
        nextEq = strchr(currLine, '=');
 
287
        if (!nextEq || nextEq > nextNL) 
 
288
        {
 
289
            currLine = nextNL + 1;
 
290
            continue;
 
291
        }
 
292
 
 
293
        // if key matches we succeeded
 
294
        if (strncmp(currLine, aKey, aKeyLen) == 0
 
295
              && nextEq-currLine == aKeyLen)
 
296
        {
 
297
            // extract the value and return
 
298
            if (*aIOValSize < nextNL - nextEq)
 
299
            {
 
300
                mError = E_SMALL_BUF;
 
301
                *aVal = '\0';
 
302
                *aIOValSize = 0;
 
303
                return mError;
 
304
            }
 
305
                
 
306
            *aIOValSize = nextNL - (nextEq + 1); 
 
307
            strncpy(aVal, (nextEq + 1), *aIOValSize);
 
308
            *(aVal + *aIOValSize) = 0; // null terminate
 
309
            mError = OK;
 
310
            return mError;
 
311
        }
 
312
        else
 
313
        {
 
314
            currLine = nextNL + 1;
 
315
        }
 
316
    }
 
317
 
 
318
    return mError;
 
319
}