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"
31
unsigned long GetPrivateProfileString(const char* szAppName,
32
const char* szKeyName,
33
const char* szDefault,
34
char* szReturnedString,
36
const char* szFileName)
38
nsINIParser parser((char*)szFileName);
39
if (parser.GetString((char*)szAppName, (char*)szKeyName, szReturnedString, &nSize) != nsINIParser::OK) {
41
strcpy(szReturnedString, szDefault);
44
return strlen(szReturnedString);
47
unsigned long WritePrivateProfileString(const char* szAppName,
48
const char* szKeyName,
50
const char* szFileName)
52
nsINIParser parser((char*)szFileName);
53
if (parser.WriteString((char*)szAppName, (char*)szKeyName, (char*)szValue) != nsINIParser::OK) {
62
nsINIParser::nsINIParser(char *aFilename)
70
mFilename = (char*)malloc(strlen(aFilename)+1);
71
strcpy(mFilename, aFilename);
83
fd = fopen(aFilename, "r");
88
if (fseek(fd, 0, SEEK_END) != 0)
94
/* malloc an internal buf the size of the file */
95
mFileBuf = (char *) calloc(1, eofpos * sizeof(char));
101
mFileBufSize = eofpos;
103
/* read the file in one swoop */
104
if (fseek(fd, 0, SEEK_SET) != 0)
106
rd = fread((void *)mFileBuf, 1, eofpos, fd);
110
/* actual number of bytes read is less than the size of the file */
111
/* due to /r/n -> /n conversion */
115
mFileBufSize = eofpos;
128
nsINIParser::~nsINIParser()
131
FILE* hFile = fopen(mFilename, "w+");
132
fwrite(mFileBuf, mFileBufSize, 1, (FILE *)hFile);
140
nsINIParser::GetString( char *aSection, char *aKey,
141
char *aValBuf, int *aIOValBufSize )
148
if ( !aSection || !aValBuf ||
149
!aIOValBufSize || (*aIOValBufSize <= 0) )
152
/* find the section if it exists */
153
mError = FindSection(aSection, &secPtr);
158
/* find the key if it exists in the valid section we found */
159
mError = GetValue(secPtr, aKey, aValBuf, aIOValBufSize);
161
mError = GetAllKeys(secPtr, aValBuf, aIOValBufSize);
169
nsINIParser::GetStringAlloc( char *aSection, char *aKey,
170
char **aOutBuf, int *aOutBufSize )
172
char buf[MAX_VAL_SIZE];
173
int bufsize = MAX_VAL_SIZE;
175
DUMP("GetStringAlloc");
177
mError = GetString(aSection, aKey, buf, &bufsize);
181
*aOutBuf = (char *) malloc(bufsize + 1);
182
strncpy(*aOutBuf, buf, bufsize);
183
*(*aOutBuf + bufsize) = 0;
184
*aOutBufSize = bufsize + 1;
190
nsINIParser::GetError()
197
nsINIParser::ResolveName(char *aINIRoot)
199
char *resolved = NULL;
201
struct stat st_exists;
207
locale = setlocale(LC_CTYPE, NULL);
211
/* resolved string: "<root>.ini.<locale>\0" */
212
resolved = (char *) malloc(strlen(aINIRoot) + 5 + strlen(locale) + 1);
216
/* locale specific ini file name */
217
sprintf(resolved, "%s.ini.%s", aINIRoot, locale);
218
if (0 == stat(resolved, &st_exists))
221
/* fallback to general ini file name */
222
sprintf(resolved, "%s.ini", aINIRoot);
223
if (0 == stat(resolved, &st_exists))
226
/* neither existed so error returned */
231
nsINIParser::WriteString(char *aSection, char *aKey, char *aValBuf)
240
if ( !aSection || !aKey || !aValBuf )
243
/* find the section if it exists */
244
mError = FindSection(aSection, &secPtr);
246
/* if the seciont doesn't exist, add it to the END of the buffer */
247
char szNewSection[MAX_VAL_SIZE];
248
strcpy(szNewSection, "[");
249
strcat(szNewSection, aSection);
250
strcat(szNewSection, "]");
251
strcat(szNewSection, NLSTRING);
252
mFileBuf = (char*)realloc(mFileBuf, mFileBufSize+strlen(szNewSection));
253
memcpy(&mFileBuf[mFileBufSize], szNewSection, strlen(szNewSection));
254
secPtr = &mFileBuf[mFileBufSize];
255
mFileBufSize+= strlen(szNewSection);
259
/* find the key if it exists in the valid section we found */
260
mError = FindKey(secPtr, aKey, &keyPtr);
262
char szNewKeyValue[MAX_VAL_SIZE];
263
strcpy(szNewKeyValue, aKey);
264
strcat(szNewKeyValue, "=");
265
strcat(szNewKeyValue, aValBuf);
266
strcat(szNewKeyValue, NLSTRING);
267
char *mNewFileBuf = (char*)calloc(1, mFileBufSize+strlen(szNewKeyValue));
268
memcpy(mNewFileBuf, mFileBuf, mFileBufSize);
269
/* Set the section pointer to the location of the rest of the buffer */
270
while (*secPtr != NL) {
274
/* Use sectionptr to compte where in the new buf we are going to insert */
275
memcpy(mNewFileBuf+(secPtr-mFileBuf), szNewKeyValue, strlen(szNewKeyValue));
276
memcpy(mNewFileBuf+(secPtr-mFileBuf)+strlen(szNewKeyValue), secPtr, mFileBufSize-(secPtr-mFileBuf));
277
mFileBufSize += strlen(szNewKeyValue);
279
mFileBuf = mNewFileBuf;
282
while (*keyPtr != '=') {
287
char* lenPtr = keyPtr;
288
while (*lenPtr != NL) {
293
difference = strlen(aValBuf)- length;
294
char *mNewFileBuf = (char*)calloc(1, mFileBufSize+difference);
295
memcpy(mNewFileBuf, mFileBuf, mFileBufSize);
296
/* Use keyptr to compte where in the new buf we are going to insert */
297
memcpy(mNewFileBuf+(keyPtr-mFileBuf), aValBuf, strlen(aValBuf));
300
memcpy(mNewFileBuf+(keyPtr-mFileBuf)+strlen(aValBuf), lenPtr, mFileBufSize-(lenPtr-mFileBuf));
301
mFileBufSize += difference;
303
mFileBuf = mNewFileBuf;
313
nsINIParser::FindSection(char *aSection, char **aOutSecPtr)
315
char *currChar = mFileBuf;
316
char *nextSec = NULL;
317
char *secClose = NULL;
323
if (!aSection || !aOutSecPtr)
329
while (currChar < (mFileBuf + mFileBufSize))
331
// look for first '['
333
nextSec = strchr(currChar, '[');
337
currChar = nextSec + 1;
339
// extract section name till first ']'
340
secClose = NULL; nextNL = NULL;
341
secClose = strchr(currChar, ']');
342
nextNL = strchr(currChar, NL);
343
if ((!nextNL) || (nextNL < secClose))
349
// if section name matches we succeeded
350
if (strnicmp(aSection, currChar, strlen(aSection)) == 0)
352
*aOutSecPtr = secClose + 1;
362
nsINIParser::GetValue(char *aSecPtr, char *aKey, char *aVal, int *aIOValSize)
366
char *currLine = aSecPtr;
372
if (!aSecPtr || !aKey || !aVal || !aIOValSize || (*aIOValSize <= 0))
378
// determine the section end
382
secEnd = strchr(secEnd, '['); // search for next sec start
385
secEnd = strchr(aSecPtr, '\0'); // else search for file end
388
mError = E_SEC_CORRUPT; // else this data is corrupt
393
// handle start section token ('[') in values for i18n
394
if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
400
while (currLine < secEnd)
403
nextNL = strchr(currLine, NL);
405
nextNL = mFileBuf + mFileBufSize;
407
// ignore commented lines (starting with ;)
408
if (currLine == strchr(currLine, ';'))
410
currLine = nextNL + 1;
414
// extract key before '='
416
nextEq = strchr(currLine, '=');
417
if (!nextEq || nextEq > nextNL)
419
currLine = nextNL + 1;
423
// if key matches we succeeded
424
if (strnicmp(currLine, aKey, strlen(aKey)) == 0)
426
// extract the value and return
427
if (*aIOValSize < nextNL - nextEq)
429
mError = E_SMALL_BUF;
435
*aIOValSize = nextNL - (nextEq + 1);
436
strncpy(aVal, (nextEq + 1), *aIOValSize);
437
*(aVal + *aIOValSize) = 0; // null terminate
443
currLine = nextNL + 1;
451
nsINIParser::GetAllKeys(char *aSecPtr, char *aVal, int *aIOValSize)
455
char *currLine = aSecPtr;
462
if (!aSecPtr || !aVal || !aIOValSize || (*aIOValSize <= 0))
468
// determine the section end
472
secEnd = strchr(secEnd, '['); // search for next sec start
475
secEnd = strchr(aSecPtr, '\0'); // else search for file end
478
mError = E_SEC_CORRUPT; // else this data is corrupt
483
// handle start section token ('[') in values for i18n
484
if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
492
while (currLine < secEnd)
495
nextNL = strchr(currLine, NL);
497
nextNL = mFileBuf + mFileBufSize;
499
// ignore commented lines (starting with ;)
500
if (currLine == strchr(currLine, ';'))
502
currLine = nextNL + 1;
506
// extract key before '='
508
nextEq = strchr(currLine, '=');
509
if (!nextEq || nextEq > nextNL)
511
currLine = nextNL + 1;
515
strncpy(aVal, currLine, nextEq-currLine);
516
aVal+= nextEq-currLine;
520
currLine = nextNL + 1;
527
nsINIParser::FindKey(char *aSecPtr, char *aKey, char **aOutKeyPtr)
531
char *currLine = aSecPtr;
537
if (!aSecPtr || !aKey || !aOutKeyPtr)
543
// determine the section end
547
secEnd = strchr(secEnd, '['); // search for next sec start
550
secEnd = strchr(aSecPtr, '\0'); // else search for file end
553
mError = E_SEC_CORRUPT; // else this data is corrupt
558
// handle start section token ('[') in values for i18n
559
if (*secEnd == '[' && !(secEnd == aSecPtr || *(secEnd-1) == NL))
565
while (currLine < secEnd)
568
nextNL = strchr(currLine, NL);
570
nextNL = mFileBuf + mFileBufSize;
572
// ignore commented lines (starting with ;)
573
if (currLine == strchr(currLine, ';'))
575
currLine = nextNL + 1;
579
// extract key before '='
581
nextEq = strchr(currLine, '=');
582
if (!nextEq || nextEq > nextNL)
584
currLine = nextNL + 1;
588
// if key matches we succeeded
589
if (strnicmp(currLine, aKey, strlen(aKey)) == 0)
591
*aOutKeyPtr = currLine;
597
currLine = nextNL + 1;