libini_config  0.7.0
ini_configobj.h
1 /*
2  INI LIBRARY
3 
4  Header file for the ini configuration interface.
5  THIS IS THE PREFERRED INTERFACE TO USE.
6 
7  Copyright (C) Dmitri Pal <dpal@redhat.com> 2010 - 2012
8 
9  INI Library is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  INI Library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with INI Library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 
24 #ifndef INI_CONFIGOBJ_H
25 #define INI_CONFIGOBJ_H
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include "ref_array.h"
33 
34 struct value_obj;
35 
36 #ifndef DING_ATTR_FORMAT
37 # if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)))
38 # define DING_ATTR_FORMAT(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
39 # else
40 # define DING_ATTR_FORMAT(fmt, args)
41 # endif
42 #endif
43 
155 };
156 
171 enum ERR_LEVEL {
175 };
176 
189 enum ERR_PARSE {
210 };
211 
225 #define INI_META_NONE 0
226 
227 #define INI_META_STATS 1
228 
246 #define INI_ACCESS_CHECK_MODE 0x00000001
247 
254 #define INI_ACCESS_CHECK_UID 0x00000002
255 
262 #define INI_ACCESS_CHECK_GID 0x00000004
263 
286 #define INI_MV1S_OVERWRITE 0x0000
287 
288 #define INI_MV1S_ERROR 0x0001
289 
290 #define INI_MV1S_PRESERVE 0x0002
291 
292 #define INI_MV1S_ALLOW 0x0003
293 
294 #define INI_MV1S_DETECT 0x0004
295 
313 #define INI_MV2S_OVERWRITE 0x0000
314 
315 #define INI_MV2S_ERROR 0x0010
316 
317 #define INI_MV2S_PRESERVE 0x0020
318 
319 #define INI_MV2S_ALLOW 0x0030
320 
321 #define INI_MV2S_DETECT 0x0040
322 
339 #define INI_MS_MERGE 0x0000
340 
341 #define INI_MS_ERROR 0x0100
342 
343 #define INI_MS_OVERWRITE 0x0200
344 
345 #define INI_MS_PRESERVE 0x0300
346 
348 #define INI_MS_DETECT 0x0400
349 
367 #define INI_PARSE_NOWRAP 0x0001
368 
369 #define INI_PARSE_NOSPACE 0x0002
370 
371 #define INI_PARSE_NOTAB 0x0004
372 
373 #define INI_PARSE_NO_C_COMMENTS 0x0008
374 
375 #define INI_PARSE_IGNORE_NON_KVP 0x0010
376 
400 enum INI_GET {
404 };
405 
420 struct access_check {
421  uint32_t flags;
427  uid_t uid;
428  gid_t gid;
429  mode_t mode;
430  mode_t mask;
435 };
436 
438 enum augmode {
442 };
443 
454 #define INI_DEFAULT_SECTION "default"
455 
462 struct ini_cfgobj;
463 struct ini_cfgfile;
464 
468 struct ini_parse_error;
469 
470 
502 int ini_config_create(struct ini_cfgobj **ini_config);
503 
512 void ini_config_destroy(struct ini_cfgobj *ini_config);
513 
523 void ini_config_clean_state(struct ini_cfgobj *ini_config);
524 
550 int ini_config_file_open(const char *filename,
551  uint32_t metadata_flags,
552  struct ini_cfgfile **file_ctx);
553 
575 int ini_config_file_from_mem(void *data_buf,
576  uint32_t data_len,
577  struct ini_cfgfile **file_ctx);
578 
588 void ini_config_file_close(struct ini_cfgfile *file_ctx);
589 
590 
605 int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in,
606  struct ini_cfgfile **file_ctx_out);
607 
608 
617 void ini_config_file_destroy(struct ini_cfgfile *file_ctx);
618 
668 int ini_config_file_backup(struct ini_cfgfile *file_ctx,
669  const char *backup_dir,
670  const char *backup_tpl,
671  struct access_check *backup_access,
672  unsigned max_num);
673 
693 int ini_config_change_access(struct ini_cfgfile *file_ctx,
694  struct access_check *new_access);
695 
704 int ini_config_save(struct ini_cfgfile *file_ctx,
705  struct access_check *new_access,
706  struct ini_cfgobj *ini_config);
707 
708 
709 /* Save configuration in a file using existing context but with a new name */
741 int ini_config_save_as(struct ini_cfgfile *file_ctx,
742  const char *filename,
743  struct access_check *new_access,
744  struct ini_cfgobj *ini_config);
745 
746 
747 
759 enum index_utf_t ini_config_get_bom(struct ini_cfgfile *file_ctx);
760 
776 int ini_config_set_bom(struct ini_cfgfile *file_ctx, enum index_utf_t bom);
777 
778 
790 unsigned ini_config_error_count(struct ini_cfgobj *ini_config);
791 
808 int ini_config_get_errors(struct ini_cfgobj *ini_config,
809  char ***errors);
810 
820 void ini_config_free_errors(char **errors);
821 
833 void ini_config_print_errors(FILE *file, char **error_list);
834 
845 const char *ini_config_get_filename(struct ini_cfgfile *file_ctx);
846 
858 const struct stat *ini_config_get_stat(struct ini_cfgfile *file_ctx);
859 
860 
861 
870 void ini_config_file_print(struct ini_cfgfile *file_ctx);
871 
897 int ini_config_access_check(struct ini_cfgfile *file_ctx,
898  uint32_t flags,
899  uid_t uid,
900  gid_t gid,
901  mode_t mode,
902  mode_t mask);
903 
935 int ini_config_changed(struct ini_cfgfile *file_ctx1,
936  struct ini_cfgfile *file_ctx2,
937  int *changed);
938 
964 int ini_config_parse(struct ini_cfgfile *file_ctx,
965  int error_level,
966  uint32_t collision_flags,
967  uint32_t parse_flags,
968  struct ini_cfgobj *ini_config);
969 
983 int ini_config_copy(struct ini_cfgobj *ini_config,
984  struct ini_cfgobj **ini_new);
985 
1012 int ini_config_merge(struct ini_cfgobj *first,
1013  struct ini_cfgobj *second,
1014  uint32_t collision_flags,
1015  struct ini_cfgobj **result);
1016 
1017 
1085 int ini_config_augment(struct ini_cfgobj *base_cfg,
1086  const char *path,
1087  const char *patterns[],
1088  const char *sections[],
1089  struct access_check *check_perm,
1090  int error_level,
1091  uint32_t collision_flags,
1092  uint32_t parse_flags,
1093  uint32_t merge_flags,
1094  struct ini_cfgobj **result_cfg,
1095  struct ref_array **error_list,
1096  struct ref_array **success_list);
1097 
1111 int ini_config_set_wrap(struct ini_cfgobj *ini_config,
1112  uint32_t boundary);
1113 
1114 /* Functions that add, modify or delete sections and values in
1115  * the configuration object can be found in section \ref ini_mod.
1116  */
1117 
1154 char **ini_get_section_list(struct ini_cfgobj *ini_config,
1155  int *size,
1156  int *error);
1157 
1167 void ini_free_section_list(char **section_list);
1168 
1189 char **ini_get_attribute_list(struct ini_cfgobj *ini_config,
1190  const char *section,
1191  int *size,
1192  int *error);
1193 
1203 void ini_free_attribute_list(char **attr_list);
1204 
1246 int ini_get_config_valueobj(const char *section,
1247  const char *name,
1248  struct ini_cfgobj *ini_config,
1249  int mode,
1250  struct value_obj **vo);
1251 
1252 
1253 
1294 int ini_get_int_config_value(struct value_obj *vo,
1295  int strict,
1296  int def,
1297  int *error);
1298 
1340 unsigned ini_get_unsigned_config_value(struct value_obj *vo,
1341  int strict,
1342  unsigned def,
1343  int *error);
1344 
1386 long ini_get_long_config_value(struct value_obj *vo,
1387  int strict,
1388  long def,
1389  int *error);
1390 
1432 unsigned long ini_get_ulong_config_value(struct value_obj *vo,
1433  int strict,
1434  unsigned long def,
1435  int *error);
1436 
1437 
1478 int32_t ini_get_int32_config_value(struct value_obj *vo,
1479  int strict,
1480  int32_t def,
1481  int *error);
1482 
1523 uint32_t ini_get_uint32_config_value(struct value_obj *vo,
1524  int strict,
1525  uint32_t def,
1526  int *error);
1527 
1568 int64_t ini_get_int64_config_value(struct value_obj *vo,
1569  int strict,
1570  int64_t def,
1571  int *error);
1572 
1613 uint64_t ini_get_uint64_config_value(struct value_obj *vo,
1614  int strict,
1615  uint64_t def,
1616  int *error);
1617 
1656 double ini_get_double_config_value(struct value_obj *vo,
1657  int strict,
1658  double def,
1659  int *error);
1660 
1694 unsigned char ini_get_bool_config_value(struct value_obj *vo,
1695  unsigned char def,
1696  int *error);
1697 
1722 char *ini_get_string_config_value(struct value_obj *vo,
1723  int *error);
1746 const char *ini_get_const_string_config_value(struct value_obj *vo,
1747  int *error);
1748 
1798 char *ini_get_bin_config_value(struct value_obj *vo,
1799  int *length,
1800  int *error);
1801 
1810 void ini_free_bin_config_value(char *bin);
1811 
1866 char **ini_get_string_config_array(struct value_obj *vo,
1867  const char *sep,
1868  int *size,
1869  int *error);
1870 
1925 char **ini_get_raw_string_config_array(struct value_obj *vo,
1926  const char *sep,
1927  int *size,
1928  int *error);
1929 
1972 long *ini_get_long_config_array(struct value_obj *vo,
1973  int *size,
1974  int *error);
1975 
2017 double *ini_get_double_config_array(struct value_obj *vo,
2018  int *size,
2019  int *error);
2020 
2030 void ini_free_string_config_array(char **str_config);
2031 
2040 void ini_free_long_config_array(long *array);
2049 void ini_free_double_config_array(double *array);
2050 
2054 struct ini_errobj;
2055 
2067 int ini_errobj_create(struct ini_errobj **_errobj);
2068 
2077 void ini_errobj_destroy(struct ini_errobj **errobj);
2078 
2092 int ini_errobj_add_msg(struct ini_errobj *errobj,
2093  const char *format, ...)
2094  DING_ATTR_FORMAT(2, 3);
2095 
2106 void ini_errobj_reset(struct ini_errobj *errobj);
2107 
2121 const char *ini_errobj_get_msg(struct ini_errobj *errobj);
2122 
2132 void ini_errobj_next(struct ini_errobj *errobj);
2133 
2144 int ini_errobj_no_more_msgs(struct ini_errobj *errobj);
2145 
2155 size_t ini_errobj_count(struct ini_errobj *errobj);
2156 
2157 typedef int (ini_validator_func)(const char *rule_name,
2158  struct ini_cfgobj *rules_obj,
2159  struct ini_cfgobj *config_obj,
2160  struct ini_errobj *errobj,
2161  void **data);
2162 
2163 typedef int (ini_schema_validator_func)(const char *rule_name,
2164  struct ini_cfgobj *rules_obj,
2165  struct ini_errobj *errobj,
2166  void **data);
2167 
2172  const char *name;
2173  ini_validator_func *func;
2174  /* currently unused, for future expansion */
2175  ini_schema_validator_func *schema_validator;
2176 };
2177 
2191 int ini_rules_read_from_file(const char *filename,
2192  struct ini_cfgobj **_rules_obj);
2193 
2218 int ini_rules_check(struct ini_cfgobj *rules_obj,
2219  struct ini_cfgobj *config_obj,
2220  struct ini_validator **extra_validators,
2221  struct ini_errobj *errobj);
2222 
2228 void ini_rules_destroy(struct ini_cfgobj *ini_config);
2229 
2234 #endif
Definition: ini_configobj.h:190
Definition: ini_configobj.h:206
const char * ini_errobj_get_msg(struct ini_errobj *errobj)
Get pointer to current message in errobj.
int ini_get_config_valueobj(const char *section, const char *name, struct ini_cfgobj *ini_config, int mode, struct value_obj **vo)
Retrieve a value object form the configuration.
void ini_free_long_config_array(long *array)
Free array of long values.
void ini_config_file_print(struct ini_cfgfile *file_ctx)
Print file context.
Definition: ini_configobj.h:199
void ini_free_attribute_list(char **attr_list)
Free list of attributes.
mode_t mode
Definition: ini_configobj.h:429
int void ini_errobj_reset(struct ini_errobj *errobj)
Reset iterator in errobj.
ERR_LEVEL
Definition: ini_configobj.h:171
void ini_config_free_errors(char **errors)
Free array of parsing errors.
char ** ini_get_string_config_array(struct value_obj *vo, const char *sep, int *size, int *error)
Convert value to an array of strings.
Definition: ini_configobj.h:201
Definition: ini_configobj.h:174
Definition: ini_configobj.h:154
INI_GET
Definition: ini_configobj.h:400
int ini_config_save_as(struct ini_cfgfile *file_ctx, const char *filename, struct access_check *new_access, struct ini_cfgobj *ini_config)
Save configuration with a new name.
Definition: ini_configobj.h:197
const char * ini_config_get_filename(struct ini_cfgfile *file_ctx)
Get the fully resolved file name.
void ini_rules_destroy(struct ini_cfgobj *ini_config)
Free the rules.
Definition: ini_configobj.h:440
Definition: ini_configobj.h:402
int ini_rules_check(struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, struct ini_validator **extra_validators, struct ini_errobj *errobj)
Check configuration file using rules.
void ini_free_section_list(char **section_list)
Free list of sections.
uint32_t ini_get_uint32_config_value(struct value_obj *vo, int strict, uint32_t def, int *error)
Convert value to uint32_t number.
int ini_config_file_backup(struct ini_cfgfile *file_ctx, const char *backup_dir, const char *backup_tpl, struct access_check *backup_access, unsigned max_num)
Save configuration in a backup configuration file.
unsigned ini_config_error_count(struct ini_cfgobj *ini_config)
Check parsing errors count.
void ini_config_destroy(struct ini_cfgobj *ini_config)
Destroy a configuration object.
Definition: ini_configobj.h:205
int ini_config_changed(struct ini_cfgfile *file_ctx1, struct ini_cfgfile *file_ctx2, int *changed)
Check if file has changed.
Definition: ini_configobj.h:403
Definition: ini_configobj.h:191
long * ini_get_long_config_array(struct value_obj *vo, int *size, int *error)
Convert value to an array of long values.
int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in, struct ini_cfgfile **file_ctx_out)
Reopen the configuration file.
long ini_get_long_config_value(struct value_obj *vo, int strict, long def, int *error)
Convert value to long number.
Definition: ini_configobj.h:194
unsigned ini_get_unsigned_config_value(struct value_obj *vo, int strict, unsigned def, int *error)
Convert value object to a unsigned number.
char ** ini_get_raw_string_config_array(struct value_obj *vo, const char *sep, int *size, int *error)
Convert value to an array of strings.
void ini_free_bin_config_value(char *bin)
Free binary buffer.
Definition: ini_configobj.h:420
int ini_config_get_errors(struct ini_cfgobj *ini_config, char ***errors)
Get array of parsing errors.
uid_t uid
Definition: ini_configobj.h:427
void ini_config_clean_state(struct ini_cfgobj *ini_config)
Flush cached search data.
Definition: ini_configobj.h:401
char * ini_get_string_config_value(struct value_obj *vo, int *error)
Get the copy of string stored in the configuration value.
Definition: ini_configobj.h:172
int ini_errobj_create(struct ini_errobj **_errobj)
Create structure to hold error messages.
uint64_t ini_get_uint64_config_value(struct value_obj *vo, int strict, uint64_t def, int *error)
Convert value to integer number.
int ini_config_merge(struct ini_cfgobj *first, struct ini_cfgobj *second, uint32_t collision_flags, struct ini_cfgobj **result)
Merge two configuration objects.
void ini_errobj_destroy(struct ini_errobj **errobj)
Free structure that holds error messages.
gid_t gid
Definition: ini_configobj.h:428
Definition: ini_configobj.h:198
int ini_config_set_wrap(struct ini_cfgobj *ini_config, uint32_t boundary)
Set the folding boundary.
Definition: ini_configobj.h:441
size_t ini_errobj_count(struct ini_errobj *errobj)
Return number of messages in errobj.
Definition: ini_configobj.h:151
Definition: ini_configobj.h:202
double * ini_get_double_config_array(struct value_obj *vo, int *size, int *error)
Convert value to an array of floating point values.
Definition: ini_configobj.h:196
Definition: ini_configobj.h:149
Definition: ini_configobj.h:195
enum index_utf_t ini_config_get_bom(struct ini_cfgfile *file_ctx)
Return the encoding indicator.
index_utf_t
Definition: ini_configobj.h:148
int ini_get_int_config_value(struct value_obj *vo, int strict, int def, int *error)
Convert value to integer number.
Definition: ini_configobj.h:208
void ini_config_file_close(struct ini_cfgfile *file_ctx)
Close configuration file after parsing.
Definition: ini_configobj.h:153
Definition: ini_configobj.h:150
void ini_config_print_errors(FILE *file, char **error_list)
Print errors to a file.
int ini_config_change_access(struct ini_cfgfile *file_ctx, struct access_check *new_access)
Change permissions and ownership of the file.
int ini_errobj_add_msg(struct ini_errobj *errobj, const char *format,...) DING_ATTR_FORMAT(2
Add new printf formated message to errobj.
int ini_config_set_bom(struct ini_cfgfile *file_ctx, enum index_utf_t bom)
Set the encoding indicator.
unsigned char ini_get_bool_config_value(struct value_obj *vo, unsigned char def, int *error)
Convert value into a logical value.
char ** ini_get_section_list(struct ini_cfgobj *ini_config, int *size, int *error)
Get list of sections.
const char * ini_get_const_string_config_value(struct value_obj *vo, int *error)
Get the string stored in the configuration value.
ERR_PARSE
Definition: ini_configobj.h:189
Definition: ini_configobj.h:152
void ini_free_double_config_array(double *array)
Free array of floating pointer values.
int ini_config_copy(struct ini_cfgobj *ini_config, struct ini_cfgobj **ini_new)
Create a copy of the configuration object.
Definition: ini_configobj.h:204
int ini_config_access_check(struct ini_cfgfile *file_ctx, uint32_t flags, uid_t uid, gid_t gid, mode_t mode, mode_t mask)
Check file properties.
void ini_config_file_destroy(struct ini_cfgfile *file_ctx)
Close configuration file and free all data.
Definition: ini_configobj.h:209
char ** ini_get_attribute_list(struct ini_cfgobj *ini_config, const char *section, int *size, int *error)
Get list of attributes.
int ini_rules_read_from_file(const char *filename, struct ini_cfgobj **_rules_obj)
Read rules from INI file.
Definition: ini_configobj.h:193
int ini_errobj_no_more_msgs(struct ini_errobj *errobj)
Check if errobj has more messages.
int32_t ini_get_int32_config_value(struct value_obj *vo, int strict, int32_t def, int *error)
Convert value to int32_t number.
int ini_config_save(struct ini_cfgfile *file_ctx, struct access_check *new_access, struct ini_cfgobj *ini_config)
Save configuration in a file.
Definition: ini_configobj.h:439
void ini_errobj_next(struct ini_errobj *errobj)
Move to the next message in errobj.
int ini_config_augment(struct ini_cfgobj *base_cfg, const char *path, const char *patterns[], const char *sections[], struct access_check *check_perm, int error_level, uint32_t collision_flags, uint32_t parse_flags, uint32_t merge_flags, struct ini_cfgobj **result_cfg, struct ref_array **error_list, struct ref_array **success_list)
Augment configuration.
char * ini_get_bin_config_value(struct value_obj *vo, int *length, int *error)
Convert value into a binary sequence.
int ini_config_create(struct ini_cfgobj **ini_config)
Create a configuration object.
int ini_config_file_open(const char *filename, uint32_t metadata_flags, struct ini_cfgfile **file_ctx)
Create a configuration file object.
int64_t ini_get_int64_config_value(struct value_obj *vo, int strict, int64_t def, int *error)
Convert value to integer number.
Definition: ini_configobj.h:173
mode_t mask
Definition: ini_configobj.h:430
double ini_get_double_config_value(struct value_obj *vo, int strict, double def, int *error)
Convert value to floating point number.
Structure used to define application specific (external to libini) validator.
Definition: ini_configobj.h:2171
int ini_config_parse(struct ini_cfgfile *file_ctx, int error_level, uint32_t collision_flags, uint32_t parse_flags, struct ini_cfgobj *ini_config)
Parse the file and populate a configuration object.
uint32_t flags
Definition: ini_configobj.h:421
int ini_config_file_from_mem(void *data_buf, uint32_t data_len, struct ini_cfgfile **file_ctx)
Create a configuration file object using memory buffer.
unsigned long ini_get_ulong_config_value(struct value_obj *vo, int strict, unsigned long def, int *error)
Convert value to unsigned long number.
const struct stat * ini_config_get_stat(struct ini_cfgfile *file_ctx)
Get pointer to collected stat data.
void ini_free_string_config_array(char **str_config)
Free array of string values.
augmode
Definition: ini_configobj.h:438