Suporta imprimir atributo como nĂºmero.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 28 Jun 2020 01:02:57 +0000 (22:02 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 28 Jun 2020 14:40:44 +0000 (11:40 -0300)
lib/attr.c
lib/attr.h

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);
+}
index c0e3bd1..8515f86 100644 (file)
@@ -26,5 +26,6 @@ int attr_set(struct pmhash **pmhash, char *key, char *val);
 int attr_parse(struct pmhash **pmhash, char *arg);
 char * attr_get(struct pmhash *pmhash, char *key);
 int attr_out(FILE *f, struct pmhash *attr, char *key, int size);
+int attr_out_int(FILE *f, struct pmhash *attr, char *key, int size);
 
 #endif