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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
|
/* inifile.c
Copyright (C) 2007-2011 Dmitry Groshev
This file is part of mtPaint.
mtPaint 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 3 of the License, or
(at your option) any later version.
mtPaint 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 mtPaint in the file COPYING.
*/
/* *** PREFACE ***
* This implementation has no comment preservation, and has nested sections -
* because this is how we like our inifiles. :-)
* Allocations are done in slabs, because no slot is ever deallocated or
* reordered; only modified string values are allocated singly, since it is
* probable they will keep being modified.
* Implementation uses one 32-bit hash function as two 16-bit ones while
* possible, but with 40000 keys or more, has to evaluate two 32-bit functions
* instead. But such loads are expected to be rare. - WJ */
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include "global.h"
#include "memory.h"
#include "inifile.h"
/* Make code not compile where it cannot run */
typedef char Integers_Do_Not_Fit_Into_Pointers[2 * (sizeof(int) <= sizeof(char *)) - 1];
#define SLAB_INCREMENT 16384
#define SLAB_RESERVED 64 /* Reserved for allocator overhead */
#define INI_LIMIT 0x40000000 /* Limit imposed by hashing scheme */
#define HASHFILL(X) ((X) * 3) /* Hash occupancy no more than 1/3 */
#define HASHSEED 0x811C9DC5
#define HASH_RND(X) ((X) * 0x10450405 + 1)
#define HASH_MIN 256 /* Minimum hash capacity, so that it won't be too tiny */
/* Slot types */
#define INI_NONE 0
#define INI_UNDEF 1
#define INI_STR 2
#define INI_INT 3
#define INI_BOOL 4
// Negative types are section nesting levels
/* Slot flags */
#define INI_STRING 0x0003 /* Index of strings block - must be the lower bits */
#define INI_SYSTEM 0x0000 /* Systemwide inifile */
#define INI_USER 0x0001 /* User inifile */
#define INI_NEW 0x0002 /* Memory only*/
#define INI_MALLOC 0x0004 /* Value is allocated (not in strings block) */
#define INI_DEFAULT 0x0008 /* Default value is defined */
#define SLOT_NAME(I,S) ((I)->sblock[(S)->flags & INI_STRING] + (S)->key)
/* Escaping is needed for leading tabs and spaces, and for inline CR and LF */
static char *escape_string(char *src)
{
static const char *escaped = "\t\r\n \\";
char c, *cp, *tmp, *t2;
if ((src[0] != '\t') && (src[0] != ' ') && !src[strcspn(src, "\r\n")])
return (NULL);
t2 = tmp = calloc(1, strlen(src) * 2 + 1);
if (tmp)
{
while ((c = *src++))
{
if ((cp = strchr(escaped, c)))
{
*t2++ = '\\';
c = "trn \\"[cp - escaped];
}
*t2++ = c;
}
*t2 = '\0';
}
return (tmp);
}
#if GTK_MAJOR_VERSION == 1
/* GLib 1.2 doesn't provide g_strcompress() */
static void unescape_string(char *buf)
{
#define NUM_ESCAPES 5
static const char escapes[] = "bfnrt01234567";
static const char escaped[] = { 7, 12, 10, 13, 8, 0, 1, 2, 3, 4, 5, 6, 7 };
char c, cc, *tmp, *src = buf;
int v;
while ((c = *src++))
{
if (c == '\\')
{
c = *src++;
if (!c) break;
while ((tmp = strchr(escapes, c)))
{
v = tmp - escapes;
c = escaped[v];
if (v >= NUM_ESCAPES)
{
// Octal escape
cc = *src - '0';
if (cc & ~7) break;
c = c * 8 + cc;
cc = *(++src) - '0';
if (cc & ~7) break;
c = c * 8 + cc;
++src;
}
break;
}
}
*buf++ = c;
}
*buf = '\0';
#undef NUM_ESCAPES
}
#else
static void unescape_string(char *buf)
{
char *tmp = g_strcompress(buf);
strcpy(buf, tmp);
g_free(tmp);
}
#endif
/* Thomas Wang's and "One at a time" hash functions */
static guint32 hashf(guint32 seed, int section, char *key)
{
seed += section;
seed = (seed << 15) + ~seed;
seed ^= (seed >> 12);
seed += seed << 2;
seed ^= seed >> 4;
seed *= 2057;
seed ^= seed >> 16;
for (; *key; key++)
{
seed += *key;
seed += seed << 10;
seed ^= seed >> 6;
}
seed += seed << 3;
seed ^= seed >> 11;
seed += seed << 15;
return (seed);
}
static int cuckoo_insert(inifile *inip, inislot *slotp)
{
static const unsigned char shift[4] = { 0, 16, 0, 0 };
gint32 idx, tmp;
guint32 key;
int i, j, d;
/* Update section's last slot index */
i = slotp->sec;
j = slotp - inip->slots;
if (i && (inip->slots[i - 1].defv < j)) inip->slots[i - 1].defv = j;
/* Decide if using one-key mode */
d = inip->seed[0] == inip->seed[1] ? 0 : 2;
/* Normal cuckoo process */
idx = (slotp - inip->slots) + 1;
for (i = 0; i < inip->maxloop; i++)
{
key = hashf(inip->seed[i & 1], slotp->sec, SLOT_NAME(inip, slotp));
key >>= shift[(i & 1) + d];
j = (key & inip->hmask) * 2 + (i & 1);
tmp = inip->hash[j];
inip->hash[j] = idx;
idx = tmp;
if (!idx) return (TRUE);
slotp = inip->slots + (idx - 1);
}
return (FALSE);
}
static int resize_hash(inifile *inip, int cnt)
{
int len;
if (!cnt) return (0); /* Degenerate case */
len = nextpow2(HASHFILL(cnt) - 1);
if (len <= inip->hmask * 2 + 2) return (0); /* Large enough */
free(inip->hash);
inip->hash = calloc(1, len * sizeof(gint32));
if (!inip->hash) return (-1); /* Failure */
inip->hmask = (len >> 1) - 1;
inip->maxloop = ceil(3.0 * log(len / 2) /
log((double)(len / 2) / cnt));
return (1); /* Resized */
}
static int rehash(inifile *inip)
{
int i, flag;
flag = resize_hash(inip, inip->count);
if (flag < 0) return (FALSE); /* Failed */
while (TRUE) /* Until done */
{
if (!flag) /* No size change */
{
inip->seed[0] = inip->seed[1] = HASH_RND(inip->seed[0]);
memset(inip->hash, 0, (inip->hmask + 1) * 2 * sizeof(gint32));
}
/* Enter two-key mode */
if (inip->hmask > 0xFFFF) inip->seed[1] = HASH_RND(inip->seed[0]);
/* Re-insert items */
for (i = 0; (i < inip->count) &&
(flag = cuckoo_insert(inip, inip->slots + i)); i++);
if (flag) return (TRUE);
}
}
static inislot *cuckoo_find(inifile *inip, int section, char *name)
{
inislot *slotp;
guint32 key;
gint32 i;
int j = 0;
key = hashf(inip->seed[0], section, name);
while (TRUE)
{
i = inip->hash[(key & inip->hmask) * 2 + j];
if (i)
{
slotp = inip->slots + i - 1;
if ((slotp->sec == section) &&
!strcmp(name, SLOT_NAME(inip, slotp)))
return (slotp);
}
if (j++) return (NULL);
if (inip->seed[0] == inip->seed[1]) key >>= 16;
else key = hashf(inip->seed[1], section, name);
}
}
static inislot *add_slot(inifile *inip)
{
inislot *ra;
int i, j;
if (inip->count >= INI_LIMIT) return (NULL); /* Too many entries */
/* Extend the slab if needed */
i = inip->count * sizeof(inislot) + SLAB_RESERVED + SLAB_INCREMENT - 1;
j = (i + sizeof(inislot)) / SLAB_INCREMENT;
if (i / SLAB_INCREMENT < j)
{
ra = realloc(inip->slots, j * SLAB_INCREMENT - SLAB_RESERVED);
if (!ra) return (NULL);
inip->slots = ra;
}
ra = inip->slots + inip->count++;
memset(ra, 0, sizeof(inislot));
return (ra);
}
static char *store_string(inifile *inip, char *str)
{
int i, j, l;
char *ra;
/* Zero offset for zero length */
if (!str || !*str) return (inip->sblock[INI_NEW]);
/* Extend the slab if needed */
l = strlen(str) + 1;
i = inip->slen + SLAB_RESERVED + SLAB_INCREMENT - 1;
j = (i + l) / SLAB_INCREMENT;
if (i / SLAB_INCREMENT < j)
{
ra = realloc(inip->sblock[INI_NEW], j * SLAB_INCREMENT - SLAB_RESERVED);
if (!ra) return (NULL);
inip->sblock[INI_NEW] = ra;
}
ra = inip->sblock[INI_NEW] + inip->slen;
memcpy(ra, str, l);
inip->slen += l;
return (ra);
}
static inislot *key_slot(inifile *inip, int section, char *key, int type)
{
inislot *slot;
if ((type < 0) && section) type = inip->slots[section - 1].type - 1;
slot = cuckoo_find(inip, section, key);
if (slot)
{
if (type == INI_NONE) return (slot);
if (slot->flags & INI_MALLOC)
{
free(slot->value);
slot->flags ^= INI_MALLOC;
}
#if VALIDATE_TYPE
if ((slot->type != type) && (slot->type > INI_UNDEF))
g_warning("INI key '%s' changed type\n", key);
#endif
}
else
{
key = store_string(inip, key);
if (!key) return (NULL);
slot = add_slot(inip);
if (!slot) return (NULL);
slot->sec = section;
slot->key = key - inip->sblock[INI_NEW];
slot->flags = INI_NEW;
if (!cuckoo_insert(inip, slot) && !rehash(inip))
return (NULL);
}
slot->type = type;
return (slot);
}
int new_ini(inifile *inip)
{
memset(inip, 0, sizeof(inifile));
inip->sblock[INI_NEW] = malloc(SLAB_INCREMENT - SLAB_RESERVED);
if (!inip->sblock[INI_NEW]) return (FALSE);
inip->sblock[INI_NEW][0] = 0;
inip->slen = 1;
inip->slots = malloc(SLAB_INCREMENT - SLAB_RESERVED);
inip->seed[0] = inip->seed[1] = HASHSEED;
return (inip->slots && (resize_hash(inip, HASH_MIN) > 0));
}
void forget_ini(inifile *inip)
{
int i;
for (i = 0; i < inip->count; i++)
{
if (inip->slots[i].flags & INI_MALLOC)
free(inip->slots[i].value);
}
free(inip->sblock[INI_SYSTEM]);
free(inip->sblock[INI_USER]);
free(inip->sblock[INI_NEW]);
free(inip->slots);
free(inip->hash);
memset(inip, 0, sizeof(inifile));
}
/* Load file whole into memory, with a zero byte before and two after */
char *slurp_file(char *fname)
{
FILE *fp;
char *buf;
int i, l;
if (!fname || !(fp = fopen(fname, "rb"))) return (NULL);
fseek(fp, 0, SEEK_END);
l = ftell(fp);
buf = calloc(1, l + 3);
if (buf)
{
fseek(fp, 0, SEEK_SET);
i = fread(buf + 1, 1, l, fp);
if (i != l)
{
free(buf);
buf = NULL;
}
}
fclose(fp);
return (buf);
}
int read_ini(inifile *inip, char *fname, int itype)
{
inifile ini;
inislot *slot;
char *tmp, *wrk, *w2, *str;
int i, j, l, q, sec = 0;
/* Read the file */
tmp = slurp_file(fname);
if (!tmp) return (0);
ini = *inip;
ini.sblock[itype] = tmp;
/* Parse the contents */
for (tmp++; ; tmp = str)
{
tmp += strspn(tmp, "\r\n\t ");
if (!*tmp) break;
str = tmp + strcspn(tmp, "\r\n");
if (*str) *str++ = '\0';
if ((*tmp == ';') || (*tmp == '#')) continue; /* Comment */
if (*tmp == '[') /* Section */
{
if (!(w2 = strchr(tmp + 1, ']'))) goto error;
for (l = i = 0; tmp[i + 1] == '>'; i++);
if (i) /* Nested section */
{
if (!sec || ((j = i + ini.slots[sec - 1].type) > 0))
goto error;
l = sec;
while (j--) l = ini.slots[l - 1].sec;
}
*w2 = '\0';
/* Hash this */
slot = cuckoo_find(&ini, l, tmp + ++i);
if (!slot) /* New section */
{
slot = add_slot(&ini);
if (!slot) goto fail;
slot->type = -i;
slot->sec = l;
slot->key = (tmp - ini.sblock[itype]) + i;
slot->flags = itype;
if (!cuckoo_insert(&ini, slot) && !rehash(&ini))
goto fail;
}
sec = slot - ini.slots + 1; /* Activate */
continue;
}
/* Variable (spaces in name allowed) */
w2 = strchr(tmp, '=');
if (!w2)
{
error: g_printerr("Wrong INI line: '%s'\n", tmp);
continue;
}
for (wrk = w2 - 1; wrk - tmp >= 0; wrk--)
if ((*wrk != ' ') && (*wrk != '\t')) break;
wrk[1] = '\0';
q = *(++w2) == '='; /* "==" means quoted value */
w2 += q + strspn(w2 + q, "\t ");
// for (wrk = str - 1; *wrk && ((*wrk == '\t') || (*wrk == ' '); *wrk-- = '\0');
/* Hash this pair */
slot = cuckoo_find(&ini, sec, tmp);
if (!slot) /* New key */
{
slot = add_slot(&ini);
if (!slot) goto fail;
slot->sec = sec;
slot->key = tmp - ini.sblock[itype];
slot->flags = itype;
if (!cuckoo_insert(&ini, slot) && !rehash(&ini))
goto fail;
}
if (q) unescape_string(w2);
slot->type = INI_UNDEF;
slot->value = w2;
}
/* Return the result */
*inip = ini;
return (1);
/* Catastrophic failure - unable to add key */
fail: forget_ini(&ini);
*inip = ini;
return (-1);
}
int write_ini(inifile *inip, char *fname, char *header)
{
FILE *fp;
inislot *slotp;
char *name, *sv, *xv;
int i, j, max, sec, var, defv;
if (!(fp = fopen(fname, "w"))) return (FALSE);
if (header) fprintf(fp, "%s\n", header);
i = sec = 0; var = 0;
while (TRUE)
{
max = sec ? inip->slots[sec - 1].defv : inip->count - 1;
for (; i <= max; i++)
{
slotp = inip->slots + i;
/* Only the current section */
if (slotp->sec != sec) continue;
/* Variables first, subsections second */
if ((slotp->type < 0) ^ var) continue;
name = SLOT_NAME(inip, slotp);
if (slotp->type < 0) /* Section */
{
if (slotp->defv <= i) continue; /* It's empty */
fputc('[', fp);
for (j = -1; j > slotp->type; j--) fputc('>', fp);
fprintf(fp, "%s]\n", name);
sec = i + 1; max = slotp->defv; var = 0;
continue;
}
/* Keys from system inifile ignore defaults, because
* they exist to override the defaults - WJ */
defv = ((slotp->flags & INI_STRING) != INI_SYSTEM) &&
(slotp->flags & INI_DEFAULT);
sv = slotp->value ? slotp->value : "";
switch (slotp->type)
{
case INI_STR:
if (defv && !strcmp(inip->sblock[INI_NEW] +
slotp->defv, sv)) break;
case INI_UNDEF:
default:
/* Escape value if needed */
if ((xv = escape_string(sv)))
{
fprintf(fp, "%s == %s\n", name, xv);
free(xv);
}
else fprintf(fp, "%s = %s\n", name, sv);
break;
case INI_INT:
if (defv && ((int)slotp->value == slotp->defv)) break;
fprintf(fp, "%s = %d\n", name, (int)slotp->value);
break;
case INI_BOOL:
if (defv && (!!slotp->value == !!slotp->defv)) break;
fprintf(fp, "%s = %s\n", name, slotp->value ?
"true" : "false");
break;
}
}
i = sec;
if (!var) var = 1; /* Process subsections now */
else if (!sec) break; /* All done */
/* Return to scanning for parent's subsections */
else sec = inip->slots[sec - 1].sec;
}
fclose(fp);
return (TRUE);
}
int ini_setstr(inifile *inip, int section, char *key, char *value)
{
inislot *slot;
if (!(slot = key_slot(inip, section, key, INI_STR))) return (FALSE);
slot->value = "";
/* NULLs are stored as empty strings, for less hazardous handling */
if (!value);
/* Uncomment this instead if want NULLs to remain NULLs */
// if (!value) slot->value = NULL;
else if (!*value);
else
{
value = strdup(value);
if (!value) return (FALSE);
slot->value = value;
slot->flags |= INI_MALLOC;
}
return (TRUE);
}
int ini_setint(inifile *inip, int section, char *key, int value)
{
inislot *slot;
if (!(slot = key_slot(inip, section, key, INI_INT))) return (FALSE);
slot->value = (char *)value;
return (TRUE);
}
int ini_setbool(inifile *inip, int section, char *key, int value)
{
inislot *slot;
if (!(slot = key_slot(inip, section, key, INI_BOOL))) return (FALSE);
slot->value = (char *)!!value;
return (TRUE);
}
char *ini_getstr(inifile *inip, int section, char *key, char *defv)
{
inislot *slot;
char *tail;
/* NULLs are stored as empty strings, for less hazardous handling */
if (!defv) defv = "";
/* Comment out the above if want NULLs to remain NULLs */
/* Read existing */
slot = key_slot(inip, section, key, INI_NONE);
if (!slot) return (defv);
if (slot->type == INI_STR)
{
#if VALIDATE_DEF
if ((slot->flags & INI_DEFAULT) &&
strcmp(defv ? defv : "", inip->sblock[INI_NEW] + slot->defv))
g_warning("INI key '%s' new default\n", key);
#endif
if (slot->flags & INI_DEFAULT) return (slot->value);
slot->type = INI_UNDEF; /* Fall through to storing default */
}
/* Store default */
tail = store_string(inip, defv);
if (tail)
{
slot->defv = tail - inip->sblock[INI_NEW];
slot->flags |= INI_DEFAULT;
}
else /* Cannot store the default */
{
slot->defv = 0;
slot->flags &= ~INI_DEFAULT;
}
if (slot->type != INI_UNDEF)
{
#if VALIDATE_TYPE
if (slot->type != INI_NONE)
g_printerr("INI key '%s' wrong type\n", key);
#endif
if (!defv) slot->value = NULL;
else if (!*defv) slot->value = "";
else
{
slot->value = strdup(defv);
if (slot->value) slot->flags |= INI_MALLOC;
}
}
slot->type = INI_STR;
return (slot->value);
}
int ini_getint(inifile *inip, int section, char *key, int defv)
{
inislot *slot;
char *tail;
long l;
/* Read existing */
slot = key_slot(inip, section, key, INI_NONE);
if (!slot) return (defv);
if (slot->type == INI_INT)
{
#if VALIDATE_DEF
if ((slot->flags & INI_DEFAULT) && (defv != slot->defv))
g_warning("INI key '%s' new default\n", key);
#endif
if (slot->flags & INI_DEFAULT) return ((int)(slot->value));
/* Fall through to storing default */
}
/* Store default */
slot->defv = defv;
slot->flags |= INI_DEFAULT;
while (slot->type != INI_INT)
{
if (slot->type == INI_UNDEF)
{
l = strtol(slot->value, &tail, 10);
slot->value = (void *)l;
if (!*tail) break;
}
else if (slot->flags & INI_MALLOC) free(slot->value);
#if VALIDATE_TYPE
if (slot->type != INI_NONE)
g_printerr("INI key '%s' wrong type\n", key);
#endif
slot->value = (char *)defv;
break;
}
slot->type = INI_INT;
return ((int)(slot->value));
}
int ini_getbool(inifile *inip, int section, char *key, int defv)
{
static const char *YN[] = { "n", "y", "0", "1", "no", "yes",
"off", "on", "false", "true", "disabled", "enabled", NULL };
inislot *slot;
int i;
defv = !!defv;
/* Read existing */
slot = key_slot(inip, section, key, INI_NONE);
if (!slot) return (defv);
if (slot->type == INI_BOOL)
{
#if VALIDATE_DEF
if ((slot->flags & INI_DEFAULT) && (defv != slot->defv))
g_warning("INI key '%s' new default\n", key);
#endif
if (slot->flags & INI_DEFAULT) return ((int)(slot->value));
/* Fall through to storing default */
}
/* Store default */
slot->defv = defv;
slot->flags |= INI_DEFAULT;
while (slot->type != INI_BOOL)
{
if (slot->type == INI_UNDEF)
{
for (i = 0; YN[i] && strcasecmp(YN[i], slot->value); i++);
slot->value = (char *)(i & 1);
if (YN[i]) break;
}
else if (slot->flags & INI_MALLOC) free(slot->value);
#if VALIDATE_TYPE
if (slot->type != INI_NONE)
g_printerr("INI key '%s' wrong type\n", key);
#endif
slot->value = (char *)defv;
break;
}
slot->type = INI_BOOL;
return ((int)(slot->value));
}
int ini_setsection(inifile *inip, int section, char *key)
{
inislot *slot;
slot = key_slot(inip, section, key, -1);
if (!slot) return (-1);
return (slot - inip->slots + 1);
}
int ini_getsection(inifile *inip, int section, char *key)
{
inislot *slot;
int type;
type = section ? inip->slots[section - 1].type - 1 : -1;
slot = cuckoo_find(inip, section, key);
if (!slot || (slot->type != type)) return (-1);
return (slot - inip->slots + 1);
}
#ifdef WIN32
char *get_home_directory(void)
{
static char *homedir = NULL;
if (homedir) return homedir;
homedir = getenv("USERPROFILE"); // Gets the current users home directory in WinXP
if (!homedir) homedir = ""; // And this, in Win9x :-)
return homedir;
}
char *extend_path(const char *path)
{
char *dir, *name;
if (path[0] == '~')
return (g_strdup_printf("%s%s", get_home_directory(), path + 1));
if (path[0] && (path[1] == ':')) // Path w/drive letter is absolute
return (g_strdup(path));
name = g_win32_get_package_installation_directory(NULL, NULL);
dir = g_locale_from_utf8(name, -1, NULL, NULL, NULL);
g_free(name);
name = g_strdup_printf("%s%s", dir, path);
g_free(dir);
return (name);
}
#else
#include <unistd.h>
#include <pwd.h>
/*
* This function came from mhWaveEdit
* by Magnus Hjorth, 2003.
*/
gchar *get_home_directory(void)
{
static char *homedir = NULL;
struct passwd *p;
if (homedir) return homedir;
homedir = getenv("HOME");
if (!homedir)
{
p = getpwuid(getuid());
if (p) homedir = p->pw_dir;
}
if (!homedir)
{
g_warning(_("Could not find home directory. Using current directory as "
"home directory."));
homedir = ".";
}
return homedir;
}
char *extend_path(const char *path)
{
if (path[0] == '~')
return (g_strdup_printf("%s%s", get_home_directory(), path + 1));
return (g_strdup(path));
}
#endif
/* Compatibility functions */
static inifile main_ini;
static char *main_ininame;
void inifile_init(char *system_ini, char *user_ini)
{
char *tmp, *ini = user_ini;
int res, mask = 3;
while (new_ini(&main_ini))
{
if ((mask & 1) && system_ini)
{
tmp = extend_path(system_ini);
res = read_ini(&main_ini, tmp, INI_SYSTEM);
g_free(tmp);
if (res <= 0) mask ^= 1; // Don't try again if failed
if (res < 0) continue; // Restart if struct got deleted
/* !!! Allow system inifile to relocate user inifile */
ini = inifile_get("userINI", user_ini);
if (!ini[0]) ini = system_ini;
}
if ((mask & 2) && user_ini)
{
main_ininame = extend_path(ini);
res = read_ini(&main_ini, main_ininame, INI_USER);
if (res <= 0) // Failed
{
mask ^= 2;
if (res < 0) continue;
}
}
break;
}
}
void inifile_quit()
{
write_ini(&main_ini, main_ininame,
"# Remove this file to restore default settings.\n");
forget_ini(&main_ini);
g_free(main_ininame);
}
char *inifile_get(char *setting, char *defaultValue)
{
return (ini_getstr(&main_ini, 0, setting, defaultValue));
}
int inifile_get_gint32(char *setting, int defaultValue)
{
return (ini_getint(&main_ini, 0, setting, defaultValue));
}
int inifile_get_gboolean(char *setting, int defaultValue)
{
return (ini_getbool(&main_ini, 0, setting, defaultValue));
}
int inifile_set(char *setting, char *value)
{
return (ini_setstr(&main_ini, 0, setting, value));
}
int inifile_set_gint32(char *setting, int value)
{
return (ini_setint(&main_ini, 0, setting, value));
}
int inifile_set_gboolean(char *setting, int value)
{
return (ini_setbool(&main_ini, 0, setting, value));
}
|