]> mj.ucw.cz Git - libucw.git/commitdiff
One more item type: u64.
authorMartin Mares <mj@ucw.cz>
Sat, 29 Nov 2003 11:47:02 +0000 (11:47 +0000)
committerMartin Mares <mj@ucw.cz>
Sat, 29 Nov 2003 11:47:02 +0000 (11:47 +0000)
lib/conf-test.c
lib/conf.c
lib/conf.h

index 719a0cfd35e4ee29a1b132935308a94f8b339c9c..47eff30ba223e3a00d06ac5da83acf3eb06fb3d4 100644 (file)
@@ -6,7 +6,7 @@
 #include <stdio.h>
 #include <string.h>
 
-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);
 
index b27745faeea8f90a0c0bab6e3d2cf809adbe42d0..844c51cb764300fadb60e59221056855e36f3684 100644 (file)
@@ -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";
        }
index 774ea1fd838885c83328d0b6e045059902aa2421..cb3e9e1d7d326520ae8642c5432b1212d838ab4a 100644 (file)
@@ -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);
 
 /*