project_files/frontlib/util/inihelper.h
changeset 7227 1c859f572d72
parent 7224 5143861c83bd
child 7271 5608ac657362
equal deleted inserted replaced
7224:5143861c83bd 7227:1c859f572d72
     7  */
     7  */
     8 
     8 
     9 #ifndef INIHELPER_H_
     9 #ifndef INIHELPER_H_
    10 #define INIHELPER_H_
    10 #define INIHELPER_H_
    11 
    11 
    12 #include "../iniparser/iniparser.h"
       
    13 
       
    14 #include <stdbool.h>
    12 #include <stdbool.h>
    15 
    13 
       
    14 #define INI_ERROR_NOTFOUND -1
       
    15 #define INI_ERROR_FORMAT -2
       
    16 #define INI_ERROR_OTHER -100
       
    17 
    16 struct _flib_ini;
    18 struct _flib_ini;
    17 typedef struct _flib_inihelper flib_ini;
    19 typedef struct _flib_ini flib_ini;
    18 
    20 
    19 /**
    21 /**
    20  * Returned buffer must be free()d
    22  * Create a new ini data structure, pre-filled with the contents of
       
    23  * the file "filename" if it exists. If filename is null, or the file
       
    24  * is not found, an empty ini will be created. However, if an error
       
    25  * occurs while reading the ini file (or any other error), null
       
    26  * is returned.
       
    27  *
       
    28  * This behavior is useful for modifying an existing ini file without
       
    29  * discarding unknown keys.
    21  */
    30  */
    22 char *inihelper_urlencode(const char *inbuf);
    31 flib_ini *flib_ini_create(const char *filename);
    23 
    32 
    24 /**
    33 /**
    25  * Returned buffer must be free()d
    34  * Similar to flib_ini_create, but fails if the file is not found
       
    35  * or if filename is null.
    26  */
    36  */
    27 char *inihelper_urldecode(const char *inbuf);
    37 flib_ini *flib_ini_load(const char *filename);
    28 
    38 
    29 /**
    39 /**
    30  * Create a key in the format "sectionName:keyName"
    40  * Store the ini to the file "filename", overwriting
    31  * Returned buffer must be free()d
    41  * the previous contents. Returns 0 on success.
    32  */
    42  */
    33 char *inihelper_createDictKey(const char *sectionName, const char *keyName);
    43 int flib_ini_save(flib_ini *ini, const char *filename);
       
    44 
       
    45 void flib_ini_destroy(flib_ini *ini);
    34 
    46 
    35 /**
    47 /**
    36  * Returns an internal buffer, don't modify or free
    48  * Enter the section with the specified name. Returns 0 on
    37  * Sets error to true if something goes wrong, leaves it unchanged otherwise.
    49  * success, INI_ERROR_NOTFOUND if the section does not exist
       
    50  * and a different value if another error occurs.
       
    51  * If an error occurs, there is no current section.
       
    52  *
       
    53  * The section name should only consist of letters and
       
    54  * numbers.
    38  */
    55  */
    39 char *inihelper_getstring(dictionary *inifile, bool *error, const char *sectionName, const char *keyName);
    56 int flib_ini_enter_section(flib_ini *ini, const char *section);
    40 
    57 
    41 /**
    58 /**
    42  * Returned buffer must be free()d
    59  * Creates and enters the section with the specified name. Simply
    43  * Sets error to true if something goes wrong, leaves it unchanged otherwise.
    60  * enters the section if it exists already. Returns 0 on success
       
    61  * and a different value if another error occurs.
       
    62  * If an error occurs, there is no current section.
    44  */
    63  */
    45 char *inihelper_getstringdup(dictionary *inifile, bool *error, const char *sectionName, const char *keyName);
    64 int flib_ini_create_section(flib_ini *ini, const char *section);
    46 
    65 
    47 /**
    66 /**
    48  * Sets error to true if something goes wrong, leaves it unchanged otherwise.
    67  * Find a key in the current section and store the value in outVar
       
    68  * as a newly allocated string. Returns 0 on success, INI_ERROR_NOTFOUND
       
    69  * if the key was not found and a different value for other errors,
       
    70  * e.g. if there is no current section.
    49  */
    71  */
    50 int inihelper_getint(dictionary *inifile, bool *error, const char *sectionName, const char *keyName);
    72 int flib_ini_get_str(flib_ini *ini, char **outVar, const char *key);
    51 
    73 
    52 /**
    74 /**
    53  * Sets error to true if something goes wrong, leaves it unchanged otherwise.
    75  * Find a key in the current section and store the value in outVar
       
    76  * as a newly allocated string. If the key is not found, the default
       
    77  * value will be used instead. Returns 0 on success.
    54  */
    78  */
    55 bool inihelper_getbool(dictionary *inifile, bool *error, const char *sectionName, const char *keyName);
    79 int flib_ini_get_str_opt(flib_ini *ini, char **outVar, const char *key, const char *def);
    56 
    80 
    57 /**
    81 /**
    58  * Returns a nonzero value on error.
    82  * Find a key in the current section and store the value in outVar
       
    83  * as an int. Returns 0 on success, INI_ERROR_NOTFOUND
       
    84  * if the key was not found, INI_ERROR_FORMAT if it was found but
       
    85  * could not be converted to an int, and a different value for other
       
    86  * errors, e.g. if there is no current section.
    59  */
    87  */
    60 int inihelper_setstr(dictionary *dict, const char *sectionName, const char *keyName, const char *value);
    88 int flib_ini_get_int(flib_ini *ini, int *outVar, const char *key);
    61 
    89 
    62 /**
    90 /**
    63  * Returns a nonzero value on error.
    91  * Find a key in the current section and store the value in outVar
       
    92  * as an int. If the key is not found, the default value will be used instead.
       
    93  * Returns 0 on success, INI_ERROR_FORMAT if the value was found but
       
    94  * could not be converted to int, and another value otherwise.
    64  */
    95  */
    65 int inihelper_setint(dictionary *dict, const char *sectionName, const char *keyName, int value);
    96 int flib_ini_get_int_opt(flib_ini *ini, int *outVar, const char *key, int def);
    66 
    97 
    67 /**
    98 /**
    68  * Set an ini setting to "true" or "false".
    99  * Find a key in the current section and store the value in outVar
    69  * Returns a nonzero value on error.
   100  * as a bool. Treats everything beginning with "Y", "T" or "1" as true,
       
   101  * everything starting with "N", "F" or "1" as false.
       
   102  *
       
   103  * Returns 0 on success, INI_ERROR_NOTFOUND if the key was not found,
       
   104  * INI_ERROR_FORMAT if the value could not be interpreted as boolean,
       
   105  * and another value otherwise.
    70  */
   106  */
    71 int inihelper_setbool(dictionary *dict, const char *sectionName, const char *keyName, bool value);
   107 int flib_ini_get_bool(flib_ini *ini, bool *outVar, const char *key);
       
   108 
       
   109 /**
       
   110  * Find a key in the current section and store the value in outVar
       
   111  * as a bool. If the key is not found, the default value will be
       
   112  * used instead. Returns 0 on success, INI_ERROR_FORMAT if the
       
   113  * value could not be interpreted as boolean, and another value otherwise.
       
   114  */
       
   115 int flib_ini_get_bool_opt(flib_ini *ini, bool *outVar, const char *key, bool def);
       
   116 
       
   117 /**
       
   118  * In the current section, associate key with value. Returns 0 on success.
       
   119  */
       
   120 int flib_ini_set_str(flib_ini *ini, const char *key, const char *value);
       
   121 
       
   122 /**
       
   123  * In the current section, associate key with value. Returns 0 on success.
       
   124  */
       
   125 int flib_ini_set_int(flib_ini *ini, const char *key, int value);
       
   126 
       
   127 /**
       
   128  * In the current section, associate key with value. Returns 0 on success.
       
   129  */
       
   130 int flib_ini_set_bool(flib_ini *ini, const char *key, bool value);
       
   131 
       
   132 /**
       
   133  * Returns the number of sections in the ini file, or a negative value on error.
       
   134  */
       
   135 int flib_ini_get_sectioncount(flib_ini *ini);
       
   136 
       
   137 /**
       
   138  * Returns the name of the section, or NULL on error. The returned string must
       
   139  * be free()d.
       
   140  *
       
   141  * Note: There is no guarantee that the order of the sections
       
   142  * will remain stable if the ini is modified.
       
   143  */
       
   144 char *flib_ini_get_sectionname(flib_ini *ini, int number);
       
   145 
       
   146 /**
       
   147  * Returns the number of keys in the current section, or -1 on error.
       
   148  */
       
   149 int flib_ini_get_keycount(flib_ini *ini);
       
   150 
       
   151 /**
       
   152  * Returns the name of the key in the current section, or NULL on error.
       
   153  * The returned string must be free()d.
       
   154  *
       
   155  * Note: There is no guarantee that the order of the keys in a section
       
   156  * will remain stable if the ini is modified.
       
   157  */
       
   158 char *flib_ini_get_keyname(flib_ini *ini, int number);
    72 
   159 
    73 #endif /* INIHELPER_H_ */
   160 #endif /* INIHELPER_H_ */