X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gedcom%2Fage.c;h=1de1d53205495398126aa2c0e62a6f6f61922edb;hb=0a66eda71b0d2f65ee968816b48a9311c17cbf34;hp=8518411252cd952b0e6ff6c221ca12fed190ee27;hpb=fffe9618ff8a5cfcac207f231b531615dfb0e38f;p=gedcom-parse.git diff --git a/gedcom/age.c b/gedcom/age.c index 8518411..1de1d53 100644 --- a/gedcom/age.c +++ b/gedcom/age.c @@ -27,11 +27,20 @@ #include #include #include "gedcom_internal.h" +#include "buffer.h" #include "age.h" struct age_value age_s; struct age_value def_age_val; +void cleanup_age_buffer(); +struct safe_buffer age_buffer = { NULL, 0, NULL, 0, cleanup_age_buffer }; + +void cleanup_age_buffer() +{ + cleanup_buffer(&age_buffer); +} + void copy_age(struct age_value *to, struct age_value from) { memcpy(to, &from, sizeof(struct age_value)); @@ -111,7 +120,7 @@ struct age_value gedcom_parse_age(const char* line_value) while (*ptr == ' ') ptr++; } - if (isdigit(*ptr)) { + if (isdigit((unsigned char)*ptr)) { int result = parse_numeric_age(&age_s, ptr); if (result == 0) { age_s.type = AGE_NUMERIC; @@ -130,3 +139,50 @@ struct age_value gedcom_parse_age(const char* line_value) return age_s; } +char* gedcom_age_to_string(struct age_value* val) +{ + int num = 0; + reset_buffer(&age_buffer); + + switch (val->mod) { + case AGE_LESS_THAN: + safe_buf_append(&age_buffer, "<"); break; + case AGE_GREATER_THAN: + safe_buf_append(&age_buffer, ">"); break; + default: + break; + } + + switch (val->type) { + case AGE_UNRECOGNIZED: + return val->phrase; break; + case AGE_CHILD: + safe_buf_append(&age_buffer, "CHILD"); break; + case AGE_INFANT: + safe_buf_append(&age_buffer, "INFANT"); break; + case AGE_STILLBORN: + safe_buf_append(&age_buffer, "STILLBORN"); break; + case AGE_NUMERIC: + if (val->years != -1) { + num = 1; + safe_buf_append(&age_buffer, "%dy", val->years); + } + if (val->months != -1) { + if (num) + safe_buf_append(&age_buffer, " "); + num = 1; + safe_buf_append(&age_buffer, "%dm", val->months); + } + if (val->days != -1) { + if (num) + safe_buf_append(&age_buffer, " "); + num = 1; + safe_buf_append(&age_buffer, "%dd", val->days); + } + break; + default: + break; + } + + return get_buf_string(&age_buffer); +}