Suporta imprimir atributo como nĂºmero.
[cascardo/declara.git] / lib / attr.c
index 3f98dbc..e74ab0a 100644 (file)
@@ -62,3 +62,29 @@ int attr_out(FILE *f, struct pmhash *attr, char *key, int size)
 {
        return fprintf(f, "%-*.*s", size, size, attr_get(attr, key) ?: "");
 }
+
+int attr_out_int(FILE *f, struct pmhash *attr, char *key, int size)
+{
+
+       /*
+        * As there is no validation of the string when parsed, there is no
+        * point returning any error when there is a parsing failure here. We
+        * emit only a warning, then. Unfortunately, there is no line
+        * information here.
+        */
+
+       char *val;
+       unsigned long long num = 0;
+       val = attr_get(attr, key);
+       if (val) {
+               char *end;
+               errno = 0;
+               num = strtoull(val, &end, 0);
+               if ((end && *end != '\0') || errno) {
+                       fprintf(stderr, "Found invalid value in attribute %s: %s: not a number\n", key, val);
+                       num = 0;
+               }
+       }
+
+       return fprintf(f, "%0*llu", size, num);
+}