00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "rcs_defs.hh"
00023
00024 #include "inifile.h"
00025
00026
00027 #if !defined(INIFILE_USE_INET_FILES) && !defined(NO_STDIO)
00028 #include <stdio.h>
00029 #endif
00030
00031 #include <string.h>
00032 #include <ctype.h>
00033
00034
00035
00036 static const char *
00037 afterequal (const char *string)
00038 {
00039 const char *spot = string;
00040
00041 for (;;)
00042 {
00043 if (*spot == '=')
00044 {
00045
00046 for (;;)
00047 {
00048 spot++;
00049 if (0 == *spot)
00050 {
00051
00052 return NULL;
00053 }
00054 else if (*spot != ' ' && *spot != '\t' && *spot != '\r'
00055 && *spot != '\n')
00056 {
00057
00058 return spot;
00059 }
00060 else
00061 {
00062
00063 continue;
00064 }
00065 }
00066 }
00067 else if (*spot == 0)
00068 {
00069
00070 return NULL;
00071 }
00072 else
00073 {
00074
00075 spot++;
00076 continue;
00077 }
00078 }
00079 }
00080
00081
00082 static char *
00083 skipwhite (char *string)
00084 {
00085 for (;;)
00086 {
00087 if (*string == 0)
00088 {
00089 return NULL;
00090 }
00091
00092 if (*string == COMMENT_CHAR)
00093 {
00094 return NULL;
00095 }
00096
00097 if (*string != ' ' && *string != '\t' && *string != '\r'
00098 && *string != '\n')
00099 {
00100 return string;
00101 }
00102
00103 string++;
00104 }
00105 }
00106
00107
00108
00109
00110
00111 static int
00112 findSection (void *fp, const char *section)
00113 {
00114 static char line[INIFILE_MAX_LINELEN + 2];
00115 static char bracketsection[INIFILE_MAX_LINELEN + 2];
00116 char *nonwhite;
00117
00118
00119 if (NULL == fp)
00120 {
00121 return -1;
00122 }
00123
00124
00125 #ifndef INIFILE_USE_INET_FILES
00126 rewind ((FILE *) fp);
00127 #else
00128 inet_file_rewind ((INET_FILE *) fp);
00129 #endif
00130
00131
00132 if (NULL == section)
00133 {
00134 return 0;
00135 }
00136
00137
00138 #ifndef NO_STDIO
00139 sprintf (bracketsection, "[%s]", section);
00140 #else
00141 strcpy (bracketsection, "[");
00142 strcat (bracketsection, section);
00143 strcat (bracketsection, "]");
00144 #endif
00145
00146
00147 #ifndef INIFILE_USE_INET_FILES
00148 while (!feof ((FILE *) fp))
00149 #else
00150 while (!inet_file_eof ((INET_FILE *) fp))
00151 #endif
00152 {
00153
00154 #ifndef INIFILE_USE_INET_FILES
00155 if (NULL == fgets (line, INIFILE_MAX_LINELEN + 1, (FILE *) fp))
00156 {
00157
00158 return -1;
00159 }
00160 #else
00161 if (NULL ==
00162 inet_file_gets (line, INIFILE_MAX_LINELEN + 1, (INET_FILE *) fp))
00163 {
00164
00165 return -1;
00166 }
00167 #endif
00168
00169
00170 if (NULL == (nonwhite = skipwhite (line)))
00171 {
00172
00173 continue;
00174 }
00175
00176
00177 if (0 != strncmp (bracketsection, nonwhite, strlen (bracketsection)))
00178 {
00179
00180 continue;
00181 }
00182
00183
00184 return 0;
00185 }
00186
00187
00188 return -1;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 const char *
00209 iniFind (void *fp, const char *tag, const char *section)
00210 {
00211 static char line[INIFILE_MAX_LINELEN + 2];
00212 static char bracketsection[INIFILE_MAX_LINELEN + 2];
00213 char *nonwhite;
00214 int newlinepos;
00215 int len;
00216 char tagend;
00217 char *value_string;
00218 char *end_value_string;
00219
00220 if (NULL == fp)
00221 return NULL;
00222
00223
00224 #ifndef INIFILE_USE_INET_FILES
00225 rewind ((FILE *) fp);
00226 #else
00227 inet_file_rewind ((INET_FILE *) fp);
00228 #endif
00229
00230
00231
00232 if (NULL != section)
00233 {
00234 #ifndef NO_STDIO
00235 sprintf (bracketsection, "[%s]", section);
00236 #else
00237 strcpy (bracketsection, "[");
00238 strcat (bracketsection, section);
00239 strcat (bracketsection, "]");
00240 #endif
00241
00242
00243
00244 #ifndef INIFILE_USE_INET_FILES
00245 while (!feof ((FILE *) fp))
00246 #else
00247 while (!inet_file_eof ((INET_FILE *) fp))
00248 #endif
00249 {
00250
00251
00252 #ifndef INIFILE_USE_INET_FILES
00253 if (NULL == fgets (line, INIFILE_MAX_LINELEN + 1, (FILE *) fp))
00254 {
00255
00256 return NULL;
00257 }
00258 #else
00259 if (NULL ==
00260 inet_file_gets (line, INIFILE_MAX_LINELEN + 1,
00261 (INET_FILE *) fp))
00262 {
00263
00264 return NULL;
00265 }
00266 #endif
00267
00268
00269
00270 newlinepos = strlen (line) - 1;
00271 if (newlinepos < 0)
00272 {
00273 newlinepos = 0;
00274 }
00275 if (line[newlinepos] == '\n')
00276 {
00277 line[newlinepos] = 0;
00278 }
00279
00280 if (NULL == (nonwhite = skipwhite (line)))
00281 {
00282
00283 continue;
00284 }
00285
00286
00287 if (0 !=
00288 strncmp (bracketsection, nonwhite, strlen (bracketsection)))
00289 {
00290
00291 continue;
00292 }
00293
00294
00295 break;
00296 }
00297 }
00298
00299 #ifndef INIFILE_USE_INET_FILES
00300 while (!feof ((FILE *) fp))
00301 #else
00302 while (!inet_file_eof ((INET_FILE *) fp))
00303 #endif
00304 {
00305
00306 #ifndef INIFILE_USE_INET_FILES
00307 if (NULL == fgets (line, INIFILE_MAX_LINELEN + 1, (FILE *) fp))
00308 {
00309
00310 return NULL;
00311 }
00312 #else
00313 if (NULL ==
00314 inet_file_gets (line, INIFILE_MAX_LINELEN + 1, (INET_FILE *) fp))
00315 {
00316
00317 return NULL;
00318 }
00319 #endif
00320
00321
00322
00323
00324 newlinepos = strlen (line) - 1;
00325 if (newlinepos < 0)
00326 {
00327 newlinepos = 0;
00328 }
00329 if (line[newlinepos] == '\n')
00330 {
00331 line[newlinepos] = 0;
00332 }
00333
00334
00335 if (NULL == (nonwhite = skipwhite (line)))
00336 {
00337
00338 continue;
00339 }
00340
00341
00342
00343 if (NULL != section && nonwhite[0] == '[')
00344 {
00345 return NULL;
00346 }
00347
00348 len = strlen (tag);
00349
00350 if (0 != strncmp (tag, nonwhite, len))
00351 {
00352
00353 continue;
00354 }
00355
00356
00357
00358 tagend = nonwhite[len];
00359 if (tagend == ' ' || tagend == '\r' || tagend == '\t' || tagend == '\n'
00360 || tagend == '=')
00361 {
00362
00363 nonwhite += len;
00364 value_string = (char *) afterequal (nonwhite);
00365
00366 if (NULL == value_string)
00367 {
00368 return NULL;
00369 }
00370 end_value_string = value_string + strlen (value_string) - 1;
00371 while (*end_value_string == ' ' || *end_value_string == '\t'
00372 || *end_value_string == '\r')
00373 {
00374 *end_value_string = 0;
00375 end_value_string--;
00376 }
00377 return value_string;
00378 }
00379
00380 }
00381
00382 return NULL;
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 int
00396 iniSection (void *fp, const char *section, INIFILE_ENTRY array[], int max)
00397 {
00398 static char line[INIFILE_MAX_LINELEN + 2];
00399 int count = 0;
00400 char *nonwhite;
00401 int newlinepos;
00402 const char *entry;
00403 #ifdef UNDER_CE
00404 int i = 0;
00405 #endif
00406
00407 if (NULL == fp)
00408 {
00409 return -1;
00410 }
00411
00412
00413 if (-1 == findSection (fp, section))
00414 {
00415
00416 return -1;
00417 }
00418
00419
00420
00421 #ifndef INIFILE_USE_INET_FILES
00422 while (!feof ((FILE *) fp) &&
00423 #else
00424 while (!inet_file_eof ((INET_FILE *) fp) &&
00425 #endif
00426 count < max)
00427 {
00428 #ifndef INIFILE_USE_INET_FILES
00429 if (NULL == fgets (line, INIFILE_MAX_LINELEN + 1, (FILE *) fp))
00430 {
00431
00432 return count;
00433 }
00434 #else
00435 if (NULL ==
00436 inet_file_gets (line, INIFILE_MAX_LINELEN + 1, (INET_FILE *) fp))
00437 {
00438
00439 return count;
00440 }
00441 #endif
00442
00443
00444 if (NULL == (nonwhite = skipwhite (line)))
00445 {
00446 continue;
00447 }
00448
00449
00450 if ('[' == *nonwhite)
00451 {
00452 return count;
00453 }
00454
00455
00456 newlinepos = strlen (line) - 1;
00457 if (newlinepos < 0)
00458 {
00459 newlinepos = 0;
00460 }
00461 if (line[newlinepos] == '\n')
00462 {
00463 line[newlinepos] = 0;
00464 }
00465
00466
00467
00468
00469 #ifndef UNDER_CE
00470 sscanf (line, "%s", array[count].tag);
00471 #else
00472 i = 0;
00473 while (line[i] != 0 && line[i] != ' ' && line[i] != '\t'
00474 && line[i] != '\r')
00475 {
00476 array[count].tag[i] = line[i];
00477 i++;
00478 }
00479 array[count].tag[i] = 0;
00480 #endif
00481
00482
00483
00484 entry = afterequal (line);
00485 if (NULL != entry)
00486 {
00487 strcpy (array[count].rest, afterequal (line));
00488 }
00489 else
00490 {
00491 array[count].rest[0] = 0;
00492 }
00493 count++;
00494 }
00495
00496
00497 return count;
00498 }