Browse Source

Implement & use a ttail_set_fmt() + tests

Yann Weber 7 years ago
parent
commit
a4c65684bf
5 changed files with 187 additions and 12 deletions
  1. 125
    0
      src/include/ttail_init.h
  2. 21
    7
      src/ttail_init.c
  3. 7
    0
      tests/ttail_argparse_check.c
  4. 33
    2
      tests/ttail_init_check.c
  5. 1
    3
      tests/ttail_search_check.c

+ 125
- 0
src/include/ttail_init.h View File

@@ -55,6 +55,131 @@ int ttail_add_logfile(ttail_t*, const char*);
55 55
  */
56 56
 int ttail_set_prefix(ttail_t*, const char*);
57 57
 
58
+/**@brief Set a date format
59
+ *
60
+ * The supported input field descriptors are listed below.  In case a text string (such as the name
61
+ * of a day of the week or a month name) is to be matched, the comparison is case insensitive.   In
62
+ * case a number is to be matched, leading zeros are permitted but not required.
63
+ * 
64
+ * \%%     The % character.
65
+ * 
66
+ * \%a or %A
67
+ * The  name  of the day of the week according to the current locale, in abbreviated form or
68
+ * the full name.
69
+ * 
70
+ * \%b or %B or %h
71
+ * The month name according to the current locale, in abbreviated form or the full name.
72
+ * 
73
+ * \%c     The date and time representation for the current locale.
74
+ * 
75
+ * \%C     The century number (0-99).
76
+ * 
77
+ * \%d or %e
78
+ * The day of month (1-31).
79
+ * 
80
+ * \%D     Equivalent to %m/%d/%y.  (This is the American style date, very confusing  to  non-Ameri‐
81
+ * cans,  especially  since %d/%m/%y is widely used in Europe.  The ISO 8601 standard format
82
+ * is %Y-%m-%d.)
83
+ * 
84
+ * \%H     The hour (0-23).
85
+ * 
86
+ * \%I     The hour on a 12-hour clock (1-12).
87
+ * 
88
+ * \%j     The day number in the year (1-366).
89
+ * 
90
+ * \%m     The month number (1-12).
91
+ * 
92
+ * \%M     The minute (0-59).
93
+ * 
94
+ * \%n     Arbitrary whitespace.
95
+ * 
96
+ * \%p     The locale's equivalent of AM or PM.  (Note: there may be none.)
97
+ * 
98
+ * \%r     The 12-hour clock time (using the locale's AM or PM).  In the POSIX locale equivalent  to
99
+ * \%I:%M:%S  %p.  If t_fmt_ampm is empty in the LC_TIME part of the current locale, then the
100
+ * behavior is undefined.
101
+ * 
102
+ * \%R     Equivalent to %H:%M.
103
+ * 
104
+ * \%S     The second (0-60; 60 may occur for leap seconds; earlier also 61 was allowed).
105
+ * 
106
+ * \%t     Arbitrary whitespace.
107
+ * 
108
+ * \%T     Equivalent to %H:%M:%S.
109
+ * 
110
+ * \%U     The week number with Sunday the first day of the week (0-53).  The first Sunday of  Janu‐
111
+ * ary is the first day of week 1.
112
+ * 
113
+ * \%w     The ordinal number of the day of the week (0-6), with Sunday = 0.
114
+ * 
115
+ * \%W     The  week number with Monday the first day of the week (0-53).  The first Monday of Janu‐
116
+ * ary is the first day of week 1.
117
+ * 
118
+ * \%x     The date, using the locale's date format.
119
+ * 
120
+ * \%X     The time, using the locale's time format.
121
+ * 
122
+ * \%y     The year within century (0-99).  When a century is not otherwise specified, values in the
123
+ * range  69-99  refer  to  years  in the twentieth century (1969-1999); values in the range
124
+ * 00-68 refer to years in the twenty-first century (2000-2068).
125
+ * 
126
+ * \%Y     The year, including century (for example, 1991).
127
+ * 
128
+ * Some field descriptors can be modified by the E or O modifier characters  to  indicate  that  an
129
+ * alternative  format or specification should be used.  If the alternative format or specification
130
+ * does not exist in the current locale, the unmodified field descriptor is used.
131
+ * 
132
+ * 
133
+ * The E modifier specifies that the input string may contain alternative locale-dependent versions
134
+ * of the date and time representation:
135
+ * 
136
+ * \%Ec    The locale's alternative date and time representation.
137
+ * 
138
+ * \%EC    The name of the base year (period) in the locale's alternative representation.
139
+ * 
140
+ * \%Ex    The locale's alternative date representation.
141
+ * 
142
+ * \%EX    The locale's alternative time representation.
143
+ * 
144
+ * \%Ey    The offset from %EC (year only) in the locale's alternative representation.
145
+ * 
146
+ * \%EY    The full alternative year representation.
147
+ * 
148
+ * The O modifier specifies that the numerical input may be in an alternative locale-dependent for‐
149
+ * mat:
150
+ * 
151
+ * \%Od or %Oe
152
+ * The day of the month using the locale's alternative numeric symbols;  leading  zeros  are
153
+ * permitted but not required.
154
+ * 
155
+ * \%OH    The hour (24-hour clock) using the locale's alternative numeric symbols.
156
+ * 
157
+ * \%OI    The hour (12-hour clock) using the locale's alternative numeric symbols.
158
+ * 
159
+ * \%Om    The month using the locale's alternative numeric symbols.
160
+ * 
161
+ * \%OM    The minutes using the locale's alternative numeric symbols.
162
+ * 
163
+ * \%OS    The seconds using the locale's alternative numeric symbols.
164
+ * 
165
+ * \%OU    The  week  number  of  the  year (Sunday as the first day of the week) using the locale's
166
+ * alternative numeric symbols.
167
+ * 
168
+ * \%Ow    The ordinal number of the day of the week (Sunday=0),
169
+ * using the locale's alternative numeric symbols.
170
+ * 
171
+ * \%OW    The week number of the year (Monday as the first day of  the  week)  using  the  locale's
172
+ * alternative numeric symbols.
173
+ * 
174
+ * \%Oy    The year (offset from %C) using the locale's alternative numeric symbols.
175
+ *
176
+ *
177
+ *@param ttail_t*
178
+ *@param const char * format
179
+ *@return 0 if no errors 1 if allready set -1 if compilation fails
180
+ */
181
+int ttail_set_fmt(ttail_t*, const char*);
182
+
58 183
 /**@brief Set dates min/max
59 184
  *
60 185
  *After the call dates are free and set to NULL except if error

+ 21
- 7
src/ttail_init.c View File

@@ -23,6 +23,7 @@ ttail_t *ttail_init(int argc, char **argv)
23 23
 	res->logfile_name = NULL;
24 24
 	res->logfile_sz = 0;
25 25
 	res->prefix_sz = -1;
26
+	res->session = NULL;
26 27
 	memset(&(res->date_min), 0, sizeof(struct tm));
27 28
 	memset(&(res->date_max), 0, sizeof(struct tm));
28 29
 
@@ -80,16 +81,10 @@ have to be set BEFORE -r option\n");
80 81
 given\n");
81 82
 					goto ttail_init_err;
82 83
 				}
83
-				res->fmt = malloc(
84
-					sizeof(char)*(strlen(optarg)+1));
85
-				if(!res->fmt)
84
+				if(ttail_set_fmt(res, optarg) < 0)
86 85
 				{
87
-					perror("Unable to allocate memory\
88
-for date format");
89 86
 					goto ttail_init_err;
90 87
 				}
91
-				strcpy(res->fmt, optarg);
92
-				res->flag |= TTAIL_FLAG_FORMAT;
93 88
 
94 89
 				break;
95 90
 			case 'l':
@@ -279,6 +274,25 @@ error message");
279 274
 	return -1;
280 275
 }
281 276
 
277
+int ttail_set_fmt(ttail_t* t, const char* fmt)
278
+{
279
+	size_t len;
280
+	if(t->flag & TTAIL_FLAG_FORMAT)
281
+	{
282
+		return -1;
283
+	}
284
+	len = strlen(fmt);
285
+	t->fmt = malloc(sizeof(char)*(len+1));
286
+	if(!t->fmt)
287
+	{
288
+		return -1;
289
+	}
290
+	strncpy(t->fmt, fmt, len);
291
+	t->fmt[len] = '\0';
292
+	t->flag |= TTAIL_FLAG_FORMAT;
293
+	return 0;
294
+}
295
+
282 296
 int ttail_set_dates(ttail_t* res, char* dates[2])
283 297
 {
284 298
 	int c, ret;

+ 7
- 0
tests/ttail_argparse_check.c View File

@@ -66,6 +66,12 @@ START_TEST (test_argparse_empty_prefixsz)
66 66
 		"ttail_t.prefix_sz should be NULL");
67 67
 }
68 68
 END_TEST
69
+START_TEST (test_argparse_empty_session)
70
+{
71
+	ck_assert_msg(ttail->session == NULL,
72
+		"ttail_t.session should be NULL");
73
+}
74
+END_TEST
69 75
 
70 76
 /*
71 77
  * Bad arguments
@@ -361,6 +367,7 @@ Suite * ttail_init_suite(void)
361 367
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt);
362 368
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose);
363 369
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_prefixsz);
370
+	tcase_add_test(tc_argparse_empty, test_argparse_empty_session);
364 371
 
365 372
 	tc_argparse_badarg = tcase_create("bad arguments parsing");
366 373
 	tcase_add_test(tc_argparse_badarg, test_argparse_bad1);

+ 33
- 2
tests/ttail_init_check.c View File

@@ -202,6 +202,30 @@ START_TEST (test_init_againbad_prefix)
202 202
 }
203 203
 END_TEST
204 204
 
205
+/*
206
+ * ttail_set_fmt() checks
207
+ */
208
+START_TEST (test_init_fmt)
209
+{
210
+	int ret;
211
+	ret = ttail_set_fmt(ttail, "%B");
212
+	ck_assert_int_eq(ret, 0);
213
+	ck_assert_int_eq(ttail->flag & TTAIL_FLAG_FORMAT, TTAIL_FLAG_FORMAT);
214
+	ck_assert_str_eq(ttail->fmt, "%B");
215
+}
216
+END_TEST
217
+
218
+START_TEST (test_init_fmt_again)
219
+{
220
+	int ret;
221
+	ttail_set_fmt(ttail, "%B");
222
+	ret = ttail_set_fmt(ttail, "%b");
223
+	ck_assert_int_eq(ret, -1);
224
+	ck_assert_int_eq(ttail->flag & TTAIL_FLAG_FORMAT, TTAIL_FLAG_FORMAT);
225
+	ck_assert_str_eq(ttail->fmt, "%B");
226
+}
227
+END_TEST
228
+
205 229
 /*
206 230
  * ttail_format_guess() tests
207 231
  */
