project_files/frontlib/util/inihelper.c
author LocutusOfBorg
Thu, 12 Jan 2017 11:40:28 -0500
changeset 12117 262d24da6fb8
parent 10017 de822cd3df3a
permissions -rw-r--r--
aaand italian
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    20
#include "inihelper.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    21
#include "../iniparser/dictionary.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    22
#include "../iniparser/iniparser.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    23
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    24
#include "logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    25
#include "util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    26
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    27
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    28
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    29
#include <ctype.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    30
#include <limits.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    31
#include <errno.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    32
#include <stdarg.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    33
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    34
struct _flib_ini {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    35
    dictionary *inidict;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    36
    char *currentSection;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    37
};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    38
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    39
static char *createDictKey(const char *sectionName, const char *keyName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    40
    return flib_asprintf("%s:%s", sectionName, keyName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    41
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    42
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    43
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    44
 * Turns a string into a lowercase string, in-place.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    45
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    46
static void strToLower(char *str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    47
    if(str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    48
        while(*str) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    49
            *str = tolower(*str);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    50
            str++;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    51
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    52
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    53
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    54
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    55
flib_ini *flib_ini_create(const char *filename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    56
    flib_ini *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    57
    flib_ini *tmpIni = flib_calloc(1, sizeof(flib_ini));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    58
    if(tmpIni) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    59
        if(filename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    60
            tmpIni->inidict = iniparser_load(filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    61
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    62
        if(!tmpIni->inidict) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    63
            tmpIni->inidict = dictionary_new(0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    64
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    65
        if(tmpIni->inidict) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    66
            result = tmpIni;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    67
            tmpIni = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    68
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    69
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    70
    flib_ini_destroy(tmpIni);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    71
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    72
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    73
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    74
flib_ini *flib_ini_load(const char *filename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    75
    flib_ini *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    76
    if(!log_badargs_if(filename==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    77
        flib_ini *tmpIni = flib_calloc(1, sizeof(flib_ini));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    78
        if(tmpIni) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    79
            tmpIni->inidict = iniparser_load(filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    80
            if(tmpIni->inidict) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    81
                result = tmpIni;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    82
                tmpIni = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    83
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    84
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    85
        flib_ini_destroy(tmpIni);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    86
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    87
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    88
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    89
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    90
int flib_ini_save(flib_ini *ini, const char *filename) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    91
    int result = INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    92
    if(!log_badargs_if2(ini==NULL, filename==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    93
        FILE *file = fopen(filename, "wb");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    94
        if(!file) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    95
            flib_log_e("Error opening file \"%s\" for writing.", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    96
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    97
            iniparser_dump_ini(ini->inidict, file);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    98
            if(fclose(file)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
    99
                flib_log_e("Write error on ini file \"%s\"", filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   100
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   101
                result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   102
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   103
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   104
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   105
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   106
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   107
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   108
void flib_ini_destroy(flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   109
    if(ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   110
        if(ini->inidict) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   111
            iniparser_freedict(ini->inidict);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   112
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   113
        free(ini->currentSection);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   114
        free(ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   115
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   116
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   117
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   118
int flib_ini_enter_section(flib_ini *ini, const char *section) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   119
    int result = INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   120
    if(ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   121
        free(ini->currentSection);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   122
        ini->currentSection = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   123
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   124
    if(!log_badargs_if2(ini==NULL, section==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   125
        if(!iniparser_find_entry(ini->inidict, section)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   126
            flib_log_d("Ini section %s not found", section);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   127
            result = INI_ERROR_NOTFOUND;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   128
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   129
            ini->currentSection = flib_strdupnull(section);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   130
            if(ini->currentSection) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   131
                // Usually iniparser ignores case, but some section-handling functions don't,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   132
                // so we set it to lowercase manually
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   133
                strToLower(ini->currentSection);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   134
                result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   135
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   136
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   137
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   138
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   139
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   140
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   141
int flib_ini_create_section(flib_ini *ini, const char *section) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   142
    int result = INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   143
    if(!log_badargs_if2(ini==NULL, section==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   144
        result = flib_ini_enter_section(ini, section);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   145
        if(result == INI_ERROR_NOTFOUND) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   146
            if(iniparser_set(ini->inidict, section, NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   147
                flib_log_e("Error creating ini section %s", section);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   148
                result = INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   149
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   150
                result = flib_ini_enter_section(ini, section);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   151
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   152
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   153
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   154
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   155
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   156
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   157
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   158
 * The result is an internal string of the iniparser, don't free it.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   159
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   160
static char *findValue(dictionary *dict, const char *section, const char *key) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   161
    char *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   162
    char *dictKey = createDictKey(section, key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   163
    if(dictKey) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   164
        result = iniparser_getstring(dict, dictKey, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   165
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   166
    free(dictKey);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   167
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   168
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   169
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   170
int flib_ini_get_str(flib_ini *ini, char **outVar, const char *key) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   171
    char *tmpValue = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   172
    int result = flib_ini_get_str_opt(ini, &tmpValue, key, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   173
    if(result==0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   174
        if(tmpValue == NULL) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   175
            result = INI_ERROR_NOTFOUND;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   176
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   177
            *outVar = tmpValue;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   178
            tmpValue = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   179
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   180
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   181
    free(tmpValue);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   182
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   183
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   184
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   185
int flib_ini_get_str_opt(flib_ini *ini, char **outVar, const char *key, const char *def) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   186
    int result = INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   187
    if(!log_badargs_if4(ini==NULL, ini->currentSection==NULL, outVar==NULL, key==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   188
        const char *value = findValue(ini->inidict, ini->currentSection, key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   189
        if(!value) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   190
            value = def;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   191
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   192
        char *valueDup = flib_strdupnull(value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   193
        if(valueDup || !def) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   194
            *outVar = valueDup;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   195
            result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   196
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   197
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   198
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   199
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   200
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   201
int flib_ini_get_int(flib_ini *ini, int *outVar, const char *key) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   202
    char *tmpValue = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   203
    int result = flib_ini_get_str(ini, &tmpValue, key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   204
    if(result==0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   205
        errno = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   206
        long val = strtol(tmpValue, NULL, 10);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   207
        if(errno!=0 || val<INT_MIN || val>INT_MAX) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   208
            flib_log_w("Cannot parse ini setting %s/%s = \"%s\" as integer.", ini->currentSection, key, tmpValue);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   209
            result = INI_ERROR_FORMAT;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   210
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   211
            *outVar = val;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   212
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   213
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   214
    free(tmpValue);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   215
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   216
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   217
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   218
int flib_ini_get_int_opt(flib_ini *ini, int *outVar, const char *key, int def) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   219
    int tmpValue;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   220
    int result = flib_ini_get_int(ini, &tmpValue, key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   221
    if(result == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   222
        *outVar = tmpValue;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   223
    } else if(result == INI_ERROR_NOTFOUND || result == INI_ERROR_FORMAT) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   224
        *outVar = def;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   225
        result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   226
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   227
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   228
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   229
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   230
int flib_ini_get_bool(flib_ini *ini, bool *outVar, const char *key) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   231
    char *tmpValue = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   232
    int result = flib_ini_get_str(ini, &tmpValue, key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   233
    if(result==0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   234
        bool trueval = strchr("1tTyY", tmpValue[0]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   235
        bool falseval = strchr("0fFnN", tmpValue[0]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   236
        if(!trueval && !falseval) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   237
            flib_log_w("ini setting %s/%s = \"%s\" is not a recognized truth value.", ini->currentSection, key, tmpValue);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   238
            result = INI_ERROR_FORMAT;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   239
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   240
            *outVar = trueval;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   241
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   242
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   243
    free(tmpValue);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   244
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   245
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   246
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   247
int flib_ini_get_bool_opt(flib_ini *ini, bool *outVar, const char *key, bool def) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   248
    bool tmpValue;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   249
    int result = flib_ini_get_bool(ini, &tmpValue, key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   250
    if(result == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   251
        *outVar = tmpValue;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   252
    } else if(result == INI_ERROR_NOTFOUND || result == INI_ERROR_FORMAT) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   253
        *outVar = def;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   254
        result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   255
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   256
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   257
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   258
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   259
int flib_ini_set_str(flib_ini *ini, const char *key, const char *value) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   260
    int result = INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   261
    if(!log_badargs_if4(ini==NULL, ini->currentSection==NULL, key==NULL, value==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   262
        char *dictKey = createDictKey(ini->currentSection, key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   263
        if(dictKey) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   264
            result = iniparser_set(ini->inidict, dictKey, value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   265
            if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   266
                flib_log_e("Error setting ini entry %s to %s", dictKey, value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   267
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   268
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   269
        free(dictKey);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   270
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   271
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   272
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   273
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   274
int flib_ini_set_int(flib_ini *ini, const char *key, int value) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   275
    int result = INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   276
    char *strvalue = flib_asprintf("%i", value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   277
    if(strvalue) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   278
        result = flib_ini_set_str(ini, key, strvalue);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   279
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   280
    free(strvalue);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   281
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   282
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   283
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   284
int flib_ini_set_bool(flib_ini *ini, const char *key, bool value) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   285
    return flib_ini_set_str(ini, key, value ? "true" : "false");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   286
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   287
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   288
int flib_ini_get_sectioncount(flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   289
    if(!log_badargs_if(ini==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   290
        return iniparser_getnsec(ini->inidict);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   291
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   292
    return INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   293
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   294
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   295
char *flib_ini_get_sectionname(flib_ini *ini, int number) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   296
    if(!log_badargs_if2(ini==NULL, number<0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   297
        return flib_strdupnull(iniparser_getsecname(ini->inidict, number));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   298
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   299
    return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   300
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   301
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   302
int flib_ini_get_keycount(flib_ini *ini) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   303
    if(!log_badargs_if2(ini==NULL, ini->currentSection==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   304
        return iniparser_getsecnkeys(ini->inidict, ini->currentSection);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   305
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   306
    return INI_ERROR_OTHER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   307
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   308
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   309
char *flib_ini_get_keyname(flib_ini *ini, int number) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   310
    char *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   311
    if(!log_badargs_if3(ini==NULL, ini->currentSection==NULL, number<0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   312
        int keyCount = iniparser_getsecnkeys(ini->inidict, ini->currentSection);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   313
        char **keys = iniparser_getseckeys(ini->inidict, ini->currentSection);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   314
        if(keys && keyCount>number) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   315
            // The keys are in the format section:key, so we have to skip the section and colon.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   316
            result = flib_strdupnull(keys[number]+strlen(ini->currentSection)+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   317
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   318
        free(keys);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   319
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   320
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7467
diff changeset
   321
}