| 1 |
/* minIni - Multi-Platform INI file parser, suitable for embedded systems |
| 2 |
* |
| 3 |
* These routines are in part based on the article "Multiplatform .INI Files" |
| 4 |
* by Joseph J. Graf in the March 1994 issue of Dr. Dobb's Journal. |
| 5 |
* |
| 6 |
* Copyright (c) CompuPhase, 2008-2020 |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 9 |
* use this file except in compliance with the License. You may obtain a copy |
| 10 |
* of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, software |
| 15 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 |
* License for the specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
* Version: $Id: minIni.c 53 2015-01-18 13:35:11Z [email protected] $ |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "minGlue.h" |
| 24 |
|
| 25 |
|
| 26 |
#define MININI_IMPLEMENTATION |
| 27 |
#include "minIni.h" |
| 28 |
#if defined NDEBUG |
| 29 |
#define assert(e) |
| 30 |
#else |
| 31 |
#include <assert.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#if defined __linux || defined __linux__ |
| 35 |
#define __LINUX__ |
| 36 |
#elif defined FREEBSD && !defined __FreeBSD__ |
| 37 |
#define __FreeBSD__ |
| 38 |
#elif defined(_MSC_VER) |
| 39 |
#pragma warning(disable: 4996) /* for Microsoft Visual C/C++ */ |
| 40 |
#endif |
| 41 |
#if !defined strnicmp && !defined PORTABLE_STRNICMP |
| 42 |
#if defined __LINUX__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ |
| 43 |
#define strnicmp strncasecmp |
| 44 |
#endif |
| 45 |
#endif |
| 46 |
#if !defined _totupper |
| 47 |
#define _totupper toupper |
| 48 |
#endif |
| 49 |
|
| 50 |
#if !defined INI_LINETERM |
| 51 |
#if defined __LINUX__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ |
| 52 |
#define INI_LINETERM __T("\n") |
| 53 |
#else |
| 54 |
#define INI_LINETERM __T("\r\n") |
| 55 |
#endif |
| 56 |
#endif |
| 57 |
#if !defined INI_FILETYPE |
| 58 |
#error Missing definition for INI_FILETYPE. |
| 59 |
#endif |
| 60 |
|
| 61 |
#if !defined sizearray |
| 62 |
#define sizearray(a) (sizeof(a) / sizeof((a)[0])) |
| 63 |
#endif |
| 64 |
|
| 65 |
enum quote_option { |
| 66 |
QUOTE_NONE, |
| 67 |
QUOTE_ENQUOTE, |
| 68 |
QUOTE_DEQUOTE, |
| 69 |
}; |
| 70 |
|
| 71 |
#if defined PORTABLE_STRNICMP |
| 72 |
int strnicmp(const mTCHAR *s1, const mTCHAR *s2, size_t n) |
| 73 |
{ |
| 74 |
while (n-- != 0 && (*s1 || *s2)) { |
| 75 |
register int c1, c2; |
| 76 |
c1 = *s1++; |
| 77 |
if ('a' <= c1 && c1 <= 'z') |
| 78 |
c1 += ('A' - 'a'); |
| 79 |
c2 = *s2++; |
| 80 |
if ('a' <= c2 && c2 <= 'z') |
| 81 |
c2 += ('A' - 'a'); |
| 82 |
if (c1 != c2) |
| 83 |
return c1 - c2; |
| 84 |
} /* while */ |
| 85 |
return 0; |
| 86 |
} |
| 87 |
#endif /* PORTABLE_STRNICMP */ |
| 88 |
|
| 89 |
static mTCHAR *skipleading(const mTCHAR *str) |
| 90 |
{ |
| 91 |
assert(str != NULL); |
| 92 |
while ('\0' < *str && *str <= ' ') |
| 93 |
str++; |
| 94 |
return (mTCHAR *)str; |
| 95 |
} |
| 96 |
|
| 97 |
static mTCHAR *skiptrailing(const mTCHAR *str, const mTCHAR *base) |
| 98 |
{ |
| 99 |
assert(str != NULL); |
| 100 |
assert(base != NULL); |
| 101 |
while (str > base && '\0' < *(str-1) && *(str-1) <= ' ') |
| 102 |
str--; |
| 103 |
return (mTCHAR *)str; |
| 104 |
} |
| 105 |
|
| 106 |
static mTCHAR *striptrailing(mTCHAR *str) |
| 107 |
{ |
| 108 |
mTCHAR *ptr = skiptrailing(_mtcschr(str, '\0'), str); |
| 109 |
assert(ptr != NULL); |
| 110 |
*ptr = '\0'; |
| 111 |
return str; |
| 112 |
} |
| 113 |
|
| 114 |
static mTCHAR *ini_strncpy(mTCHAR *dest, const mTCHAR *source, size_t maxlen, enum quote_option option) |
| 115 |
{ |
| 116 |
size_t d, s; |
| 117 |
|
| 118 |
assert(maxlen>0); |
| 119 |
assert(source != NULL && dest != NULL); |
| 120 |
assert((dest < source || (dest == source && option != QUOTE_ENQUOTE)) || dest > source + strlen(source)); |
| 121 |
if (option == QUOTE_ENQUOTE && maxlen < 3) |
| 122 |
option = QUOTE_NONE; /* cannot store two quotes and a terminating zero in less than 3 characters */ |
| 123 |
|
| 124 |
switch (option) { |
| 125 |
case QUOTE_NONE: |
| 126 |
for (d = 0; d < maxlen - 1 && source[d] != '\0'; d++) |
| 127 |
dest[d] = source[d]; |
| 128 |
assert(d < maxlen); |
| 129 |
dest[d] = '\0'; |
| 130 |
break; |
| 131 |
case QUOTE_ENQUOTE: |
| 132 |
d = 0; |
| 133 |
dest[d++] = '"'; |
| 134 |
for (s = 0; source[s] != '\0' && d < maxlen - 2; s++, d++) { |
| 135 |
if (source[s] == '"') { |
| 136 |
if (d >= maxlen - 3) |
| 137 |
break; /* no space to store the escape character plus the one that follows it */ |
| 138 |
dest[d++] = '\\'; |
| 139 |
} /* if */ |
| 140 |
dest[d] = source[s]; |
| 141 |
} /* for */ |
| 142 |
dest[d++] = '"'; |
| 143 |
dest[d] = '\0'; |
| 144 |
break; |
| 145 |
case QUOTE_DEQUOTE: |
| 146 |
for (d = s = 0; source[s] != '\0' && d < maxlen - 1; s++, d++) { |
| 147 |
if ((source[s] == '"' || source[s] == '\\') && source[s + 1] == '"') |
| 148 |
s++; |
| 149 |
dest[d] = source[s]; |
| 150 |
} /* for */ |
| 151 |
dest[d] = '\0'; |
| 152 |
break; |
| 153 |
default: |
| 154 |
assert(0); |
| 155 |
} /* switch */ |
| 156 |
|
| 157 |
return dest; |
| 158 |
} |
| 159 |
|
| 160 |
static mTCHAR *cleanstring(mTCHAR *string, enum quote_option *quotes) |
| 161 |
{ |
| 162 |
int isstring; |
| 163 |
mTCHAR *ep; |
| 164 |
|
| 165 |
assert(string != NULL); |
| 166 |
assert(quotes != NULL); |
| 167 |
|
| 168 |
/* Remove a trailing comment */ |
| 169 |
isstring = 0; |
| 170 |
for (ep = string; *ep != '\0' && ((*ep != ';' && *ep != '#') || isstring); ep++) { |
| 171 |
if (*ep == '"') { |
| 172 |
if (*(ep + 1) == '"') |
| 173 |
ep++; /* skip "" (both quotes) */ |
| 174 |
else |
| 175 |
isstring = !isstring; /* single quote, toggle isstring */ |
| 176 |
} else if (*ep == '\\' && *(ep + 1) == '"') { |
| 177 |
ep++; /* skip \" (both quotes */ |
| 178 |
} /* if */ |
| 179 |
} /* for */ |
| 180 |
assert(ep != NULL && (*ep == '\0' || *ep == ';' || *ep == '#')); |
| 181 |
*ep = '\0'; /* terminate at a comment */ |
| 182 |
striptrailing(string); |
| 183 |
/* Remove double quotes surrounding a value */ |
| 184 |
*quotes = QUOTE_NONE; |
| 185 |
if (*string == '"' && (ep = _mtcschr(string, '\0')) != NULL && *(ep - 1) == '"') { |
| 186 |
string++; |
| 187 |
*--ep = '\0'; |
| 188 |
*quotes = QUOTE_DEQUOTE; /* this is a string, so remove escaped characters */ |
| 189 |
} /* if */ |
| 190 |
return string; |
| 191 |
} |
| 192 |
|
| 193 |
static int getkeystring(INI_FILETYPE *fp, const mTCHAR *Section, const mTCHAR *Key, |
| 194 |
int idxSection, int idxKey, mTCHAR *Buffer, int BufferSize, |
| 195 |
INI_FILEPOS *mark) |
| 196 |
{ |
| 197 |
mTCHAR *sp, *ep; |
| 198 |
int len, idx; |
| 199 |
enum quote_option quotes; |
| 200 |
mTCHAR LocalBuffer[INI_BUFFERSIZE]; |
| 201 |
|
| 202 |
assert(fp != NULL); |
| 203 |
/* Move through file 1 line at a time until a section is matched or EOF. If |
| 204 |
* parameter Section is NULL, only look at keys above the first section. If |
| 205 |
* idxSection is positive, copy the relevant section name. |
| 206 |
*/ |
| 207 |
len = (Section != NULL) ? (int)_mtcslen(Section) : 0; |
| 208 |
if (len > 0 || idxSection >= 0) { |
| 209 |
assert(idxSection >= 0 || Section != NULL); |
| 210 |
idx = -1; |
| 211 |
do { |
| 212 |
if (!ini_read(LocalBuffer, INI_BUFFERSIZE, fp)) |
| 213 |
return 0; |
| 214 |
sp = skipleading(LocalBuffer); |
| 215 |
ep = _mtcsrchr(sp, ']'); |
| 216 |
} while (*sp != '[' || ep == NULL || |
| 217 |
(((int)(ep-sp-1) != len || Section == NULL || _mtcsnicmp(sp+1,Section,len) != 0) && ++idx != idxSection)); |
| 218 |
if (idxSection >= 0) { |
| 219 |
if (idx == idxSection) { |
| 220 |
assert(ep != NULL); |
| 221 |
assert(*ep == ']'); |
| 222 |
*ep = '\0'; |
| 223 |
ini_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE); |
| 224 |
return 1; |
| 225 |
} /* if */ |
| 226 |
return 0; /* no more section found */ |
| 227 |
} /* if */ |
| 228 |
} /* if */ |
| 229 |
|
| 230 |
/* Now that the section has been found, find the entry. |
| 231 |
* Stop searching upon leaving the section's area. |
| 232 |
*/ |
| 233 |
assert(Key != NULL || idxKey >= 0); |
| 234 |
len = (Key != NULL) ? (int)_mtcslen(Key) : 0; |
| 235 |
idx = -1; |
| 236 |
do { |
| 237 |
if (mark != NULL) |
| 238 |
ini_tell(fp, mark); /* optionally keep the mark to the start of the line */ |
| 239 |
if (!ini_read(LocalBuffer,INI_BUFFERSIZE,fp) || *(sp = skipleading(LocalBuffer)) == '[') |
| 240 |
return 0; |
| 241 |
sp = skipleading(LocalBuffer); |
| 242 |
ep = _mtcschr(sp, '='); /* Parse out the equal sign */ |
| 243 |
if (ep == NULL) |
| 244 |
ep = _mtcschr(sp, ':'); |
| 245 |
} while (*sp == ';' || *sp == '#' || ep == NULL |
| 246 |
|| ((len == 0 || (int)(skiptrailing(ep,sp)-sp) != len || _mtcsnicmp(sp,Key,len) != 0) && ++idx != idxKey)); |
| 247 |
if (idxKey >= 0) { |
| 248 |
if (idx == idxKey) { |
| 249 |
assert(ep != NULL); |
| 250 |
assert(*ep == '=' || *ep == ':'); |
| 251 |
*ep = '\0'; |
| 252 |
striptrailing(sp); |
| 253 |
ini_strncpy(Buffer, sp, BufferSize, QUOTE_NONE); |
| 254 |
return 1; |
| 255 |
} /* if */ |
| 256 |
return 0; /* no more key found (in this section) */ |
| 257 |
} /* if */ |
| 258 |
|
| 259 |
/* Copy up to BufferSize chars to buffer */ |
| 260 |
assert(ep != NULL); |
| 261 |
assert(*ep == '=' || *ep == ':'); |
| 262 |
sp = skipleading(ep + 1); |
| 263 |
sp = cleanstring(sp, "es); /* Remove a trailing comment */ |
| 264 |
ini_strncpy(Buffer, sp, BufferSize, quotes); |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
/** ini_gets() |
| 269 |
* \param Section the name of the section to search for |
| 270 |
* \param Key the name of the entry to find the value of |
| 271 |
* \param DefValue default string in the event of a failed read |
| 272 |
* \param Buffer a pointer to the buffer to copy into |
| 273 |
* \param BufferSize the maximum number of characters to copy |
| 274 |
* \param Filename the name and full path of the .ini file to read from |
| 275 |
* |
| 276 |
* \return the number of characters copied into the supplied buffer |
| 277 |
*/ |
| 278 |
int ini_gets(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *DefValue, |
| 279 |
mTCHAR *Buffer, int BufferSize, const TCHAR *Filename) |
| 280 |
{ |
| 281 |
INI_FILETYPE fp; |
| 282 |
int ok = 0; |
| 283 |
|
| 284 |
if (Buffer == NULL || BufferSize <= 0 || Key == NULL) |
| 285 |
return 0; |
| 286 |
if (ini_openread(Filename, &fp)) { |
| 287 |
ok = getkeystring(&fp, Section, Key, -1, -1, Buffer, BufferSize, NULL); |
| 288 |
(void)ini_close(&fp); |
| 289 |
} /* if */ |
| 290 |
if (!ok) |
| 291 |
ini_strncpy(Buffer, (DefValue != NULL) ? DefValue : _mT(""), BufferSize, QUOTE_NONE); |
| 292 |
return (int)_mtcslen(Buffer); |
| 293 |
} |
| 294 |
|
| 295 |
/** ini_getl() |
| 296 |
* \param Section the name of the section to search for |
| 297 |
* \param Key the name of the entry to find the value of |
| 298 |
* \param DefValue the default value in the event of a failed read |
| 299 |
* \param Filename the name of the .ini file to read from |
| 300 |
* |
| 301 |
* \return the value located at Key |
| 302 |
*/ |
| 303 |
long ini_getl(const mTCHAR *Section, const mTCHAR *Key, long DefValue, const TCHAR *Filename) |
| 304 |
{ |
| 305 |
mTCHAR LocalBuffer[64]; |
| 306 |
int len = ini_gets(Section, Key, _mT(""), LocalBuffer, sizearray(LocalBuffer), Filename); |
| 307 |
return (len == 0) ? DefValue |
| 308 |
: ((len >= 2 && _totupper((int)LocalBuffer[1]) == 'X') ? _mtcstol(LocalBuffer, NULL, 16) |
| 309 |
: _mtcstol(LocalBuffer, NULL, 10)); |
| 310 |
} |
| 311 |
|
| 312 |
#if defined INI_REAL |
| 313 |
/** ini_getf() |
| 314 |
* \param Section the name of the section to search for |
| 315 |
* \param Key the name of the entry to find the value of |
| 316 |
* \param DefValue the default value in the event of a failed read |
| 317 |
* \param Filename the name of the .ini file to read from |
| 318 |
* |
| 319 |
* \return the value located at Key |
| 320 |
*/ |
| 321 |
INI_REAL ini_getf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL DefValue, const TCHAR *Filename) |
| 322 |
{ |
| 323 |
mTCHAR LocalBuffer[64]; |
| 324 |
int len = ini_gets(Section, Key, _mT(""), LocalBuffer, sizearray(LocalBuffer), Filename); |
| 325 |
return (len == 0) ? DefValue : ini_atof(LocalBuffer); |
| 326 |
} |
| 327 |
#endif |
| 328 |
|
| 329 |
/** ini_getbool() |
| 330 |
* \param Section the name of the section to search for |
| 331 |
* \param Key the name of the entry to find the value of |
| 332 |
* \param DefValue default value in the event of a failed read; it should |
| 333 |
* zero (0) or one (1). |
| 334 |
* \param Filename the name and full path of the .ini file to read from |
| 335 |
* |
| 336 |
* A true boolean is found if one of the following is matched: |
| 337 |
* - A string starting with 'y' or 'Y' |
| 338 |
* - A string starting with 't' or 'T' |
| 339 |
* - A string starting with '1' |
| 340 |
* |
| 341 |
* A false boolean is found if one of the following is matched: |
| 342 |
* - A string starting with 'n' or 'N' |
| 343 |
* - A string starting with 'f' or 'F' |
| 344 |
* - A string starting with '0' |
| 345 |
* |
| 346 |
* \return the true/false flag as interpreted at Key |
| 347 |
*/ |
| 348 |
int ini_getbool(const mTCHAR *Section, const mTCHAR *Key, int DefValue, const TCHAR *Filename) |
| 349 |
{ |
| 350 |
mTCHAR LocalBuffer[2] = _mT(""); |
| 351 |
int ret; |
| 352 |
|
| 353 |
ini_gets(Section, Key, _mT(""), LocalBuffer, sizearray(LocalBuffer), Filename); |
| 354 |
LocalBuffer[0] = (mTCHAR)_totupper((int)LocalBuffer[0]); |
| 355 |
if (LocalBuffer[0] == 'Y' || LocalBuffer[0] == '1' || LocalBuffer[0] == 'T') |
| 356 |
ret = 1; |
| 357 |
else if (LocalBuffer[0] == 'N' || LocalBuffer[0] == '0' || LocalBuffer[0] == 'F') |
| 358 |
ret = 0; |
| 359 |
else |
| 360 |
ret = DefValue; |
| 361 |
|
| 362 |
return(ret); |
| 363 |
} |
| 364 |
|
| 365 |
/** ini_getsection() |
| 366 |
* \param idx the zero-based sequence number of the section to return |
| 367 |
* \param Buffer a pointer to the buffer to copy into |
| 368 |
* \param BufferSize the maximum number of characters to copy |
| 369 |
* \param Filename the name and full path of the .ini file to read from |
| 370 |
* |
| 371 |
* \return the number of characters copied into the supplied buffer |
| 372 |
*/ |
| 373 |
int ini_getsection(int idx, mTCHAR *Buffer, int BufferSize, const TCHAR *Filename) |
| 374 |
{ |
| 375 |
INI_FILETYPE fp; |
| 376 |
int ok = 0; |
| 377 |
|
| 378 |
if (Buffer == NULL || BufferSize <= 0 || idx < 0) |
| 379 |
return 0; |
| 380 |
if (ini_openread(Filename, &fp)) { |
| 381 |
ok = getkeystring(&fp, NULL, NULL, idx, -1, Buffer, BufferSize, NULL); |
| 382 |
(void)ini_close(&fp); |
| 383 |
} /* if */ |
| 384 |
if (!ok) |
| 385 |
*Buffer = '\0'; |
| 386 |
return (int)_mtcslen(Buffer); |
| 387 |
} |
| 388 |
|
| 389 |
/** ini_getkey() |
| 390 |
* \param Section the name of the section to browse through, or NULL to |
| 391 |
* browse through the keys outside any section |
| 392 |
* \param idx the zero-based sequence number of the key to return |
| 393 |
* \param Buffer a pointer to the buffer to copy into |
| 394 |
* \param BufferSize the maximum number of characters to copy |
| 395 |
* \param Filename the name and full path of the .ini file to read from |
| 396 |
* |
| 397 |
* \return the number of characters copied into the supplied buffer |
| 398 |
*/ |
| 399 |
int ini_getkey(const mTCHAR *Section, int idx, mTCHAR *Buffer, int BufferSize, const TCHAR *Filename) |
| 400 |
{ |
| 401 |
INI_FILETYPE fp; |
| 402 |
int ok = 0; |
| 403 |
|
| 404 |
if (Buffer == NULL || BufferSize <= 0 || idx < 0) |
| 405 |
return 0; |
| 406 |
if (ini_openread(Filename, &fp)) { |
| 407 |
ok = getkeystring(&fp, Section, NULL, -1, idx, Buffer, BufferSize, NULL); |
| 408 |
(void)ini_close(&fp); |
| 409 |
} /* if */ |
| 410 |
if (!ok) |
| 411 |
*Buffer = '\0'; |
| 412 |
return (int)_mtcslen(Buffer); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
#if !defined INI_NOBROWSE |
| 417 |
/** ini_browse() |
| 418 |
* \param Callback a pointer to a function that will be called for every |
| 419 |
* setting in the INI file. |
| 420 |
* \param UserData arbitrary data, which the function passes on the |
| 421 |
* \c Callback function |
| 422 |
* \param Filename the name and full path of the .ini file to read from |
| 423 |
* |
| 424 |
* \return 1 on success, 0 on failure (INI file not found) |
| 425 |
* |
| 426 |
* \note The \c Callback function must return 1 to continue |
| 427 |
* browsing through the INI file, or 0 to stop. Even when the |
| 428 |
* callback stops the browsing, this function will return 1 |
| 429 |
* (for success). |
| 430 |
*/ |
| 431 |
int ini_browse(INI_CALLBACK Callback, void *UserData, const TCHAR *Filename) |
| 432 |
{ |
| 433 |
mTCHAR LocalBuffer[INI_BUFFERSIZE]; |
| 434 |
int lenSec, lenKey; |
| 435 |
enum quote_option quotes; |
| 436 |
INI_FILETYPE fp; |
| 437 |
|
| 438 |
if (Callback == NULL) |
| 439 |
return 0; |
| 440 |
if (!ini_openread(Filename, &fp)) |
| 441 |
return 0; |
| 442 |
|
| 443 |
LocalBuffer[0] = '\0'; /* copy an empty section in the buffer */ |
| 444 |
lenSec = (int)_mtcslen(LocalBuffer) + 1; |
| 445 |
for ( ;; ) { |
| 446 |
mTCHAR *sp, *ep; |
| 447 |
if (!ini_read(LocalBuffer + lenSec, INI_BUFFERSIZE - lenSec, &fp)) |
| 448 |
break; |
| 449 |
sp = skipleading(LocalBuffer + lenSec); |
| 450 |
/* ignore empty strings and comments */ |
| 451 |
if (*sp == '\0' || *sp == ';' || *sp == '#') |
| 452 |
continue; |
| 453 |
/* see whether we reached a new section */ |
| 454 |
ep = _mtcsrchr(sp, ']'); |
| 455 |
if (*sp == '[' && ep != NULL) { |
| 456 |
*ep = '\0'; |
| 457 |
ini_strncpy(LocalBuffer, sp + 1, INI_BUFFERSIZE, QUOTE_NONE); |
| 458 |
lenSec = (int)_mtcslen(LocalBuffer) + 1; |
| 459 |
continue; |
| 460 |
} /* if */ |
| 461 |
/* not a new section, test for a key/value pair */ |
| 462 |
ep = _mtcschr(sp, '='); /* test for the equal sign or colon */ |
| 463 |
if (ep == NULL) |
| 464 |
ep = _mtcschr(sp, ':'); |
| 465 |
if (ep == NULL) |
| 466 |
continue; /* invalid line, ignore */ |
| 467 |
*ep++ = '\0'; /* split the key from the value */ |
| 468 |
striptrailing(sp); |
| 469 |
ini_strncpy(LocalBuffer + lenSec, sp, INI_BUFFERSIZE - lenSec, QUOTE_NONE); |
| 470 |
lenKey = (int)_mtcslen(LocalBuffer + lenSec) + 1; |
| 471 |
/* clean up the value */ |
| 472 |
sp = skipleading(ep); |
| 473 |
sp = cleanstring(sp, "es); /* Remove a trailing comment */ |
| 474 |
ini_strncpy(LocalBuffer + lenSec + lenKey, sp, INI_BUFFERSIZE - lenSec - lenKey, quotes); |
| 475 |
/* call the callback */ |
| 476 |
if (!Callback(LocalBuffer, LocalBuffer + lenSec, LocalBuffer + lenSec + lenKey, UserData)) |
| 477 |
break; |
| 478 |
} /* for */ |
| 479 |
|
| 480 |
(void)ini_close(&fp); |
| 481 |
return 1; |
| 482 |
} |
| 483 |
#endif /* INI_NOBROWSE */ |
| 484 |
|
| 485 |
#if ! defined INI_READONLY |
| 486 |
static void ini_tempname(mTCHAR *dest, const mTCHAR *source, int maxlength) |
| 487 |
{ |
| 488 |
mTCHAR *p; |
| 489 |
|
| 490 |
ini_strncpy(dest, source, maxlength, QUOTE_NONE); |
| 491 |
p = _mtcschr(dest, '\0'); |
| 492 |
assert(p != NULL); |
| 493 |
*(p - 1) = '~'; |
| 494 |
} |
| 495 |
|
| 496 |
static enum quote_option check_enquote(const mTCHAR *Value) |
| 497 |
{ |
| 498 |
const mTCHAR *p; |
| 499 |
|
| 500 |
/* run through the value, if it has trailing spaces, or '"', ';' or '#' |
| 501 |
* characters, enquote it |
| 502 |
*/ |
| 503 |
assert(Value != NULL); |
| 504 |
for (p = Value; *p != '\0' && *p != '"' && *p != ';' && *p != '#'; p++) |
| 505 |
/* nothing */; |
| 506 |
return (*p != '\0' || (p > Value && *(p - 1) == ' ')) ? QUOTE_ENQUOTE : QUOTE_NONE; |
| 507 |
} |
| 508 |
|
| 509 |
static void writesection(mTCHAR *LocalBuffer, const mTCHAR *Section, INI_FILETYPE *fp) |
| 510 |
{ |
| 511 |
if (Section != NULL && _mtcslen(Section) > 0) { |
| 512 |
mTCHAR *p; |
| 513 |
LocalBuffer[0] = '['; |
| 514 |
ini_strncpy(LocalBuffer + 1, Section, INI_BUFFERSIZE - 4, QUOTE_NONE); /* -1 for '[', -1 for ']', -2 for '\r\n' */ |
| 515 |
p = _mtcschr(LocalBuffer, '\0'); |
| 516 |
assert(p != NULL); |
| 517 |
*p++ = ']'; |
| 518 |
_mtcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */ |
| 519 |
if (fp != NULL) |
| 520 |
(void)ini_write(LocalBuffer, fp); |
| 521 |
} /* if */ |
| 522 |
} |
| 523 |
|
| 524 |
static void writekey(mTCHAR *LocalBuffer, const mTCHAR *Key, const mTCHAR *Value, INI_FILETYPE *fp) |
| 525 |
{ |
| 526 |
mTCHAR *p; |
| 527 |
enum quote_option option = check_enquote(Value); |
| 528 |
ini_strncpy(LocalBuffer, Key, INI_BUFFERSIZE - 3, QUOTE_NONE); /* -1 for '=', -2 for '\r\n' */ |
| 529 |
p = _mtcschr(LocalBuffer, '\0'); |
| 530 |
assert(p != NULL); |
| 531 |
*p++ = '='; |
| 532 |
ini_strncpy(p, Value, INI_BUFFERSIZE - (p - LocalBuffer) - 2, option); /* -2 for '\r\n' */ |
| 533 |
p = _mtcschr(LocalBuffer, '\0'); |
| 534 |
assert(p != NULL); |
| 535 |
_mtcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */ |
| 536 |
if (fp != NULL) |
| 537 |
(void)ini_write(LocalBuffer, fp); |
| 538 |
} |
| 539 |
|
| 540 |
static int cache_accum(const mTCHAR *string, int *size, int max) |
| 541 |
{ |
| 542 |
int len = (int)_mtcslen(string); |
| 543 |
if (*size + len >= max) |
| 544 |
return 0; |
| 545 |
*size += len; |
| 546 |
return 1; |
| 547 |
} |
| 548 |
|
| 549 |
static int cache_flush(mTCHAR *buffer, int *size, |
| 550 |
INI_FILETYPE *rfp, INI_FILETYPE *wfp, INI_FILEPOS *mark) |
| 551 |
{ |
| 552 |
int terminator_len = (int)_mtcslen(INI_LINETERM); |
| 553 |
int pos = 0; |
| 554 |
|
| 555 |
(void)ini_seek(rfp, mark); |
| 556 |
assert(buffer != NULL); |
| 557 |
buffer[0] = '\0'; |
| 558 |
assert(size != NULL); |
| 559 |
assert(*size <= INI_BUFFERSIZE); |
| 560 |
while (pos < *size) { |
| 561 |
(void)ini_read(buffer + pos, INI_BUFFERSIZE - pos, rfp); |
| 562 |
while (pos < *size && buffer[pos] != '\0') |
| 563 |
pos++; /* cannot use _mtcslen() because buffer may not be zero-terminated */ |
| 564 |
} /* while */ |
| 565 |
if (buffer[0] != '\0') { |
| 566 |
assert(pos > 0 && pos <= INI_BUFFERSIZE); |
| 567 |
if (pos == INI_BUFFERSIZE) |
| 568 |
pos--; |
| 569 |
buffer[pos] = '\0'; /* force zero-termination (may be left unterminated in the above while loop) */ |
| 570 |
(void)ini_write(buffer, wfp); |
| 571 |
} |
| 572 |
ini_tell(rfp, mark); /* update mark */ |
| 573 |
*size = 0; |
| 574 |
/* return whether the buffer ended with a line termination */ |
| 575 |
return (pos > terminator_len) && (_mtcscmp(buffer + pos - terminator_len, INI_LINETERM) == 0); |
| 576 |
} |
| 577 |
|
| 578 |
static int close_rename(INI_FILETYPE *rfp, INI_FILETYPE *wfp, const TCHAR *filename, mTCHAR *buffer) |
| 579 |
{ |
| 580 |
(void)ini_close(rfp); |
| 581 |
(void)ini_close(wfp); |
| 582 |
(void)ini_tempname(buffer, filename, INI_BUFFERSIZE); |
| 583 |
#if defined ini_remove || defined INI_REMOVE |
| 584 |
(void)ini_remove(filename); |
| 585 |
#endif |
| 586 |
(void)ini_rename(buffer, filename); |
| 587 |
return 1; |
| 588 |
} |
| 589 |
|
| 590 |
/** ini_puts() |
| 591 |
* \param Section the name of the section to write the string in |
| 592 |
* \param Key the name of the entry to write, or NULL to erase all keys in the section |
| 593 |
* \param Value a pointer to the buffer the string, or NULL to erase the key |
| 594 |
* \param Filename the name and full path of the .ini file to write to |
| 595 |
* |
| 596 |
* \return 1 if successful, otherwise 0 |
| 597 |
*/ |
| 598 |
int ini_puts(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const TCHAR *Filename) |
| 599 |
{ |
| 600 |
INI_FILETYPE rfp; |
| 601 |
INI_FILETYPE wfp; |
| 602 |
INI_FILEPOS mark; |
| 603 |
INI_FILEPOS head, tail; |
| 604 |
mTCHAR *sp, *ep; |
| 605 |
mTCHAR LocalBuffer[INI_BUFFERSIZE]; |
| 606 |
int len, match, flag, cachelen; |
| 607 |
|
| 608 |
assert(Filename != NULL); |
| 609 |
if (!ini_openread(Filename, &rfp)) { |
| 610 |
/* If the .ini file doesn't exist, make a new file */ |
| 611 |
if (Key != NULL && Value != NULL) { |
| 612 |
if (!ini_openwrite(Filename, &wfp)) |
| 613 |
return 0; |
| 614 |
writesection(LocalBuffer, Section, &wfp); |
| 615 |
writekey(LocalBuffer, Key, Value, &wfp); |
| 616 |
(void)ini_close(&wfp); |
| 617 |
} /* if */ |
| 618 |
return 1; |
| 619 |
} /* if */ |
| 620 |
|
| 621 |
/* If parameters Key and Value are valid (so this is not an "erase" request) |
| 622 |
* and the setting already exists, there are two short-cuts to avoid rewriting |
| 623 |
* the INI file. |
| 624 |
*/ |
| 625 |
if (Key != NULL && Value != NULL) { |
| 626 |
match = getkeystring(&rfp, Section, Key, -1, -1, LocalBuffer, sizearray(LocalBuffer), &head); |
| 627 |
if (match) { |
| 628 |
/* if the current setting is identical to the one to write, there is |
| 629 |
* nothing to do. |
| 630 |
*/ |
| 631 |
if (_mtcscmp(LocalBuffer,Value) == 0) { |
| 632 |
(void)ini_close(&rfp); |
| 633 |
return 1; |
| 634 |
} /* if */ |
| 635 |
/* if the new setting has the same length as the current setting, and the |
| 636 |
* glue file permits file read/write access, we can modify in place. |
| 637 |
*/ |
| 638 |
#if defined ini_openrewrite || defined INI_OPENREWRITE |
| 639 |
/* we already have the start of the (raw) line, get the end too */ |
| 640 |
ini_tell(&rfp, &tail); |
| 641 |
/* create new buffer (without writing it to file) */ |
| 642 |
writekey(LocalBuffer, Key, Value, NULL); |
| 643 |
if (_mtcslen(LocalBuffer) == (size_t)(tail - head)) { |
| 644 |
/* length matches, close the file & re-open for read/write, then |
| 645 |
* write at the correct position |
| 646 |
*/ |
| 647 |
(void)ini_close(&rfp); |
| 648 |
if (!ini_openrewrite(Filename, &wfp)) |
| 649 |
return 0; |
| 650 |
(void)ini_seek(&wfp, &head); |
| 651 |
(void)ini_write(LocalBuffer, &wfp); |
| 652 |
(void)ini_close(&wfp); |
| 653 |
return 1; |
| 654 |
} /* if */ |
| 655 |
#endif |
| 656 |
} /* if */ |
| 657 |
/* key not found, or different value & length -> proceed */ |
| 658 |
} else if (Key != NULL && Value == NULL) { |
| 659 |
/* Conversely, for a request to delete a setting; if that setting isn't |
| 660 |
present, just return */ |
| 661 |
match = getkeystring(&rfp, Section, Key, -1, -1, LocalBuffer, sizearray(LocalBuffer), NULL); |
| 662 |
if (!match) { |
| 663 |
(void)ini_close(&rfp); |
| 664 |
return 1; |
| 665 |
} /* if */ |
| 666 |
/* key found -> proceed to delete it */ |
| 667 |
} /* if */ |
| 668 |
|
| 669 |
/* Get a temporary file name to copy to. Use the existing name, but with |
| 670 |
* the last character set to a '~'. |
| 671 |
*/ |
| 672 |
(void)ini_close(&rfp); |
| 673 |
ini_tempname(LocalBuffer, Filename, INI_BUFFERSIZE); |
| 674 |
if (!ini_openwrite(LocalBuffer, &wfp)) |
| 675 |
return 0; |
| 676 |
/* In the case of (advisory) file locks, ini_openwrite() may have been blocked |
| 677 |
* on the open, and after the block is lifted, the original file may have been |
| 678 |
* renamed, which is why the original file was closed and is now reopened */ |
| 679 |
if (!ini_openread(Filename, &rfp)) { |
| 680 |
/* If the .ini file doesn't exist any more, make a new file */ |
| 681 |
assert(Key != NULL && Value != NULL); |
| 682 |
writesection(LocalBuffer, Section, &wfp); |
| 683 |
writekey(LocalBuffer, Key, Value, &wfp); |
| 684 |
(void)ini_close(&wfp); |
| 685 |
return 1; |
| 686 |
} /* if */ |
| 687 |
|
| 688 |
(void)ini_tell(&rfp, &mark); |
| 689 |
cachelen = 0; |
| 690 |
|
| 691 |
/* Move through the file one line at a time until a section is |
| 692 |
* matched or until EOF. Copy to temp file as it is read. |
| 693 |
*/ |
| 694 |
len = (Section != NULL) ? (int)_mtcslen(Section) : 0; |
| 695 |
if (len > 0) { |
| 696 |
do { |
| 697 |
if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) { |
| 698 |
/* Failed to find section, so add one to the end */ |
| 699 |
flag = cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 700 |
if (Key!=NULL && Value!=NULL) { |
| 701 |
if (!flag) |
| 702 |
(void)ini_write(INI_LINETERM, &wfp); /* force a new line behind the last line of the INI file */ |
| 703 |
writesection(LocalBuffer, Section, &wfp); |
| 704 |
writekey(LocalBuffer, Key, Value, &wfp); |
| 705 |
} /* if */ |
| 706 |
return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */ |
| 707 |
} /* if */ |
| 708 |
/* Copy the line from source to dest, but not if this is the section that |
| 709 |
* we are looking for and this section must be removed |
| 710 |
*/ |
| 711 |
sp = skipleading(LocalBuffer); |
| 712 |
ep = _mtcsrchr(sp, ']'); |
| 713 |
match = (*sp == '[' && ep != NULL && (int)(ep-sp-1) == len && _mtcsnicmp(sp + 1,Section,len) == 0); |
| 714 |
if (!match || Key != NULL) { |
| 715 |
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) { |
| 716 |
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 717 |
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); |
| 718 |
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); |
| 719 |
} /* if */ |
| 720 |
} /* if */ |
| 721 |
} while (!match); |
| 722 |
} /* if */ |
| 723 |
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 724 |
/* when deleting a section, the section head that was just found has not been |
| 725 |
* copied to the output file, but because this line was not "accumulated" in |
| 726 |
* the cache, the position in the input file was reset to the point just |
| 727 |
* before the section; this must now be skipped (again) |
| 728 |
*/ |
| 729 |
if (Key == NULL) { |
| 730 |
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); |
| 731 |
(void)ini_tell(&rfp, &mark); |
| 732 |
} /* if */ |
| 733 |
|
| 734 |
/* Now that the section has been found, find the entry. Stop searching |
| 735 |
* upon leaving the section's area. Copy the file as it is read |
| 736 |
* and create an entry if one is not found. |
| 737 |
*/ |
| 738 |
len = (Key != NULL) ? (int)_mtcslen(Key) : 0; |
| 739 |
for( ;; ) { |
| 740 |
if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) { |
| 741 |
/* EOF without an entry so make one */ |
| 742 |
flag = cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 743 |
if (Key!=NULL && Value!=NULL) { |
| 744 |
if (!flag) |
| 745 |
(void)ini_write(INI_LINETERM, &wfp); /* force a new line behind the last line of the INI file */ |
| 746 |
writekey(LocalBuffer, Key, Value, &wfp); |
| 747 |
} /* if */ |
| 748 |
return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */ |
| 749 |
} /* if */ |
| 750 |
sp = skipleading(LocalBuffer); |
| 751 |
ep = _mtcschr(sp, '='); /* Parse out the equal sign */ |
| 752 |
if (ep == NULL) |
| 753 |
ep = _mtcschr(sp, ':'); |
| 754 |
match = (ep != NULL && len > 0 && (int)(skiptrailing(ep,sp)-sp) == len && _mtcsnicmp(sp,Key,len) == 0); |
| 755 |
if ((Key != NULL && match) || *sp == '[') |
| 756 |
break; /* found the key, or found a new section */ |
| 757 |
/* copy other keys in the section */ |
| 758 |
if (Key == NULL) { |
| 759 |
(void)ini_tell(&rfp, &mark); /* we are deleting the entire section, so update the read position */ |
| 760 |
} else { |
| 761 |
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) { |
| 762 |
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 763 |
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); |
| 764 |
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); |
| 765 |
} /* if */ |
| 766 |
} /* if */ |
| 767 |
} /* for */ |
| 768 |
/* the key was found, or we just dropped on the next section (meaning that it |
| 769 |
* wasn't found); in both cases we need to write the key, but in the latter |
| 770 |
* case, we also need to write the line starting the new section after writing |
| 771 |
* the key |
| 772 |
*/ |
| 773 |
flag = (*sp == '['); |
| 774 |
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 775 |
if (Key != NULL && Value != NULL) |
| 776 |
writekey(LocalBuffer, Key, Value, &wfp); |
| 777 |
/* cache_flush() reset the "read pointer" to the start of the line with the |
| 778 |
* previous key or the new section; read it again (because writekey() destroyed |
| 779 |
* the buffer) |
| 780 |
*/ |
| 781 |
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); |
| 782 |
if (flag) { |
| 783 |
/* the new section heading needs to be copied to the output file */ |
| 784 |
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); |
| 785 |
} else { |
| 786 |
/* forget the old key line */ |
| 787 |
(void)ini_tell(&rfp, &mark); |
| 788 |
} /* if */ |
| 789 |
/* Copy the rest of the INI file */ |
| 790 |
while (ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) { |
| 791 |
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) { |
| 792 |
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 793 |
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); |
| 794 |
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); |
| 795 |
} /* if */ |
| 796 |
} /* while */ |
| 797 |
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); |
| 798 |
return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */ |
| 799 |
} |
| 800 |
|
| 801 |
/* Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C" book. */ |
| 802 |
#define ABS(v) ((v) < 0 ? -(v) : (v)) |
| 803 |
|
| 804 |
static void strreverse(mTCHAR *str) |
| 805 |
{ |
| 806 |
int i, j; |
| 807 |
for (i = 0, j = (int)_mtcslen(str) - 1; i < j; i++, j--) { |
| 808 |
mTCHAR t = str[i]; |
| 809 |
str[i] = str[j]; |
| 810 |
str[j] = t; |
| 811 |
} /* for */ |
| 812 |
} |
| 813 |
|
| 814 |
static void long2str(long value, mTCHAR *str) |
| 815 |
{ |
| 816 |
int i = 0; |
| 817 |
long sign = value; |
| 818 |
|
| 819 |
/* generate digits in reverse order */ |
| 820 |
do { |
| 821 |
int n = (int)(value % 10); /* get next lowest digit */ |
| 822 |
str[i++] = (mTCHAR)(ABS(n) + '0'); /* handle case of negative digit */ |
| 823 |
} while (value /= 10); /* delete the lowest digit */ |
| 824 |
if (sign < 0) |
| 825 |
str[i++] = '-'; |
| 826 |
str[i] = '\0'; |
| 827 |
|
| 828 |
strreverse(str); |
| 829 |
} |
| 830 |
|
| 831 |
/** ini_putl() |
| 832 |
* \param Section the name of the section to write the value in |
| 833 |
* \param Key the name of the entry to write |
| 834 |
* \param Value the value to write |
| 835 |
* \param Filename the name and full path of the .ini file to write to |
| 836 |
* |
| 837 |
* \return 1 if successful, otherwise 0 |
| 838 |
*/ |
| 839 |
int ini_putl(const mTCHAR *Section, const mTCHAR *Key, long Value, const TCHAR *Filename) |
| 840 |
{ |
| 841 |
mTCHAR LocalBuffer[32]; |
| 842 |
long2str(Value, LocalBuffer); |
| 843 |
return ini_puts(Section, Key, LocalBuffer, Filename); |
| 844 |
} |
| 845 |
|
| 846 |
#if defined INI_REAL |
| 847 |
/** ini_putf() |
| 848 |
* \param Section the name of the section to write the value in |
| 849 |
* \param Key the name of the entry to write |
| 850 |
* \param Value the value to write |
| 851 |
* \param Filename the name and full path of the .ini file to write to |
| 852 |
* |
| 853 |
* \return 1 if successful, otherwise 0 |
| 854 |
*/ |
| 855 |
int ini_putf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL Value, const TCHAR *Filename) |
| 856 |
{ |
| 857 |
mTCHAR LocalBuffer[64]; |
| 858 |
ini_ftoa(LocalBuffer, Value); |
| 859 |
return ini_puts(Section, Key, LocalBuffer, Filename); |
| 860 |
} |
| 861 |
#endif /* INI_REAL */ |
| 862 |
#endif /* !INI_READONLY */ |