Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

cfg+.h

Go to the documentation of this file.
00001 /*
00002  * libcfg+ - precise command line & config file parsing library
00003  *
00004  * cfg+.h - main implementation header file
00005  * ____________________________________________________________
00006  *
00007  * Developed by Ondrej Jombik <nepto@platon.sk>
00008  *          and Lubomir Host <rajo@platon.sk>
00009  * Copyright (c) 2001-2004 Platon SDG, http://platon.sk/
00010  * All rights reserved.
00011  *
00012  * See README file for more information about this software.
00013  * See COPYING file for license information.
00014  *
00015  * Download the latest version from
00016  * http://platon.sk/projects/libcfg+/
00017  */
00018 
00019 /* $Platon: libcfg+/src/cfg+.h,v 1.60 2004/01/12 06:03:08 nepto Exp $ */
00020 
00030 #ifndef _PLATON_CFG_H
00031 #define _PLATON_CFG_H
00032 
00033 #include <stdio.h>
00034 
00036 #define CFG_END_OPTION { NULL, '\0', NULL, CFG_END, NULL, 0 }
00037 #define CFG_END_OF_LIST CFG_END_OPTION 
00046 enum cfg_error { /* {{{ */
00047 
00050         CFG_ERR_OK = 0,
00051         CFG_ERROR_OK = 0,
00052         CFG_OK = 0,
00056         CFG_ERR_NOARG = -1,
00057         CFG_ERROR_NOARG = -1,
00058 
00060         CFG_ERR_NOTALLOWEDARG = -2,
00061         CFG_ERROR_NOTALLOWEDARG = -2,
00062 
00064         CFG_ERR_BADOPT = -3,
00065         CFG_ERROR_BADOPT = -3,
00066 
00068         CFG_ERR_BADQUOTE = -4,
00069         CFG_ERROR_BADQUOTE = -4,
00070 
00072         CFG_ERR_BADNUMBER = -5,
00073         CFG_ERROR_BADNUMBER = -5,
00074 
00076         CFG_ERR_OVERFLOW = -6,
00077         CFG_ERROR_OVERFLOW = -6,
00078 
00080         CFG_ERR_MULTI = -7,
00081         CFG_ERROR_MULTI = -7,
00082 
00084         CFG_ERR_NOMEM = -8,
00085         CFG_ERROR_NOMEM = -8,
00086 
00088         CFG_ERR_STOP_STR = -9,
00089         CFG_ERR_STOP_STR_FOUND = -9,
00090         CFG_ERROR_STOP_STR = -9,
00091         CFG_ERROR_STOP_STR_FOUND = -9,
00092 
00094         CFG_ERR_NOEQUAL = -10,
00095         CFG_ERROR_NOEQUAL = -10,
00096 
00098         CFG_ERR_UNKNOWN = -11,
00099         CFG_ERROR_UNKNOWN = -11,
00100 
00102         CFG_ERR_FILE_NOT_FOUND = -12,
00103         CFG_ERROR_FILE_NOT_FOUND = -12,
00104 
00106         CFG_ERR_SEEK_ERROR = -13,
00107         CFG_ERROR_SEEK_ERROR = -13,
00108 
00110         CFG_ERR_INTERNAL = -20,
00111         CFG_ERROR_INTERNAL = -20
00112 
00113 }; /* }}} */ /*@}*/
00114 
00124 enum cfg_flag { /* {{{ */
00125 
00127         CFG_IGNORE_MULTI = 1,
00128 
00130         CFG_IGNORE_UNKNOWN = 2,
00131 
00133         CFG_PROCESS_FIRST = 0,
00134 
00136         CFG_SKIP_FIRST = 4,
00137 
00139         CFG_POSIXLY_LEFTOVERS = 0,
00140 
00142         CFG_ADVANCED_LEFTOVERS = 8,
00143 
00146         CFG_NORMAL_LEFTOVERS = 0,
00147 
00150         CFG_STRICT_LEFTOVERS = 16,
00151 
00153         CFG_FILE_BYTE_POS_USAGE = 0,
00154 
00156         CFG_FILE_LINE_POS_USAGE = 32
00157 
00158                 /* Ignore all quotations in options arguments. */
00159                 /*
00160                    CFG_USE_QUOTE = 0,
00161                    CFG_IGNORE_QUOTE = 16
00162                    */
00163                 /* Advanced quotations are things like  option = va"'l'"ue
00164                    which resolves to                                    va'l'ue.
00165 
00166                    We really want this strange stuff? Any volunter?
00167 
00168                    CFG_ADVANCED_QUOTE = 32 + 16
00169                    */
00170 
00171 }; /* }}} */ /*@}*/
00172 
00182 enum cfg_option_type { /* {{{ */
00183 
00185         CFG_BOOL = 1,
00186         CFG_BOOLEAN = 1,
00187 
00189         CFG_INT = 2,
00190         CFG_INTEGER = 2,
00191 
00193         CFG_UINT = 3,
00194         CFG_UNSIGNED = 3,
00195         CFG_UNSIGNED_INT = 3,
00196 
00198         CFG_LONG = 4,
00199 
00201         CFG_ULONG = 5,
00202         CFG_UNSIGNED_LONG = 5,
00203 
00205         CFG_FLOAT = 6,
00206 
00208         CFG_DOUBLE = 7,
00209 
00211         CFG_STR = 8,
00212         CFG_STRING = 8,
00213 
00215         CFG_END = 0,
00216 
00218         CFG_DATA_TYPE_MASK = 31,
00219 
00225         CFG_SINGLE = 0,
00226         CFG_MULTI = 32,
00227         CFG_MULTI_ARRAY = 32,
00228         CFG_MULTI_SEPARATED = 32 + 64,
00229 
00234         CFG_LAST_ARGS = 128,
00235         CFG_LAST_ARGUMENTS = 128,
00236         CFG_LEFTOVER_ARGS = 128,
00237         CFG_LEFTOVER_ARGUMENTS = 128
00238 
00239 }; /* }}} */ /*@}*/
00240 
00245 enum cfg_property_type { /* {{{ */
00246 
00251 
00254         CFG_LINE_STOP_STRING = 0,
00255 
00258         CFG_LINE_SHORT_OPTION_PREFIX = 1,
00259 
00262         CFG_LINE_LONG_OPTION_PREFIX = 2,
00263 
00266         CFG_LINE_OPTION_ARG_SEPARATOR = 3,
00267 
00270         CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR = 4,
00271 
00274         CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR = 5,
00275 
00278         CFG_LINE_QUOTE_PREFIX  = 6,
00279         CFG_LINE_QUOTE_POSTFIX = 7,
00280 
00283         CFG_FILE_STOP_PREFIX = 8,
00284 
00287         CFG_FILE_COMMENT_PREFIX = 9,
00288 
00291         CFG_FILE_MULTI_LINE_POSTFIX = 10,
00292 
00295         CFG_FILE_OPTION_ARG_SEPARATOR = 11,
00296 
00299         CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR = 12,
00300 
00303         CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR = 13,
00304 
00307         CFG_FILE_QUOTE_PREFIX  = 14,
00308         CFG_FILE_QUOTE_POSTFIX = 15,
00309 
00317         CFG_N_PROPS = 16,
00324 
00326         CFG_QUOTE         = 50,
00327         CFG_LINE_QUOTE    = 51,
00328         CFG_FILE_QUOTE    = 52,
00329         CFG_QUOTE_PREFIX  = 53,
00330         CFG_QUOTE_POSTFIX = 54,
00331 
00333         CFG_MULTI_VALS_SEPARATOR          = 55,
00334         CFG_FILE_MULTI_VALS_SEPARATOR     = 56,
00335         CFG_LINE_MULTI_VALS_SEPARATOR     = 57,
00336         CFG_NORMAL_MULTI_VALS_SEPARATOR   = 58,
00337         CFG_LEFTOVER_MULTI_VALS_SEPARATOR = 59,
00338 
00340         CFG_OPTION_ARG_SEPARATOR = 60
00342 }; /* }}} */
00343 
00348 #define CFG_EOT                 CFG_N_PROPS
00349 #define CFG_END_TYPE    CFG_N_PROPS
00350 
00362 enum cfg_context_type { /* {{{ */
00363 
00365         CFG_NO_CONTEXT = 0,
00366 
00368         CFG_CMDLINE = 1,
00369         CFG_LINE = 1,
00370 
00372         CFG_CFGFILE = 2,
00373         CFG_FILE = 2
00374 }; /* }}} */
00375 
00381 enum cfg_line_option_type { /* {{{ */
00382 
00384         CFG_NONE_OPTION = 0,
00385 
00387         CFG_SHORT_OPTION = 1,
00388 
00390         CFG_LONG_OPTION = 2,
00391 
00393         CFG_SHORT_OPTIONS = 4,
00394 
00396         CFG_LONG_SEPINIT = 8,
00397 
00399         CFG_LONG_NOSEPINIT = 0
00400 }; /* }}} */ /*@}*/
00401 
00405 struct cfg_option { /* {{{ */
00407         const char *cmdline_long_name;
00409         const char  cmdline_short_name;
00411         const char *cfgfile_name;
00412 
00415         const enum cfg_option_type type;
00416 
00418         void *value;
00419 
00421         int val;
00422 }; /* }}} */
00423 
00427 struct cfg_context { /* {{{ */
00428 
00433 
00435         enum cfg_context_type type;
00436 
00438         int flags;
00439 
00441         const struct cfg_option *options;
00442 
00444         long begin_pos;
00445 
00448         long size;
00449 
00451         int *used_opt_idx;
00452 
00454         enum cfg_error error_code;
00455 
00457         char **prop[CFG_N_PROPS];
00458 
00460         long cur_idx;
00461         long cur_idx_tmp;
00462         int  cur_opt_type;
00463 
00465         char *cur_opt;
00466 
00468         char *cur_arg;
00469 
00476 
00478         int parsing_started:1;
00479 
00481         char **argv;
00482 
00489 
00491         char *filename;
00492 
00494         FILE *fhandle;
00495 
00497 }; /* }}} */
00498 
00502 typedef struct cfg_context * CFG_CONTEXT;
00503 
00504 /*
00505  * Functions
00506  */
00507 
00508 #ifdef __cplusplus
00509 extern "C" {
00510 #endif
00511 
00515  /* {{{ */
00516 
00524         CFG_CONTEXT cfg_get_context(struct cfg_option *options);
00525 
00536         CFG_CONTEXT cfg_get_cmdline_context(
00537                         long begin_pos,
00538                         long size,
00539                         char **argv,
00540                         struct cfg_option *options);
00541 
00542 #define cfg_get_cmdline_context_pos(begin_pos, end_pos, argv, options) \
00543         cfg_get_cmdline_context( \
00544                         begin_pos, \
00545                         end_pos - begin_pos + 1, \
00546                         argv, \
00547                         options)
00548 
00557                 CFG_CONTEXT cfg_get_cmdline_context_argc(
00558                                 int argc,
00559                                 char **argv,
00560                                 struct cfg_option *options);
00561 
00572         CFG_CONTEXT cfg_get_cfgfile_context(
00573                         long begin_pos,
00574                         long size,
00575                         char *filename,
00576                         struct cfg_option *options);
00577 
00578 #define cfg_get_cfgfile_context_pos(begin_pos, end_pos, argv, options) \
00579         cfg_get_cfgfile_context( \
00580                         begin_pos, \
00581                         end_pos - begin_pos + 1, \
00582                         argv, \
00583                         options)
00584 
00595                 void cfg_set_cmdline_context(
00596                                 const CFG_CONTEXT con,
00597                                 long begin_pos,
00598                                 long size,
00599                                 char **argv);
00600 
00601 #define cfg_set_cmdline_context_pos(con, begin_pos, end_pos, argv) \
00602         cfg_get_cmdline_context( \
00603                         con \
00604                         begin_pos, \
00605                         end_pos - begin_pos + 1, \
00606                         argv)
00607 
00616                 void cfg_set_cmdline_context_argc(
00617                                 const CFG_CONTEXT con,
00618                                 int argc,
00619                                 char **argv);
00620 
00631         void cfg_set_cfgfile_context(
00632                         const CFG_CONTEXT con,
00633                         long begin_pos,
00634                         long size,
00635                         char *filename);
00636 
00637 #define cfg_set_cfgfile_context_pos(con, begin_pos, end_pos, argv) \
00638         cfg_get_cfgfile_context( \
00639                         con \
00640                         begin_pos, \
00641                         end_pos - begin_pos + 1, \
00642                         argv)
00643 
00650                 void cfg_reset_context(const CFG_CONTEXT con);
00651 
00658         void cfg_free_context(const CFG_CONTEXT con);
00659 
00660         /* }}} */ /*@}*/
00661 
00665  /* {{{ */
00666 
00674         void cfg_set_context_flag(const CFG_CONTEXT con, int flag);
00675 
00683         void cfg_clear_context_flag(const CFG_CONTEXT con, int flag);
00684 
00692         int cfg_get_context_flag(const CFG_CONTEXT con, int flag);
00693 
00694 #define cfg_is_context_flag(con, flag) cfg_get_context_flag(con, flag)
00695 
00703         void cfg_set_context_flags(const CFG_CONTEXT con, int flags);
00704 
00711         int cfg_get_context_flags(const CFG_CONTEXT con);
00712 
00713         /* }}} */ /*@}*/
00714 
00718  /* {{{ */
00719 
00728         int cfg_clear_property(
00729                         const CFG_CONTEXT con, enum cfg_property_type type);
00730 
00739         int cfg_clear_properties(
00740                         const CFG_CONTEXT con, enum cfg_property_type type, ...);
00741 
00742 
00752         int cfg_add_property(
00753                         const CFG_CONTEXT con, enum cfg_property_type type, char *str);
00754 
00768         int cfg_add_properties(
00769                         const CFG_CONTEXT con, enum cfg_property_type type, char *str, ...);
00770 
00784         int cfg_add_properties_str(
00785                         const CFG_CONTEXT con, char *str, enum cfg_property_type type, ...);
00786 
00800         int cfg_add_properties_type(
00801                         const CFG_CONTEXT con, enum cfg_property_type type, char *str, ...);
00802 
00812         int cfg_remove_property(
00813                         const CFG_CONTEXT con, enum cfg_property_type type, char *str);
00814 
00828         int cfg_remove_properties(
00829                         const CFG_CONTEXT con, enum cfg_property_type type, char *str, ...);
00830 
00844         int cfg_remove_properties_str(
00845                         const CFG_CONTEXT con, char *str, enum cfg_property_type type, ...);
00846 
00860         int cfg_remove_properties_type(
00861                         const CFG_CONTEXT con, enum cfg_property_type type, char *str, ...);
00862 
00863         /* }}} */ /*@}*/
00864 
00868  /* {{{ */
00869 
00878         int cfg_parse(const CFG_CONTEXT con);
00879 
00889         int cfg_get_next_opt(const CFG_CONTEXT con);
00890 
00897         char *cfg_get_cur_opt(const CFG_CONTEXT con);
00898 
00905         char *cfg_get_cur_arg(const CFG_CONTEXT con);
00906 
00913         int cfg_get_cur_idx(const CFG_CONTEXT con);
00914 
00915         /* }}} */ /*@}*/
00916 
00920  /* {{{ */
00921 
00928         void cfg_print_error(const CFG_CONTEXT con);
00929 
00937         void cfg_fprint_error(const CFG_CONTEXT con, FILE *fh);
00938 
00946         char *cfg_get_error_str(const CFG_CONTEXT con);
00947 
00954         char *cfg_get_static_error_str(const int errorcode);
00955 
00956         /* }}} */ /*@}*/
00957 
00958 #ifdef __cplusplus
00959 }
00960 #endif
00961 
00962 #endif /* _PLATON_CFG_H */
00963 
00964 /* Modeline for ViM {{{
00965  * vim:set ts=4:
00966  * vim600:fdm=marker fdl=0 fdc=0:
00967  * }}} */
00968 

libcfg+ developer documentation generated by Doxygen