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) * sizeof(char));
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';
85
nsINIParser::~nsINIParser()
91
nsINIParser::GetString( char *aSection, char *aKey,
92
char *aValBuf, int *aIOValBufSize )
99
if ( !aSection || !aKey || !aValBuf ||
100
!aIOValBufSize || (*aIOValBufSize <= 0) )
103
/* find the section if it exists */
104
mError = FindSection(aSection, &secPtr);
108
/* find the key if it exists in the valid section we found */
109
mError = FindKey(secPtr, aKey, aValBuf, aIOValBufSize);
115
nsINIParser::GetStringAlloc( char *aSection, char *aKey,
116
char **aOutBuf, int *aOutBufSize )
118
char buf[MAX_VAL_SIZE];
119
int bufsize = MAX_VAL_SIZE;
121
DUMP("GetStringAlloc");
123
mError = GetString(aSection, aKey, buf, &bufsize);
127
*aOutBuf = (char *) malloc(bufsize + 1);
128
strncpy(*aOutBuf, buf, bufsize);
129
*(*aOutBuf + bufsize) = 0;
130
*aOutBufSize = bufsize + 1;
136
nsINIParser::GetError()
143
nsINIParser::ResolveName(char *aINIRoot)
145
char *resolved = NULL;
147
struct stat st_exists;
153
locale = setlocale(LC_CTYPE, NULL);
157
/* resolved string: "<root>.ini.<locale>\0" */
158
resolved = (char *) malloc(strlen(aINIRoot) + 5 + strlen(locale) + 1);
162
/* locale specific ini file name */
163
sprintf(resolved, "%s.ini.%s", aINIRoot, locale);
164
if (0 == stat(resolved, &st_exists))
167
/* fallback to general ini file name */
168
sprintf(resolved, "%s.ini", aINIRoot);
169
if (0 == stat(resolved, &st_exists))
172
/* neither existed so error returned */
177
nsINIParser::FindSection(char *aSection, char **aOutSecPtr)
179
char *currChar = mFileBuf;
180
char *nextSec = NULL;
181
char *secClose = NULL;
183
int aSectionLen = strlen(aSection);
188
if (!aSection || !aOutSecPtr)
194
while (currChar < (mFileBuf + mFileBufSize))
196
// look for first '['
198
nextSec = strchr(currChar, '[');
202
currChar = nextSec + 1;
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))
214
// if section name matches we succeeded
215
if (strncmp(aSection, currChar, aSectionLen) == 0
216
&& secClose-currChar == aSectionLen)
218
*aOutSecPtr = secClose + 1;
228
nsINIParser::FindKey(char *aSecPtr, char *aKey, char *aVal, int *aIOValSize)
232
char *currLine = aSecPtr;
234
int aKeyLen = strlen(aKey);
239
if (!aSecPtr || !aKey || !aVal || !aIOValSize || (*aIOValSize <= 0))
245
// determine the section end
249
secEnd = strchr(secEnd, '['); // search for next sec start
252
secEnd = strchr(aSecPtr, '\0'); // else search for file end
255
mError = E_SEC_CORRUPT; // else this data is corrupt
260
// handle start section token ('[') in values for i18n
261
if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
267
while (currLine < secEnd)
270
nextNL = strchr(currLine, NL);
272
nextNL = mFileBuf + mFileBufSize;
274
// ignore commented lines (starting with ;)
275
if (currLine == strchr(currLine, ';'))
277
currLine = nextNL + 1;
281
// extract key before '='
283
nextEq = strchr(currLine, '=');
284
if (!nextEq || nextEq > nextNL)
286
currLine = nextNL + 1;
290
// if key matches we succeeded
291
if (strncmp(currLine, aKey, aKeyLen) == 0
292
&& nextEq-currLine == aKeyLen)
294
// extract the value and return
295
if (*aIOValSize < nextNL - nextEq)
297
mError = E_SMALL_BUF;
303
*aIOValSize = nextNL - (nextEq + 1);
304
strncpy(aVal, (nextEq + 1), *aIOValSize);
305
*(aVal + *aIOValSize) = 0; // null terminate
311
currLine = nextNL + 1;