From 2727d745eb46486679d780f4f4e26609fe4015ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Fri, 28 Dec 2012 20:09:07 +0100 Subject: [PATCH] Print struct tm has header --- src/data.c | 117 +++++++++++++++++++++++++++++++------------------------- src/data.h | 4 ++ src/shigofumi.c | 5 +-- 3 files changed, 69 insertions(+), 57 deletions(-) diff --git a/src/data.c b/src/data.c index 52e838c..8ce1aef 100644 --- a/src/data.c +++ b/src/data.c @@ -652,6 +652,68 @@ error: } +/* Return formatted date as mallocated string. NULL or special error string can + * be returned. Application must free even the error string. */ +char *tm2string(const struct tm *date) { + char *buffer; + size_t buffer_length = 128; + + if (!date) return NULL; + + buffer = malloc(buffer_length); + if (!buffer) return strdup(_("")); + + if (0 == strftime(buffer, buffer_length, "%x", date)) { + free(buffer); + return strdup(_("")); + } + + return buffer; +} + + +/* Convert string representation of full ISO 8601 or locale date to tm structure. + * Return NULL if error occurs + * XXX: Not all ISO formats are supported */ +struct tm *datestring2tm(const char *string) { + struct tm *date = NULL; + char *offset; + if (!string) return NULL; + + date = calloc(1, sizeof(*date)); + if (!date) return NULL; + + /* xsd:date is ISO 8601 string, thus ASCII */ + offset = strptime(string, "%Y-%m-%d", date); + if (offset && *offset == '\0') + return date; + + offset = strptime(string, "%x", date); + if (offset && *offset == '\0') + return date; + + free(date); + return NULL; +} + + +/* Print formatted header if date value is defined. + * @header is locale encoded header name */ +void print_header_tm(const char *header, struct tm *date) { + char *string; + + if (NULL == date) return; + string = tm2string(date); + + if (NULL == string) + oprintf(_("%s: \n"), header); + else + print_header(header, string); + + free(string); +} + + /* Print formatted header if UTF-9 value is defined. * @header is locale encoded header name * @value is UTF-8 encoded header value */ @@ -1521,62 +1583,13 @@ static void format_PersonName(const struct isds_PersonName *personName) { } -/* Return formatted date as mallocated string. NULL or special error string can - * be returned. Application must free even the error string. */ -char *tm2string(const struct tm *date) { - char *buffer; - size_t buffer_length = 128; - - if (!date) return NULL; - - buffer = malloc(buffer_length); - if (!buffer) return strdup(_("")); - - if (0 == strftime(buffer, buffer_length, "%x", date)) { - free(buffer); - return strdup(_("")); - } - - return buffer; -} - - -/* Convert string representation of full ISO 8601 or locale date to tm structure. - * Return NULL if error occurs - * XXX: Not all ISO formats are supported */ -struct tm *datestring2tm(const char *string) { - struct tm *date = NULL; - char *offset; - if (!string) return NULL; - - date = calloc(1, sizeof(*date)); - if (!date) return NULL; - - /* xsd:date is ISO 8601 string, thus ASCII */ - offset = strptime(string, "%Y-%m-%d", date); - if (offset && *offset == '\0') - return date; - - offset = strptime(string, "%x", date); - if (offset && *offset == '\0') - return date; - - free(date); - return NULL; -} - - static void format_BirthInfo(const struct isds_BirthInfo *birth) { - char *date; if (!birth || !(birth->biDate || birth->biCity || birth->biCounty || birth->biState)) return; oprintf(_("Birth details:\n")); - date = tm2string(birth->biDate); - print_header(_("\tDate"), date); - free(date); - + print_header_tm(_("\tDate"), birth->biDate); print_header_utf8(_("\tCity"), birth->biCity); print_header_utf8(_("\tCounty"), birth->biCounty); print_header_utf8(_("\tState"), birth->biState); @@ -1677,9 +1690,7 @@ void format_DbUserInfo(const struct isds_DbUserInfo *info) { format_PersonName(info->personName); format_Address(info->address); - buffer = tm2string(info->biDate); - print_header(_("Birth date"), buffer); - zfree(buffer); + print_header_tm(_("Birth date"), info->biDate); format_supervising_firm(info->ic, info->firmName); format_contact_address(info->caStreet, info->caCity, info->caZipCode, diff --git a/src/data.h b/src/data.h index 8b48f25..017e3d8 100644 --- a/src/data.h +++ b/src/data.h @@ -41,6 +41,10 @@ void print_header(const char *header, const char *value); * @header is locale encoded header name */ void print_header_timeval(const char *header, struct timeval *time); +/* Print formatted header if date value is defined. + * @header is locale encoded header name */ +void print_header_tm(const char *header, struct tm *date); + void print_message_list(const struct isds_list *messages, _Bool outgoing); void format_message(const struct isds_message *message); diff --git a/src/shigofumi.c b/src/shigofumi.c index 4293c16..e7deac5 100644 --- a/src/shigofumi.c +++ b/src/shigofumi.c @@ -4146,7 +4146,6 @@ static int shi_resign(int argc, const char **argv) { void *resigned_data = NULL; /* Dynamic, stored into message */ size_t data_length = 0, resigned_data_length = 0; struct tm *valid_to = NULL; /* Dynamic */ - char *valid_to_string = NULL; /* Dynamic */ isds_error err; if (!argv || argc > 3) { @@ -4189,10 +4188,8 @@ static int shi_resign(int argc, const char **argv) { return -1; } - valid_to_string = tm2string(valid_to); + print_header_tm(_("New time stamp expires"), valid_to); free(valid_to); - print_header(_("New time stamp expires"), valid_to_string); - free(valid_to_string); return do_load_anything(resigned_data, resigned_data_length, BUFFER_MOVE); } -- 2.11.4.GIT