From 97c88ec31ee3ce21bab2ae0cba93184b18298eb9 Mon Sep 17 00:00:00 2001 From: teodor Date: Fri, 13 Feb 2009 12:35:43 +0000 Subject: [PATCH] Add INFGetBollean method --- data/test.inf | 2 ++ expected/inf | 4 ++++ inftest.c | 6 ++++++ prs_inf.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ prs_inf.h | 2 +- tools.h | 4 ++++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/data/test.inf b/data/test.inf index c0b254b..b2fdcb5 100644 --- a/data/test.inf +++ b/data/test.inf @@ -11,6 +11,8 @@ HAHA\ #wwekljrw [test2] #sfljkl'sdf #dfghjkl +true= enaBle +false= no t2="qweqwe#asd" t2="qweqwe#as\"d" #HEH t5="hsdf diff --git a/expected/inf b/expected/inf index 11dbc3f..c01c922 100644 --- a/expected/inf +++ b/expected/inf @@ -5,6 +5,8 @@ S:'test' K:'kde' V:'v' S:'test' K:'asdfas' V:' HAHA ' +S:'test2' K:'true' V:'enaBle' +S:'test2' K:'false' V:'no' S:'test2' K:'t2' V:'qweqwe#asd' S:'test2' K:'t2' V:'qweqwe#as"d' S:'test2' K:'t5' V:'hsdf @@ -15,3 +17,5 @@ sdf sdfssd f \ sdf sdf sdf ' +test2.true: 1 +test2.false: 0 diff --git a/inftest.c b/inftest.c index 9f76304..a744466 100644 --- a/inftest.c +++ b/inftest.c @@ -34,6 +34,7 @@ int main(int argn, char *argv[] ) { InfMap *map, *ptr; + int boolval; if ( argn!=2 ) { puts("Usage:"); @@ -50,6 +51,11 @@ main(int argn, char *argv[] ) { ); ptr++; } + + INFGetBoolean(map, "test2", "true", &boolval); + printf("test2.true: %d\n", boolval); + INFGetBoolean(map, "test2", "false", &boolval); + printf("test2.false: %d\n", boolval); INFFree(map); return 0; } diff --git a/prs_inf.c b/prs_inf.c index 5651e33..1f4b633 100644 --- a/prs_inf.c +++ b/prs_inf.c @@ -327,4 +327,48 @@ INFGetString(InfMap *inf, char *sect, char *key, char **val) { return 1; } +static int +parseBool(char *str) +{ + struct { + char *key; + int val; + } + bdata[] = + { + {"1", 1}, + {"0", 0}, + {"on", 1}, + {"off", 0}, + {"enable", 1}, + {"disable", 0}, + {"true", 1}, + {"false", 0}, + {"yes", 1}, + {"no", 0} + }; + int i; + + while(*str && isspace(*str)) + str++; + + for(i=0;ivalue); + return 0; + } + return 1; +} + diff --git a/prs_inf.h b/prs_inf.h index 4694cb7..ddc2a43 100644 --- a/prs_inf.h +++ b/prs_inf.h @@ -48,5 +48,5 @@ int INFGetUint(InfMap *inf, char *sect, char *key, u_int32_t *val); int INFGetFloat(InfMap *inf, char *sect, char *key, float *val); int INFGetDouble(InfMap *inf, char *sect, char *key, double *val); int INFGetString(InfMap *inf, char *sect, char *key, char **val); - +int INFGetBoolean(InfMap *inf, char *sect, char *key, int *val); #endif diff --git a/tools.h b/tools.h index f4ee5b7..a7c9b09 100644 --- a/tools.h +++ b/tools.h @@ -46,6 +46,10 @@ double elapsedtime(struct timeval *begin); #define offsetof(type, field) ((int) &((type *)0)->field) #endif /* offsetof */ +#ifndef lengthof +#define lengthof(array) (sizeof (array) / sizeof ((array)[0])) +#endif + #ifndef TYPEALIGN #define TYPEALIGN(ALIGNVAL,LEN) \ (((long) (LEN) + ((ALIGNVAL) - 1)) & ~((long) ((ALIGNVAL) - 1))) -- 2.37.3