X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fdeclara.git;a=blobdiff_plain;f=lib%2Fattr.c;fp=lib%2Fattr.c;h=e74ab0a23bdfe20f9d36fd2dba0ccf84cb9c2992;hp=3f98dbc3861bf5d662d9d17452b4dabb5499037b;hb=d5c812c549b81bec3650c72f16d56b8d2302c1bb;hpb=6ec61d7d67646324136872daee17d75875b0d39d diff --git a/lib/attr.c b/lib/attr.c index 3f98dbc..e74ab0a 100644 --- a/lib/attr.c +++ b/lib/attr.c @@ -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); +}