From a4c65684bf04fecd9e5f958b8115bc97ec926fc9 Mon Sep 17 00:00:00 2001 From: Yann Weber Date: Thu, 9 Mar 2017 12:24:31 +0100 Subject: [PATCH] Implement & use a ttail_set_fmt() + tests --- src/include/ttail_init.h | 125 +++++++++++++++++++++++++++++++++++ src/ttail_init.c | 28 ++++++-- tests/ttail_argparse_check.c | 7 ++ tests/ttail_init_check.c | 35 +++++++++- tests/ttail_search_check.c | 4 +- 5 files changed, 187 insertions(+), 12 deletions(-) diff --git a/src/include/ttail_init.h b/src/include/ttail_init.h index cc2dad0..1f3fbae 100644 --- a/src/include/ttail_init.h +++ b/src/include/ttail_init.h @@ -55,6 +55,131 @@ int ttail_add_logfile(ttail_t*, const char*); */ int ttail_set_prefix(ttail_t*, const char*); +/**@brief Set a date format + * + * The supported input field descriptors are listed below. In case a text string (such as the name + * of a day of the week or a month name) is to be matched, the comparison is case insensitive. In + * case a number is to be matched, leading zeros are permitted but not required. + * + * \%% The % character. + * + * \%a or %A + * The name of the day of the week according to the current locale, in abbreviated form or + * the full name. + * + * \%b or %B or %h + * The month name according to the current locale, in abbreviated form or the full name. + * + * \%c The date and time representation for the current locale. + * + * \%C The century number (0-99). + * + * \%d or %e + * The day of month (1-31). + * + * \%D Equivalent to %m/%d/%y. (This is the American style date, very confusing to non-Ameri‐ + * cans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard format + * is %Y-%m-%d.) + * + * \%H The hour (0-23). + * + * \%I The hour on a 12-hour clock (1-12). + * + * \%j The day number in the year (1-366). + * + * \%m The month number (1-12). + * + * \%M The minute (0-59). + * + * \%n Arbitrary whitespace. + * + * \%p The locale's equivalent of AM or PM. (Note: there may be none.) + * + * \%r The 12-hour clock time (using the locale's AM or PM). In the POSIX locale equivalent to + * \%I:%M:%S %p. If t_fmt_ampm is empty in the LC_TIME part of the current locale, then the + * behavior is undefined. + * + * \%R Equivalent to %H:%M. + * + * \%S The second (0-60; 60 may occur for leap seconds; earlier also 61 was allowed). + * + * \%t Arbitrary whitespace. + * + * \%T Equivalent to %H:%M:%S. + * + * \%U The week number with Sunday the first day of the week (0-53). The first Sunday of Janu‐ + * ary is the first day of week 1. + * + * \%w The ordinal number of the day of the week (0-6), with Sunday = 0. + * + * \%W The week number with Monday the first day of the week (0-53). The first Monday of Janu‐ + * ary is the first day of week 1. + * + * \%x The date, using the locale's date format. + * + * \%X The time, using the locale's time format. + * + * \%y The year within century (0-99). When a century is not otherwise specified, values in the + * range 69-99 refer to years in the twentieth century (1969-1999); values in the range + * 00-68 refer to years in the twenty-first century (2000-2068). + * + * \%Y The year, including century (for example, 1991). + * + * Some field descriptors can be modified by the E or O modifier characters to indicate that an + * alternative format or specification should be used. If the alternative format or specification + * does not exist in the current locale, the unmodified field descriptor is used. + * + * + * The E modifier specifies that the input string may contain alternative locale-dependent versions + * of the date and time representation: + * + * \%Ec The locale's alternative date and time representation. + * + * \%EC The name of the base year (period) in the locale's alternative representation. + * + * \%Ex The locale's alternative date representation. + * + * \%EX The locale's alternative time representation. + * + * \%Ey The offset from %EC (year only) in the locale's alternative representation. + * + * \%EY The full alternative year representation. + * + * The O modifier specifies that the numerical input may be in an alternative locale-dependent for‐ + * mat: + * + * \%Od or %Oe + * The day of the month using the locale's alternative numeric symbols; leading zeros are + * permitted but not required. + * + * \%OH The hour (24-hour clock) using the locale's alternative numeric symbols. + * + * \%OI The hour (12-hour clock) using the locale's alternative numeric symbols. + * + * \%Om The month using the locale's alternative numeric symbols. + * + * \%OM The minutes using the locale's alternative numeric symbols. + * + * \%OS The seconds using the locale's alternative numeric symbols. + * + * \%OU The week number of the year (Sunday as the first day of the week) using the locale's + * alternative numeric symbols. + * + * \%Ow The ordinal number of the day of the week (Sunday=0), + * using the locale's alternative numeric symbols. + * + * \%OW The week number of the year (Monday as the first day of the week) using the locale's + * alternative numeric symbols. + * + * \%Oy The year (offset from %C) using the locale's alternative numeric symbols. + * + * + *@param ttail_t* + *@param const char * format + *@return 0 if no errors 1 if allready set -1 if compilation fails + */ +int ttail_set_fmt(ttail_t*, const char*); + /**@brief Set dates min/max * *After the call dates are free and set to NULL except if error diff --git a/src/ttail_init.c b/src/ttail_init.c index 60b8fc6..4a8ac10 100644 --- a/src/ttail_init.c +++ b/src/ttail_init.c @@ -23,6 +23,7 @@ ttail_t *ttail_init(int argc, char **argv) res->logfile_name = NULL; res->logfile_sz = 0; res->prefix_sz = -1; + res->session = NULL; memset(&(res->date_min), 0, sizeof(struct tm)); memset(&(res->date_max), 0, sizeof(struct tm)); @@ -80,16 +81,10 @@ have to be set BEFORE -r option\n"); given\n"); goto ttail_init_err; } - res->fmt = malloc( - sizeof(char)*(strlen(optarg)+1)); - if(!res->fmt) + if(ttail_set_fmt(res, optarg) < 0) { - perror("Unable to allocate memory\ -for date format"); goto ttail_init_err; } - strcpy(res->fmt, optarg); - res->flag |= TTAIL_FLAG_FORMAT; break; case 'l': @@ -279,6 +274,25 @@ error message"); return -1; } +int ttail_set_fmt(ttail_t* t, const char* fmt) +{ + size_t len; + if(t->flag & TTAIL_FLAG_FORMAT) + { + return -1; + } + len = strlen(fmt); + t->fmt = malloc(sizeof(char)*(len+1)); + if(!t->fmt) + { + return -1; + } + strncpy(t->fmt, fmt, len); + t->fmt[len] = '\0'; + t->flag |= TTAIL_FLAG_FORMAT; + return 0; +} + int ttail_set_dates(ttail_t* res, char* dates[2]) { int c, ret; diff --git a/tests/ttail_argparse_check.c b/tests/ttail_argparse_check.c index 8ece60b..d31f174 100644 --- a/tests/ttail_argparse_check.c +++ b/tests/ttail_argparse_check.c @@ -66,6 +66,12 @@ START_TEST (test_argparse_empty_prefixsz) "ttail_t.prefix_sz should be NULL"); } END_TEST +START_TEST (test_argparse_empty_session) +{ + ck_assert_msg(ttail->session == NULL, + "ttail_t.session should be NULL"); +} +END_TEST /* * Bad arguments @@ -361,6 +367,7 @@ Suite * ttail_init_suite(void) tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt); tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose); tcase_add_test(tc_argparse_empty, test_argparse_empty_prefixsz); + tcase_add_test(tc_argparse_empty, test_argparse_empty_session); tc_argparse_badarg = tcase_create("bad arguments parsing"); tcase_add_test(tc_argparse_badarg, test_argparse_bad1); diff --git a/tests/ttail_init_check.c b/tests/ttail_init_check.c index c2f3117..7f765c9 100644 --- a/tests/ttail_init_check.c +++ b/tests/ttail_init_check.c @@ -202,6 +202,30 @@ START_TEST (test_init_againbad_prefix) } END_TEST +/* + * ttail_set_fmt() checks + */ +START_TEST (test_init_fmt) +{ + int ret; + ret = ttail_set_fmt(ttail, "%B"); + ck_assert_int_eq(ret, 0); + ck_assert_int_eq(ttail->flag & TTAIL_FLAG_FORMAT, TTAIL_FLAG_FORMAT); + ck_assert_str_eq(ttail->fmt, "%B"); +} +END_TEST + +START_TEST (test_init_fmt_again) +{ + int ret; + ttail_set_fmt(ttail, "%B"); + ret = ttail_set_fmt(ttail, "%b"); + ck_assert_int_eq(ret, -1); + ck_assert_int_eq(ttail->flag & TTAIL_FLAG_FORMAT, TTAIL_FLAG_FORMAT); + ck_assert_str_eq(ttail->fmt, "%B"); +} +END_TEST + /* * ttail_format_guess() tests */ @@ -493,8 +517,8 @@ END_TEST Suite * ttail_init_suite(void) { Suite *s; - TCase *tc_init_logfile, *tc_init_prefix ,*tc_init_fmt_guess, \ - *tc_init_set_dates, *tc_init_check; + TCase *tc_init_logfile, *tc_init_prefix , *tc_init_fmt, \ + *tc_init_fmt_guess, *tc_init_set_dates, *tc_init_check; s = suite_create("ttail init checks"); @@ -516,6 +540,12 @@ Suite * ttail_init_suite(void) tcase_add_test(tc_init_prefix, test_init_again_prefix); tcase_add_test(tc_init_prefix, test_init_againbad_prefix); + tc_init_fmt = tcase_create("date format init checks"); + tcase_add_checked_fixture(tc_init_fmt, + setup_ttail_empty, teardown_ttail); + tcase_add_test(tc_init_fmt, test_init_fmt); + tcase_add_test(tc_init_fmt, test_init_fmt_again); + tc_init_fmt_guess = tcase_create("date format guess init checks"); tcase_add_checked_fixture(tc_init_fmt_guess, setup_ttail_empty, teardown_ttail); @@ -544,6 +574,7 @@ Suite * ttail_init_suite(void) suite_add_tcase(s, tc_init_logfile); suite_add_tcase(s, tc_init_prefix); + suite_add_tcase(s, tc_init_fmt); suite_add_tcase(s, tc_init_fmt_guess); suite_add_tcase(s, tc_init_set_dates); suite_add_tcase(s, tc_init_check); diff --git a/tests/ttail_search_check.c b/tests/ttail_search_check.c index 696f076..fc2dfb5 100644 --- a/tests/ttail_search_check.c +++ b/tests/ttail_search_check.c @@ -157,9 +157,7 @@ START_TEST (test_file_minmax1) */ printf("%ld\n", ttail->logfile_sz); ttail->flag |= TTAIL_FLAG_FORMAT; - /* - ttail->fmt = "%b%n%d %H:%M"; - */ + ttail_set_fmt(ttail, "%B%n%d %H:%M"); r = _ttail_search_closest_files_init(ttail); ck_assert_int_eq(r, 0); /*