From: Martin Mares Date: Sat, 29 Nov 2003 11:47:02 +0000 (+0000) Subject: One more item type: u64. X-Git-Tag: holmes-import~1156 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=9032f650148717ff68b03c55ecc4d466695c6e74;p=libucw.git One more item type: u64. --- diff --git a/lib/conf-test.c b/lib/conf-test.c index 719a0cfd..47eff30b 100644 --- a/lib/conf-test.c +++ b/lib/conf-test.c @@ -6,7 +6,7 @@ #include #include -static int robert=14; +static u64 robert=14; static int spalek=-3; static char *heslo="prazdne"; static int nastaveni1=0,nastaveni2=1; @@ -36,7 +36,7 @@ static byte *set_nastaveni(struct cfitem *item, byte *value) static struct cfitem jmeno[]={ {"jmeno", CT_SECTION, NULL}, - {"robert", CT_INT, &robert}, + {"robert", CT_U64, &robert}, {"spalek", CT_INT, &spalek}, {"heslo", CT_STRING, &heslo}, {"nastaveni1", CT_FUNCTION, &set_nastaveni}, @@ -120,8 +120,8 @@ int main(int argc, char *argv[]) printf ("\n"); } - printf("robert=%d, spalek=%d, heslo=%s, nastaveni1/2=%d/%d decker=%f\n", - robert,spalek,heslo,nastaveni1,nastaveni2,decker); + printf("robert=%Ld, spalek=%d, heslo=%s, nastaveni1/2=%d/%d decker=%f\n", + (long long)robert,spalek,heslo,nastaveni1,nastaveni2,decker); printf("vek=%d, vyska=%d, vaha=%d\n", vek,vyska,vaha); diff --git a/lib/conf.c b/lib/conf.c index b27745fa..844c51cb 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -149,6 +149,35 @@ byte *cf_parse_int(byte *value, uns *varp) return msg; } +byte *cf_parse_u64(byte *value, u64 *varp) +{ + char *msg = NULL; + const struct unit *u; + + if (!*value) + msg = "Missing number"; + else { + errno = 0; + char *end; + u64 x = strtoull(value, &end, 0); + if (errno == ERANGE) + msg = cf_rngerr; + else if (u = cf_lookup_unit(value, end, &msg)) { + if (x > ~(u64)0 / u->num) + msg = "Number out of range"; + else { + x *= u->num; + if (x % u->den) + msg = "Number is not an integer"; + else + *varp = x / u->den; + } + } else + *varp = x; + } + return msg; +} + byte *cf_parse_double(byte *value, double *varp) { char *msg = NULL; @@ -194,6 +223,9 @@ byte *cf_set_item(byte *sect, byte *name, byte *value) case CT_DOUBLE: msg = cf_parse_double(value, (double *) item->var); break; + case CT_U64: + msg = cf_parse_u64(value, (u64 *) item->var); + break; default: msg = "Unknown keyword"; } diff --git a/lib/conf.h b/lib/conf.h index 774ea1fd..cb3e9e1d 100644 --- a/lib/conf.h +++ b/lib/conf.h @@ -34,7 +34,7 @@ byte *cfg_stralloc(byte *s); * function. */ -enum cftype { CT_STOP, CT_SECTION, CT_INCOMPLETE_SECTION, CT_INT, CT_STRING, CT_FUNCTION, CT_DOUBLE }; +enum cftype { CT_STOP, CT_SECTION, CT_INCOMPLETE_SECTION, CT_INT, CT_STRING, CT_FUNCTION, CT_DOUBLE, CT_U64 }; struct cfitem { byte *name; @@ -60,6 +60,7 @@ void cf_read(byte *filename); */ byte *cf_parse_int(byte *value, uns *varp); +byte *cf_parse_u64(byte *value, u64 *varp); byte *cf_parse_double(byte *value, double *varp); /*