@@ -493,8 +517,8 @@ END_TEST
493 517
 Suite * ttail_init_suite(void)
494 518
 {
495 519
 	Suite *s;
496
-	TCase *tc_init_logfile, *tc_init_prefix ,*tc_init_fmt_guess, \
497
-		*tc_init_set_dates, *tc_init_check;
520
+	TCase *tc_init_logfile, *tc_init_prefix , *tc_init_fmt, \
521
+		*tc_init_fmt_guess, *tc_init_set_dates, *tc_init_check;
498 522
 
499 523
 	s = suite_create("ttail init checks");
500 524
 
@@ -516,6 +540,12 @@ Suite * ttail_init_suite(void)
516 540
 	tcase_add_test(tc_init_prefix, test_init_again_prefix);
517 541
 	tcase_add_test(tc_init_prefix, test_init_againbad_prefix);
518 542
 
543
+	tc_init_fmt = tcase_create("date format init checks");
544
+	tcase_add_checked_fixture(tc_init_fmt,
545
+		setup_ttail_empty, teardown_ttail);
546
+	tcase_add_test(tc_init_fmt, test_init_fmt);
547
+	tcase_add_test(tc_init_fmt, test_init_fmt_again);
548
+
519 549
 	tc_init_fmt_guess = tcase_create("date format guess init checks");
520 550
 	tcase_add_checked_fixture(tc_init_fmt_guess,
521 551
 		setup_ttail_empty, teardown_ttail);
@@ -544,6 +574,7 @@ Suite * ttail_init_suite(void)
544 574
 
545 575
 	suite_add_tcase(s, tc_init_logfile);
546 576
 	suite_add_tcase(s, tc_init_prefix);
577
+	suite_add_tcase(s, tc_init_fmt);
547 578
 	suite_add_tcase(s, tc_init_fmt_guess);
548 579
 	suite_add_tcase(s, tc_init_set_dates);
549 580
 	suite_add_tcase(s, tc_init_check);

+ 1
- 3
tests/ttail_search_check.c View File

@@ -157,9 +157,7 @@ START_TEST (test_file_minmax1)
157 157
 	*/
158 158
 	printf("%ld\n", ttail->logfile_sz);
159 159
 	ttail->flag |= TTAIL_FLAG_FORMAT;
160
-	/*
161
-	ttail->fmt = "%b%n%d %H:%M";
162
-	*/
160
+	ttail_set_fmt(ttail, "%B%n%d %H:%M");
163 161
 	r = _ttail_search_closest_files_init(ttail);
164 162
 	ck_assert_int_eq(r, 0);
165 163
 	/*

Loading…
Cancel
Save