File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / tests / setopt_ptr.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:49:17 2021 UTC (3 years, 3 months ago) by misho
Branches: confuse, MAIN
CVS tags: v3_3, HEAD
confuse 3.3

    1: #include "check_confuse.h"
    2: #include <stdio.h>
    3: #include <stdlib.h>
    4: 
    5: static int ptr_count;
    6: 
    7: static int parse_ptr(cfg_t *cfg, cfg_opt_t *opt,
    8: 		     const char *value, void *result)
    9: {
   10: 	int *ptr = malloc(sizeof(int));
   11: 	if (!ptr)
   12: 		return -1;
   13: 
   14: 	*ptr = atoi(value);
   15: 	*(void **)result = ptr;
   16: 
   17: 	fprintf(stderr, "make ptr %p (value '%s')\n", ptr, value);
   18: 	ptr_count++;
   19: 
   20: 	return 0;
   21: }
   22: 
   23: static void free_ptr(void *ptr)
   24: {
   25: 	ptr_count--;
   26: 	fprintf(stderr, "free ptr %p\n", ptr);
   27: 
   28: 	free(ptr);
   29: }
   30: 
   31: int main(void)
   32: {
   33: 	cfg_opt_t opts[] = {
   34: 		CFG_PTR_CB("ptr", "1", CFGF_NONE, parse_ptr, free_ptr),
   35: 		CFG_END()
   36: 	};
   37: 
   38: 	cfg_t *cfg = cfg_init(opts, 0);
   39: 	fail_unless(cfg_setopt(cfg, cfg_getopt(cfg, "ptr"), "2"));
   40: 	fail_unless(cfg_setopt(cfg, cfg_getopt(cfg, "ptr"), "3"));
   41: 	char *ptr4[] = { "4" };
   42: 	fail_unless(cfg_setmulti(cfg, "ptr", 1, ptr4) == CFG_SUCCESS);
   43: 	char *ptr5[] = { "5" };
   44: 	fail_unless(cfg_setmulti(cfg, "ptr", 1, ptr5) == CFG_SUCCESS);
   45: 	fail_unless(cfg_parse_buf(cfg, "ptr = 6") == CFG_SUCCESS);
   46: 	fail_unless(cfg_parse_buf(cfg, "ptr = 7") == CFG_SUCCESS);
   47: 	cfg_free(cfg);
   48: 
   49: 	/* Is malloc/free of ptrs balanced? */
   50: 	fail_unless(ptr_count == 0);
   51: 
   52: 	return 0;
   53: }
   54: 
   55: /**
   56:  * Local Variables:
   57:  *  indent-tabs-mode: t
   58:  *  c-file-style: "linux"
   59:  * End:
   60:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>