1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*
Library for inifile like data files.
Copyright (C) 2006 Olaf Klein, o.b.klein@gpsbabel.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "inifile.h"
#define MYNAME "inifile"
typedef struct inifile_entry_s {
queue Q;
char* key;
char* val;
} inifile_entry_t;
typedef struct inifile_section_s {
queue Q;
char* name;
int ientries;
queue entries;
} inifile_section_t;
/* internal procedures */
#define START_BUFSIZE 257
#define DELTA_BUFSIZE 128
#define GPSBABEL_INIFILE "gpsbabel.ini"
/* Remember the filename we used so we can include it in errors. */
char* gbinipathname;
static char*
find_gpsbabel_inifile(const char* path) /* can be empty or NULL */
{
FILE* test;
char* buff;
int len;
if (path == NULL) {
return NULL;
}
len = strlen(path);
buff = (char*) xmalloc(len + 1 + strlen(GPSBABEL_INIFILE) + 1);
strcpy(buff, path);
if (len > 0) {
char test = buff[len - 1];
#ifdef __WIN32__
if ((test != '\\') && (test != ':')) {
strcat(buff, "\\");
}
#else
if (test != '/') {
strcat(buff, "/");
}
#endif
}
strcat(buff, GPSBABEL_INIFILE);
test = fopen(buff, "rb");
if (test) {
fclose(test);
return buff;
}
xfree(buff);
return NULL;
}
static gbfile*
open_gpsbabel_inifile(void)
{
char* name;
char* envstr;
gbfile* res = NULL;
envstr = getenv("GPSBABELINI");
if (envstr != NULL) {
FILE* test;
test = fopen(envstr, "r");
if (test != NULL) {
fclose(test);
return gbfopen(envstr, "r", "GPSBabel");
}
warning("WARNING: GPSBabel-inifile, defined in environment, NOT found!\n");
return NULL;
}
name = find_gpsbabel_inifile(""); /* PWD */
if (name == NULL) {
#ifdef __WIN32__
name = find_gpsbabel_inifile(getenv("APPDATA"));
if (name == NULL) {
name = find_gpsbabel_inifile(getenv("WINDIR"));
}
if (name == NULL) {
name = find_gpsbabel_inifile(getenv("SYSTEMROOT"));
}
#else
if ((envstr = getenv("HOME")) != NULL) {
char* path;
path = (char*) xmalloc(strlen(envstr) + 11);
strcpy(path, envstr);
strcat(path, "/.gpsbabel");
name = find_gpsbabel_inifile(path);
xfree(path);
}
if (name == NULL) {
name = find_gpsbabel_inifile("/usr/local/etc");
}
if (name == NULL) {
name = find_gpsbabel_inifile("/etc");
}
#endif
}
if (name != NULL) {
res = gbfopen(name, "r", "GPSBabel");
if (gbinipathname) {
xfree(gbinipathname);
}
gbinipathname = name;
}
return res;
}
static void
inifile_load_file(gbfile* fin, inifile_t* inifile, const char* myname)
{
char* buf;
inifile_section_t* sec = NULL;
int line = 0;
while ((buf = gbfgetstr(fin))) {
char* cin = lrtrim(buf);
if ((line++ == 0) && fin->unicode) {
inifile->unicode = 1;
}
if (*cin == '\0') {
continue; /* skip empty lines */
}
if ((*cin == '#') || (*cin == ';')) {
continue; /* skip comments */
}
if (*cin == '[') {
char* cend = strchr(++cin, ']');
if (cend != NULL) {
*cend = '\0';
cin = lrtrim(cin);
}
if ((*cin == '\0') || (cend == NULL)) {
fatal("%s: invalid section header '%s' in '%s'.\n", myname, cin, gbinipathname);
}
sec = (inifile_section_t*) xcalloc(1, sizeof(*sec));
sec->name = xstrdup(cin);
QUEUE_INIT(&sec->entries);
ENQUEUE_TAIL(&inifile->secs, &sec->Q);
inifile->isecs++;
} else {
char* cx;
inifile_entry_t* entry;
if (sec == NULL) {
fatal("%s: missing section header in '%s'.\n", myname,gbinipathname);
}
entry = (inifile_entry_t*) xcalloc(1, sizeof(*entry));
ENQUEUE_TAIL(&sec->entries, &entry->Q);
sec->ientries++;
cx = strchr(cin, '=');
if (cx != NULL) {
*cx = '\0';
cin = lrtrim(cin);
}
entry->key = xstrdup(cin);
if (cx != NULL) {
cx = lrtrim(++cx);
entry->val = xstrdup(cx);
} else {
entry->val = xstrdup("");
}
}
}
}
static char*
inifile_find_value(const inifile_t* inifile, const char* sec_name, const char* key)
{
queue* elem, *tmp;
if (inifile == NULL) {
return NULL;
}
QUEUE_FOR_EACH(&inifile->secs, elem, tmp) {
inifile_section_t* sec = (inifile_section_t*) elem;
if (case_ignore_strcmp(sec->name, sec_name) == 0) {
queue* elem, *tmp;
QUEUE_FOR_EACH(&sec->entries, elem, tmp) {
inifile_entry_t* entry = (inifile_entry_t*) elem;
if (case_ignore_strcmp(entry->key, key) == 0) {
return entry->val;
}
}
}
}
return NULL;
}
/* public procedures */
/*
inifile_init:
reads inifile filename into memory
myname represents the calling module
filename == NULL: try to open global gpsbabel.ini
*/
inifile_t*
inifile_init(const char* filename, const char* myname)
{
inifile_t* result;
gbfile* fin = NULL;
if (filename == NULL) {
fin = open_gpsbabel_inifile();
if (fin == NULL) {
return NULL;
}
} else {
fin = gbfopen(filename, "rb", myname);
}
result = (inifile_t*) xcalloc(1, sizeof(*result));
QUEUE_INIT(&result->secs);
inifile_load_file(fin, result, myname);
gbfclose(fin);
return result;
}
void
inifile_done(inifile_t* inifile)
{
if (inifile == NULL) {
return;
}
if (inifile->isecs > 0) {
queue* elem, *tmp;
QUEUE_FOR_EACH(&inifile->secs, elem, tmp) {
inifile_section_t* sec = (inifile_section_t*) elem;
if (sec->ientries > 0) {
queue* elem, *tmp;
QUEUE_FOR_EACH(&sec->entries, elem, tmp) {
inifile_entry_t* entry = (inifile_entry_t*) elem;
if (entry->key) {
xfree(entry->key);
}
if (entry->val) {
xfree(entry->val);
}
dequeue(elem);
xfree(entry);
}
}
dequeue(elem);
if (sec->name) {
xfree(sec->name);
}
xfree(sec);
}
xfree(inifile);
}
if (gbinipathname) {
xfree(gbinipathname);
gbinipathname = NULL;
}
}
int
inifile_has_section(const inifile_t* inifile, const char* section)
{
queue* elem, *tmp;
QUEUE_FOR_EACH(&inifile->secs, elem, tmp) {
inifile_section_t* sec = (inifile_section_t*) elem;
if (case_ignore_strcmp(sec->name, section) == 0) {
return 1;
}
}
return 0;
}
/*
inifile_readstr:
returns NULL if not found, otherwise a pointer to the value of key ...
all key values are valid entities until "inifile_done"
*/
char*
inifile_readstr(const inifile_t* inifile, const char* section, const char* key)
{
return inifile_find_value(inifile, section, key);
}
/*
inifile_readint:
on success the value is stored into "*value" and "inifile_readint" returns 1,
otherwise inifile_readint returns 0
*/
int
inifile_readint(const inifile_t* inifile, const char* section, const char* key, int* value)
{
char* str;
str = inifile_find_value(inifile, section, key);
if (str == NULL) {
return 0;
}
if (value != NULL) {
*value = atoi(str);
}
return 1;
}
/*
inifile_readint_def:
if found inifile_readint_def returns value of key, otherwise a default value "def"
*/
int
inifile_readint_def(const inifile_t* inifile, const char* section, const char* key, const int def)
{
int result;
if (inifile_readint(inifile, section, key, &result) == 0) {
return def;
} else {
return result;
}
}
|