00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 };
00125 enum cfg_flag {
00126
00128 CFG_IGNORE_MULTI = 1,
00129
00131 CFG_IGNORE_UNKNOWN = 2,
00132
00134 CFG_PROCESS_FIRST = 0,
00135
00137 CFG_SKIP_FIRST = 4,
00138
00140 CFG_POSIXLY_LEFTOVERS = 0,
00141
00143 CFG_ADVANCED_LEFTOVERS = 8,
00144
00146 CFG_NORMAL_LEFTOVERS = 0,
00147
00149 CFG_STRICT_LEFTOVERS = 16,
00150
00152 CFG_FILE_BYTE_POS_USAGE = 0,
00153
00155 CFG_FILE_LINE_POS_USAGE = 32
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 };
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 };
00246 enum cfg_property_type {
00247
00252
00254 CFG_LINE_STOP_STRING = 0,
00255
00257 CFG_LINE_SHORT_OPTION_PREFIX = 1,
00258
00260 CFG_LINE_LONG_OPTION_PREFIX = 2,
00261
00263 CFG_LINE_OPTION_ARG_SEPARATOR = 3,
00264
00266 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR = 4,
00267
00269 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR = 5,
00270
00272 CFG_LINE_QUOTE_PREFIX = 6,
00273 CFG_LINE_QUOTE_POSTFIX = 7,
00274
00278 CFG_FILE_STOP_PREFIX = 8,
00279
00283 CFG_FILE_COMMENT_PREFIX = 9,
00284
00286 CFG_FILE_MULTI_LINE_POSTFIX = 10,
00287
00289 CFG_FILE_OPTION_ARG_SEPARATOR = 11,
00290
00292 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR = 12,
00293
00295 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR = 13,
00296
00298 CFG_FILE_QUOTE_PREFIX = 14,
00299 CFG_FILE_QUOTE_POSTFIX = 15,
00300
00308 CFG_N_PROPS = 16,
00315
00317 CFG_QUOTE = 50,
00318 CFG_LINE_QUOTE = 51,
00319 CFG_FILE_QUOTE = 52,
00320 CFG_QUOTE_PREFIX = 53,
00321 CFG_QUOTE_POSTFIX = 54,
00322
00324 CFG_MULTI_VALS_SEPARATOR = 55,
00325 CFG_FILE_MULTI_VALS_SEPARATOR = 56,
00326 CFG_LINE_MULTI_VALS_SEPARATOR = 57,
00327 CFG_NORMAL_MULTI_VALS_SEPARATOR = 58,
00328 CFG_LEFTOVER_MULTI_VALS_SEPARATOR = 59,
00329
00331 CFG_OPTION_ARG_SEPARATOR = 60,
00332
00333
00334 CFG_VIRTUAL_SPECIAL_PROPERTY = 99
00336 };
00348 enum cfg_context_type {
00349
00351 CFG_NO_CONTEXT = 0,
00352
00354 CFG_CMDLINE = 1,
00355 CFG_LINE = 1,
00356
00358 CFG_CFGFILE = 2,
00359 CFG_FILE = 2
00360 };
00361
00367 enum cfg_line_option_type {
00368
00370 CFG_NONE_OPTION = 0,
00371
00373 CFG_SHORT_OPTION = 1,
00374
00376 CFG_LONG_OPTION = 2,
00377
00379 CFG_SHORT_OPTIONS = 4,
00380
00382 CFG_LONG_SEPINIT = 8,
00383
00385 CFG_LONG_NOSEPINIT = 0
00386 };
00392 struct cfg_option {
00394 const char *cmdline_long_name;
00396 const char cmdline_short_name;
00398 const char *cfgfile_name;
00399
00402 const enum cfg_option_type type;
00403
00405 void *value;
00406
00408 int val;
00409 };
00410
00414 struct cfg_context {
00415
00420
00422 enum cfg_context_type type;
00423
00425 int flags;
00426
00428 const struct cfg_option *options;
00429
00431 long begin_pos;
00432
00435 long size;
00436
00438 int *used_opt_idx;
00439
00441 enum cfg_error error_code;
00442
00444 char **prop[CFG_N_PROPS];
00445
00447 long cur_idx;
00448 long cur_idx_tmp;
00449 int cur_opt_type;
00450
00452 char *cur_opt;
00453
00455 char *cur_arg;
00456
00463
00465 int parsing_started:1;
00466
00468 char **argv;
00469
00476
00478 char *filename;
00479
00481 FILE *fhandle;
00482
00484 };
00485
00489 typedef struct cfg_context * CFG_CONTEXT;
00490
00491
00492
00493
00494
00495 #ifdef __cplusplus
00496 extern "C" {
00497 #endif
00498
00503
00504
00512 CFG_CONTEXT cfg_get_context(struct cfg_option *options);
00513
00524 CFG_CONTEXT cfg_get_cmdline_context(
00525 long begin_pos,
00526 long size,
00527 char **argv,
00528 struct cfg_option *options);
00529
00530 #define cfg_get_cmdline_context_pos(begin_pos, end_pos, argv, options) \
00531 cfg_get_cmdline_context( \
00532 begin_pos, \
00533 end_pos - begin_pos + 1, \
00534 argv, \
00535 options)
00536
00545 CFG_CONTEXT cfg_get_cmdline_context_argc(
00546 int argc,
00547 char **argv,
00548 struct cfg_option *options);
00549
00560 CFG_CONTEXT cfg_get_cfgfile_context(
00561 long begin_pos,
00562 long size,
00563 char *filename,
00564 struct cfg_option *options);
00565
00566 #define cfg_get_cfgfile_context_pos(begin_pos, end_pos, argv, options) \
00567 cfg_get_cfgfile_context( \
00568 begin_pos, \
00569 end_pos - begin_pos + 1, \
00570 argv, \
00571 options)
00572
00583 void cfg_set_cmdline_context(
00584 const CFG_CONTEXT con,
00585 long begin_pos,
00586 long size,
00587 char **argv);
00588
00589 #define cfg_set_cmdline_context_pos(con, begin_pos, end_pos, argv) \
00590 cfg_get_cmdline_context( \
00591 con \
00592 begin_pos, \
00593 end_pos - begin_pos + 1, \
00594 argv)
00595
00604 void cfg_set_cmdline_context_argc(
00605 const CFG_CONTEXT con,
00606 int argc,
00607 char **argv);
00608
00619 void cfg_set_cfgfile_context(
00620 const CFG_CONTEXT con,
00621 long begin_pos,
00622 long size,
00623 char *filename);
00624
00625 #define cfg_set_cfgfile_context_pos(con, begin_pos, end_pos, argv) \
00626 cfg_get_cfgfile_context( \
00627 con \
00628 begin_pos, \
00629 end_pos - begin_pos + 1, \
00630 argv)
00631
00638 void cfg_reset_context(const CFG_CONTEXT con);
00639
00646 void cfg_free_context(const CFG_CONTEXT con);
00647
00648
00655
00656
00664 void cfg_set_context_flag(const CFG_CONTEXT con, int flag);
00665
00673 void cfg_clear_context_flag(const CFG_CONTEXT con, int flag);
00674
00682 int cfg_get_context_flag(const CFG_CONTEXT con, int flag);
00683
00684 #define cfg_is_context_flag(con, flag) cfg_get_context_flag(con, flag)
00685
00693 void cfg_set_context_flags(const CFG_CONTEXT con, int flags);
00694
00701 int cfg_get_context_flags(const CFG_CONTEXT con);
00702
00703
00710
00711
00720 int cfg_clear_property(
00721 const CFG_CONTEXT con, enum cfg_property_type type);
00722
00732 int cfg_add_property(
00733 const CFG_CONTEXT con, enum cfg_property_type type, char *str);
00734
00744 int cfg_remove_property(
00745 const CFG_CONTEXT con, enum cfg_property_type type, char *str);
00746
00747
00754
00755
00763 int cfg_parse(const CFG_CONTEXT con);
00764
00774 int cfg_get_next_opt(const CFG_CONTEXT con);
00775
00782 char *cfg_get_cur_opt(const CFG_CONTEXT con);
00783
00790 char *cfg_get_cur_arg(const CFG_CONTEXT con);
00791
00798 int cfg_get_cur_idx(const CFG_CONTEXT con);
00799
00800
00807
00808
00815 void cfg_print_error(const CFG_CONTEXT con);
00816
00824 void cfg_fprint_error(const CFG_CONTEXT con, FILE *fh);
00825
00833 char *cfg_get_error_str(const CFG_CONTEXT con);
00834
00841 char *cfg_get_static_error_str(const int errorcode);
00842
00843
00846 #ifdef __cplusplus
00847 }
00848 #endif
00849
00850 #endif
00851
00852
00853
00854
00855
00856