1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/
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.
13
* The Original Code is Mozilla Communicator client code,
14
* released March 31, 1998.
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
22
* Samir Gehani <sgehani@netscape.com>
26
#include "nsINIParser.h"
28
nsINIParser::nsINIParser(char *aFilename)
47
fd = fopen(aFilename, "r");
52
if (fseek(fd, 0, SEEK_END) != 0)
58
/* malloc an internal buf the size of the file */
59
mFileBuf = (char *) malloc(eofpos + 1);
65
mFileBufSize = eofpos;
67
/* read the file in one swoop */
68
if (fseek(fd, 0, SEEK_SET) != 0)
70
rd = fread((void *)mFileBuf, 1, eofpos, fd);
73
mFileBuf[mFileBufSize] = '\0';
78
/* Make sure the buffer is null-terminated. */
79
mFileBuf[eofpos] = '\0';
88
nsINIParser::~nsINIParser()
94
nsINIParser::GetString( char *aSection, char *aKey,
95
char *aValBuf, int *aIOValBufSize )
102
if ( !aSection || !aKey || !aValBuf ||
103
!aIOValBufSize || (*aIOValBufSize <= 0) )
106
/* find the section if it exists */
107
mError = FindSection(aSection, &secPtr);
111
/* find the key if it exists in the valid section we found */
112
mError = FindKey(secPtr, aKey, aValBuf, aIOValBufSize);
118
nsINIParser::GetStringAlloc( char *aSection, char *aKey,
119
char **aOutBuf, int *aOutBufSize )
121
char buf[MAX_VAL_SIZE];
122
int bufsize = MAX_VAL_SIZE;
124
DUMP("GetStringAlloc");
126
mError = GetString(aSection, aKey, buf, &bufsize);
130
*aOutBuf = (char *) malloc(bufsize + 1);
131
strncpy(*aOutBuf, buf, bufsize);
132
*(*aOutBuf + bufsize) = 0;
133
*aOutBufSize = bufsize + 1;
139
nsINIParser::GetError()
146
nsINIParser::ResolveName(char *aINIRoot)
148
char *resolved = NULL;
150
struct stat st_exists;
156
locale = setlocale(LC_CTYPE, NULL);
160
/* resolved string: "<root>.ini.<locale>\0" */
161
resolved = (char *) malloc(strlen(aINIRoot) + 5 + strlen(locale) + 1);
165
/* locale specific ini file name */
166
sprintf(resolved, "%s.ini.%s", aINIRoot, locale);
167
if (0 == stat(resolved, &st_exists))
170
/* fallback to general ini file name */
171
sprintf(resolved, "%s.ini", aINIRoot);
172
if (0 == stat(resolved, &st_exists))
175
/* neither existed so error returned */
180
nsINIParser::FindSection(char *aSection, char **aOutSecPtr)
182
char *currChar = mFileBuf;
183
char *nextSec = NULL;
184
char *secClose = NULL;
186
int aSectionLen = strlen(aSection);
191
if (!aSection || !aOutSecPtr)
197
while (currChar < (mFileBuf + mFileBufSize))
199
// look for first '['
201
nextSec = strchr(currChar, '[');
205
currChar = nextSec + 1;
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))
217
// if section name matches we succeeded
218
if (strncmp(aSection, currChar, aSectionLen) == 0
219
&& secClose-currChar == aSectionLen)
221
*aOutSecPtr = secClose + 1;
231
nsINIParser::FindKey(char *aSecPtr, char *aKey, char *aVal, int *aIOValSize)
235
char *currLine = aSecPtr;
237
int aKeyLen = strlen(aKey);
242
if (!aSecPtr || !aKey || !aVal || !aIOValSize || (*aIOValSize <= 0))
248
// determine the section end
252
secEnd = strchr(secEnd, '['); // search for next sec start
255
secEnd = strchr(aSecPtr, '\0'); // else search for file end
258
mError = E_SEC_CORRUPT; // else this data is corrupt
263
// handle start section token ('[') in values for i18n
264
if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
270
while (currLine < secEnd)
273
nextNL = strchr(currLine, NL);
275
nextNL = mFileBuf + mFileBufSize;
277
// ignore commented lines (starting with ;)
278
if (currLine == strchr(currLine, ';'))
280
currLine = nextNL + 1;
284
// extract key before '='
286
nextEq = strchr(currLine, '=');
287
if (!nextEq || nextEq > nextNL)
289
currLine = nextNL + 1;
293
// if key matches we succeeded
294
if (strncmp(currLine, aKey, aKeyLen) == 0
295
&& nextEq-currLine == aKeyLen)
297
// extract the value and return
298
if (*aIOValSize < nextNL - nextEq)
300
mError = E_SMALL_BUF;
306
*aIOValSize = nextNL - (nextEq + 1);
307
strncpy(aVal, (nextEq + 1), *aIOValSize);
308
*(aVal + *aIOValSize) = 0; // null terminate
314
currLine = nextNL + 1;