2 * libcfg+ - precise command line & config file parsing library
4 * props.c - context properties manipulation
5 * ____________________________________________________________
7 * Developed by Ondrej Jombik <nepto@platon.sk>
8 * and Lubomir Host <rajo@platon.sk>
9 * Copyright (c) 2001-2004 Platon SDG, http://platon.sk/
10 * All rights reserved.
12 * See README file for more information about this software.
13 * See COPYING file for license information.
15 * Download the latest version from
16 * http://platon.sk/projects/libcfg+/
19 /* $Platon: libcfg+/src/props.c,v 1.34 2004/01/12 06:03:09 nepto Exp $ */
29 #include <platon/str/strdyn.h>
33 /* Default properties {{{ */
34 char *cfg_default_properties[CFG_N_PROPS][4] = {
35 /* CFG_LINE_STOP_STRING */ { NULL, NULL, NULL, NULL },
36 /* CFG_LINE_SHORT_OPTION_PREFIX */ { "-", NULL, NULL, NULL },
37 /* CFG_LINE_LONG_OPTION_PREFIX */ { "--", NULL, NULL, NULL },
38 /* CFG_LINE_OPTION_ARG_SEPARATOR */ { "=", NULL, NULL, NULL },
39 /* CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR */ { ",", ";", NULL, NULL },
40 /* CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR */ { NULL, NULL, NULL, NULL },
41 /* CFG_LINE_QUOTE_PREFIX */ { NULL, NULL, NULL, NULL },
42 /* CFG_LINE_QUOTE_POSTFIX */ { NULL, NULL, NULL, NULL },
43 /* CFG_FILE_STOP_PREFIX */ { NULL, NULL, NULL, NULL },
44 /* CFG_FILE_COMMENT_PREFIX */ { "#", ";", NULL, NULL },
45 /* CFG_FILE_MULTI_LINE_POSTFIX */ { "\\", NULL, NULL, NULL },
46 /* CFG_FILE_OPTION_ARG_SEPARATOR */ { "=", NULL, NULL, NULL },
47 /* CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR */ { ",", ";", " ", NULL },
48 /* CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR */ { " ", NULL, NULL, NULL },
49 /* CFG_FILE_QUOTE_PREFIX */ { "\"", "'", NULL, NULL },
50 /* CFG_FILE_QUOTE_POSTFIX */ { "\"", "'", NULL, NULL },
54 * Static functions for strdyn manipulation
58 strdyn_array_clear(where)
65 *where = PLATON_FUNC(strdyn_remove_all)(*where);
67 return *where != NULL;
71 strdyn_array_add(where, str)
78 *where = strdyn_create();
81 *where = PLATON_FUNC(strdyn_add)(*where, str);
83 return *where != NULL;
87 strdyn_array_remove(where, str)
95 *where = PLATON_FUNC(strdyn_remove_str_all)(*where, str);
97 return *where != NULL;
102 * Functions for context flags manipulation.
106 cfg_set_context_flag(con, flag)
107 const CFG_CONTEXT con;
114 cfg_clear_context_flag(con, flag)
115 const CFG_CONTEXT con;
122 cfg_get_context_flag(con, flag)
123 const CFG_CONTEXT con;
126 return con->flags & flag;
131 cfg_set_context_flags(con, flags)
132 const CFG_CONTEXT con;
136 con->cur_idx_tmp = con->flags & CFG_PROCESS_FIRST ? 0 : 1;
140 cfg_get_context_flags(con)
141 const CFG_CONTEXT con;
150 #define cfg_normal_property_type(type) ((type) >= 0 && (type) < CFG_N_PROPS)
151 #define cfg_virtual_property_type(type) ((type) > CFG_N_PROPS)
158 cfg_clear_property(con, type)
159 const CFG_CONTEXT con;
160 enum cfg_property_type type;
162 if (cfg_normal_property_type(type)) {
163 return strdyn_array_clear(&(con->prop[type]));
165 else if (cfg_virtual_property_type(type)) {
166 register int ret = 1;
168 default: /* unknown special property; do nothing */
172 ret &= cfg_clear_property(con, CFG_LINE_QUOTE);
173 ret &= cfg_clear_property(con, CFG_FILE_QUOTE);
176 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_PREFIX);
177 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_POSTFIX);
180 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_PREFIX);
181 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_POSTFIX);
183 case CFG_QUOTE_PREFIX:
184 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_PREFIX);
185 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_PREFIX);
187 case CFG_QUOTE_POSTFIX:
188 ret &= cfg_clear_property(con, CFG_LINE_QUOTE_POSTFIX);
189 ret &= cfg_clear_property(con, CFG_FILE_QUOTE_POSTFIX);
191 case CFG_MULTI_VALS_SEPARATOR:
192 ret &= cfg_clear_property(con, CFG_LINE_MULTI_VALS_SEPARATOR);
193 ret &= cfg_clear_property(con, CFG_FILE_MULTI_VALS_SEPARATOR);
195 case CFG_LINE_MULTI_VALS_SEPARATOR:
196 ret &= cfg_clear_property(con,
197 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR);
198 ret &= cfg_clear_property(con,
199 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR);
201 case CFG_FILE_MULTI_VALS_SEPARATOR:
202 ret &= cfg_clear_property(con,
203 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR);
204 ret &= cfg_clear_property(con,
205 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR);
207 case CFG_NORMAL_MULTI_VALS_SEPARATOR:
208 ret &= cfg_clear_property(con,
209 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR);
210 ret &= cfg_clear_property(con,
211 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR);
213 case CFG_LEFTOVER_MULTI_VALS_SEPARATOR:
214 ret &= cfg_clear_property(con,
215 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR);
216 ret &= cfg_clear_property(con,
217 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR);
219 case CFG_OPTION_ARG_SEPARATOR:
220 ret &= cfg_clear_property(con, CFG_LINE_OPTION_ARG_SEPARATOR);
221 ret &= cfg_clear_property(con, CFG_FILE_OPTION_ARG_SEPARATOR);
228 return 0; /* failure */
232 cfg_clear_properties(
233 const CFG_CONTEXT con,
234 enum cfg_property_type type, ...)
237 enum cfg_property_type tmp_type;
244 /* argument list must be termited with typeN = CFG_EOT */
245 while (tmp_type != CFG_EOT) {
246 ret &= cfg_clear_property(con, tmp_type);
249 tmp_type = va_arg(ap, enum cfg_property_type);
263 cfg_add_property(con, type, str)
264 const CFG_CONTEXT con;
265 enum cfg_property_type type;
270 remove cfg_add_property_old() calls from here,
271 substitute it by new function cfg_add_properties() */
273 if (cfg_normal_property_type(type)) {
274 return strdyn_array_add(&(con->prop[type]), str);
276 else if (cfg_virtual_property_type(type)) {
277 register int ret = 1;
279 default: /* unknown special property; do nothing */
283 ret &= cfg_add_property(con, CFG_LINE_QUOTE, str);
284 ret &= cfg_add_property(con, CFG_FILE_QUOTE, str);
287 ret &= cfg_add_property(con, CFG_LINE_QUOTE_PREFIX, str);
288 ret &= cfg_add_property(con, CFG_LINE_QUOTE_POSTFIX, str);
291 ret &= cfg_add_property(con, CFG_FILE_QUOTE_PREFIX, str);
292 ret &= cfg_add_property(con, CFG_FILE_QUOTE_POSTFIX, str);
294 case CFG_QUOTE_PREFIX:
295 ret &= cfg_add_property(con, CFG_LINE_QUOTE_PREFIX, str);
296 ret &= cfg_add_property(con, CFG_FILE_QUOTE_PREFIX, str);
298 case CFG_QUOTE_POSTFIX:
299 ret &= cfg_add_property(con, CFG_LINE_QUOTE_POSTFIX, str);
300 ret &= cfg_add_property(con, CFG_FILE_QUOTE_POSTFIX, str);
302 case CFG_MULTI_VALS_SEPARATOR:
303 ret &= cfg_add_property(con,
304 CFG_LINE_MULTI_VALS_SEPARATOR, str);
305 ret &= cfg_add_property(con,
306 CFG_FILE_MULTI_VALS_SEPARATOR, str);
308 case CFG_LINE_MULTI_VALS_SEPARATOR:
309 ret &= cfg_add_property(con,
310 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
311 ret &= cfg_add_property(con,
312 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
314 case CFG_FILE_MULTI_VALS_SEPARATOR:
315 ret &= cfg_add_property(con,
316 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
317 ret &= cfg_add_property(con,
318 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
320 case CFG_NORMAL_MULTI_VALS_SEPARATOR:
321 ret &= cfg_add_property(con,
322 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
323 ret &= cfg_add_property(con,
324 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
326 case CFG_LEFTOVER_MULTI_VALS_SEPARATOR:
327 ret &= cfg_add_property(con,
328 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
329 ret &= cfg_add_property(con,
330 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
332 case CFG_OPTION_ARG_SEPARATOR:
333 ret &= cfg_add_property(con,
334 CFG_LINE_OPTION_ARG_SEPARATOR, str);
335 ret &= cfg_add_property(con,
336 CFG_FILE_OPTION_ARG_SEPARATOR, str);
343 return 0; /* failure */
348 const CFG_CONTEXT con,
349 enum cfg_property_type type,
353 enum cfg_property_type tmp_type;
362 /* argument list must be termited with typeN = CFG_EOT or
365 while (tmp_type != CFG_EOT && tmp_str != NULL) {
366 ret &= cfg_add_property(con, tmp_type, tmp_str);
369 tmp_type = va_arg(ap, enum cfg_property_type);
370 if (tmp_type == CFG_EOT) /* if typeN == CFG_EOT, strN may be not specified! */
372 tmp_str = va_arg(ap, char *);
382 cfg_add_properties_str(
383 const CFG_CONTEXT con,
385 enum cfg_property_type type, ...)
388 enum cfg_property_type tmp_type;
395 /* argument list must be termited with typeN = CFG_EOT or */
397 while (tmp_type != CFG_EOT) {
398 ret &= cfg_add_property(con, tmp_type, str);
401 tmp_type = va_arg(ap, enum cfg_property_type);
411 cfg_add_properties_type(
412 const CFG_CONTEXT con,
413 enum cfg_property_type type,
424 /* argument list must be termited with str = NULL */
426 while (tmp_str != NULL) {
427 ret &= cfg_add_property(con, type, tmp_str);
430 tmp_str = va_arg(ap, char *);
444 cfg_remove_property(con, type, str)
445 const CFG_CONTEXT con;
446 enum cfg_property_type type;
451 remove cfg_remove_property_old() calls from here,
452 substitute it by new function cfg_remove_properties() */
454 if (cfg_normal_property_type(type)) {
455 return strdyn_array_remove(&(con->prop[type]), str);
457 else if (cfg_virtual_property_type(type)) {
458 register int ret = 1;
460 default: /* unknown special property; do nothing */
464 ret &= cfg_remove_property(con, CFG_LINE_QUOTE, str);
465 ret &= cfg_remove_property(con, CFG_FILE_QUOTE, str);
468 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_PREFIX, str);
469 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_POSTFIX, str);
472 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_PREFIX, str);
473 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_POSTFIX, str);
475 case CFG_QUOTE_PREFIX:
476 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_PREFIX, str);
477 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_PREFIX, str);
479 case CFG_QUOTE_POSTFIX:
480 ret &= cfg_remove_property(con, CFG_LINE_QUOTE_POSTFIX, str);
481 ret &= cfg_remove_property(con, CFG_FILE_QUOTE_POSTFIX, str);
483 case CFG_MULTI_VALS_SEPARATOR:
484 ret &= cfg_remove_property(con,
485 CFG_LINE_MULTI_VALS_SEPARATOR, str);
486 ret &= cfg_remove_property(con,
487 CFG_FILE_MULTI_VALS_SEPARATOR, str);
489 case CFG_LINE_MULTI_VALS_SEPARATOR:
490 ret &= cfg_remove_property(con,
491 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
492 ret &= cfg_remove_property(con,
493 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
495 case CFG_FILE_MULTI_VALS_SEPARATOR:
496 ret &= cfg_remove_property(con,
497 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
498 ret &= cfg_remove_property(con,
499 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
501 case CFG_NORMAL_MULTI_VALS_SEPARATOR:
502 ret &= cfg_remove_property(con,
503 CFG_LINE_NORMAL_MULTI_VALS_SEPARATOR, str);
504 ret &= cfg_remove_property(con,
505 CFG_FILE_NORMAL_MULTI_VALS_SEPARATOR, str);
507 case CFG_LEFTOVER_MULTI_VALS_SEPARATOR:
508 ret &= cfg_remove_property(con,
509 CFG_LINE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
510 ret &= cfg_remove_property(con,
511 CFG_FILE_LEFTOVER_MULTI_VALS_SEPARATOR, str);
513 case CFG_OPTION_ARG_SEPARATOR:
514 ret &= cfg_remove_property(con,
515 CFG_LINE_OPTION_ARG_SEPARATOR, str);
516 ret &= cfg_remove_property(con,
517 CFG_FILE_OPTION_ARG_SEPARATOR, str);
524 return 0; /* failure */
528 cfg_remove_properties( /* code same like cfg_add_properties() */
529 const CFG_CONTEXT con,
530 enum cfg_property_type type,
534 enum cfg_property_type tmp_type;
543 /* argument list must be termited with typeN = CFG_EOT or
546 while (tmp_type != CFG_EOT && tmp_str != NULL) {
547 ret &= cfg_remove_property(con, tmp_type, tmp_str);
550 tmp_type = va_arg(ap, enum cfg_property_type);
551 if (tmp_type == CFG_EOT) /* if typeN == CFG_EOT, strN may be not specified! */
553 tmp_str = va_arg(ap, char *);
563 cfg_remove_properties_str(
564 const CFG_CONTEXT con,
566 enum cfg_property_type type, ...)
569 enum cfg_property_type tmp_type;
576 /* argument list must be termited with typeN = CFG_EOT or */
578 while (tmp_type != CFG_EOT) {
579 ret &= cfg_remove_property(con, tmp_type, str);
582 tmp_type = va_arg(ap, enum cfg_property_type);
592 cfg_remove_properties_type(
593 const CFG_CONTEXT con,
594 enum cfg_property_type type,
605 /* argument list must be termited with str = NULL */
607 while (tmp_str != NULL) {
608 ret &= cfg_remove_property(con, type, tmp_str);
611 tmp_str = va_arg(ap, char *);
621 /* Modeline for ViM {{{
623 * vim600:fdm=marker fdl=0 fdc=0: