project_files/frontlib/util/inihelper.h
author nemo
Sat, 05 Feb 2022 10:50:25 -0500
changeset 15835 f0f615dcbe7c
parent 10017 de822cd3df3a
permissions -rw-r--r--
1.0.0 merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    20
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    21
 * Convenience interface for ini reading/writing.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    22
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    23
 * We currently use iniparser in the background, but using its interface directly is a bit verbose.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    24
 * This module is supposed to 1. make ini reading and writing a bit more convenient, and 2. hide
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    25
 * the iniparser dependency so it can at need be easily replaced.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    26
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    27
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    28
#ifndef INIHELPER_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    29
#define INIHELPER_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    31
#include <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    32
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    33
#define INI_ERROR_NOTFOUND -1
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    34
#define INI_ERROR_FORMAT -2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    35
#define INI_ERROR_OTHER -100
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    36
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    37
typedef struct _flib_ini flib_ini;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    38
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    39
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    40
 * Create a new ini data structure, pre-filled with the contents of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    41
 * the file "filename" if it exists. If filename is null, or the file
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    42
 * is not found, an empty ini will be created. However, if an error
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    43
 * occurs while reading the ini file (or any other error), null
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    44
 * is returned.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    45
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    46
 * This behavior is useful for modifying an existing ini file without
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    47
 * discarding unknown keys.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    48
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    49
flib_ini *flib_ini_create(const char *filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    51
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    52
 * Similar to flib_ini_create, but fails if the file is not found
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    53
 * or if filename is null.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    54
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    55
flib_ini *flib_ini_load(const char *filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    57
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    58
 * Store the ini to the file "filename", overwriting
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    59
 * the previous contents. Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    60
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    61
int flib_ini_save(flib_ini *ini, const char *filename);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    62
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    63
void flib_ini_destroy(flib_ini *ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    64
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    65
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    66
 * Enter the section with the specified name. Returns 0 on
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    67
 * success, INI_ERROR_NOTFOUND if the section does not exist
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    68
 * and a different value if another error occurs.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    69
 * If an error occurs, there is no current section.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    70
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    71
 * The section name should only consist of letters and
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    72
 * numbers.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    73
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    74
int flib_ini_enter_section(flib_ini *ini, const char *section);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    75
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    76
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    77
 * Creates and enters the section with the specified name. Simply
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    78
 * enters the section if it exists already. Returns 0 on success
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    79
 * and a different value if another error occurs.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    80
 * If an error occurs, there is no current section.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    81
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    82
int flib_ini_create_section(flib_ini *ini, const char *section);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    83
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    84
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    85
 * Find a key in the current section and store the value in outVar
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    86
 * as a newly allocated string. Returns 0 on success, INI_ERROR_NOTFOUND
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    87
 * if the key was not found and a different value for other errors,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    88
 * e.g. if there is no current section.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    89
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    90
int flib_ini_get_str(flib_ini *ini, char **outVar, const char *key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    91
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    92
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    93
 * Find a key in the current section and store the value in outVar
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    94
 * as a newly allocated string. If the key is not found, the default
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    95
 * value will be used instead. Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    96
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    97
int flib_ini_get_str_opt(flib_ini *ini, char **outVar, const char *key, const char *def);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    98
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    99
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   100
 * Find a key in the current section and store the value in outVar
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   101
 * as an int. Returns 0 on success, INI_ERROR_NOTFOUND
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   102
 * if the key was not found, INI_ERROR_FORMAT if it was found but
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   103
 * could not be converted to an int, and a different value for other
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   104
 * errors, e.g. if there is no current section.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   105
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   106
int flib_ini_get_int(flib_ini *ini, int *outVar, const char *key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   107
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   108
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   109
 * Find a key in the current section and store the value in outVar
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   110
 * as an int. If the key is not found, the default value will be used instead.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   111
 * Returns 0 on success, INI_ERROR_FORMAT if the value was found but
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   112
 * could not be converted to int, and another value otherwise.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   113
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   114
int flib_ini_get_int_opt(flib_ini *ini, int *outVar, const char *key, int def);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   115
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   116
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   117
 * Find a key in the current section and store the value in outVar
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   118
 * as a bool. Treats everything beginning with "Y", "T" or "1" as true,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   119
 * everything starting with "N", "F" or "1" as false.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   120
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   121
 * Returns 0 on success, INI_ERROR_NOTFOUND if the key was not found,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   122
 * INI_ERROR_FORMAT if the value could not be interpreted as boolean,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   123
 * and another value otherwise.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   124
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   125
int flib_ini_get_bool(flib_ini *ini, bool *outVar, const char *key);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   126
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   127
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   128
 * Find a key in the current section and store the value in outVar
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   129
 * as a bool. If the key is not found, the default value will be
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   130
 * used instead. Returns 0 on success, INI_ERROR_FORMAT if the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   131
 * value could not be interpreted as boolean, and another value otherwise.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   132
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   133
int flib_ini_get_bool_opt(flib_ini *ini, bool *outVar, const char *key, bool def);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   134
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   135
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   136
 * In the current section, associate key with value. Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   137
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   138
int flib_ini_set_str(flib_ini *ini, const char *key, const char *value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   139
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   140
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   141
 * In the current section, associate key with value. Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   142
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   143
int flib_ini_set_int(flib_ini *ini, const char *key, int value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   144
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   145
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   146
 * In the current section, associate key with value. Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   147
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   148
int flib_ini_set_bool(flib_ini *ini, const char *key, bool value);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   149
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   150
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   151
 * Returns the number of sections in the ini file, or a negative value on error.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   152
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   153
int flib_ini_get_sectioncount(flib_ini *ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   154
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   155
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   156
 * Returns the name of the section, or NULL on error. The returned string must
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   157
 * be free()d.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   158
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   159
 * Note: There is no guarantee that the order of the sections
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   160
 * will remain stable if the ini is modified.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   161
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   162
char *flib_ini_get_sectionname(flib_ini *ini, int number);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   163
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   164
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   165
 * Returns the number of keys in the current section, or -1 on error.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   166
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   167
int flib_ini_get_keycount(flib_ini *ini);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   168
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   169
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   170
 * Returns the name of the key in the current section, or NULL on error.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   171
 * The returned string must be free()d.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   172
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   173
 * Note: There is no guarantee that the order of the keys in a section
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   174
 * will remain stable if the ini is modified.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   175
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   176
char *flib_ini_get_keyname(flib_ini *ini, int number);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   177
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   178
#endif /* INIHELPER_H_ */