Browse Source

ttail_format_guess() do not take ttail_t* as argument

Making this function usable by logfiles and stdin format autodetection
Yann Weber 7 years ago
parent
commit
fe382fff5f
3 changed files with 20 additions and 59 deletions
  1. 2
    3
      src/include/ttail_init.h
  2. 12
    22
      src/ttail_init.c
  3. 6
    34
      tests/ttail_init_format_guess.c

+ 2
- 3
src/include/ttail_init.h View File

@@ -303,12 +303,11 @@ int _ttail_set_date_relative(ttail_t*, char*, int);
303 303
 int _ttail_norm_dates(ttail_t*);
304 304
 
305 305
 /**@brief Attempt to guess a dateformat
306
- *@param ttail_t* ttail if manage to guess set the ttail_t.fmt
307 306
  *@param const char* date as a dtring
308 307
  *@param struct tm* if non NULL will be set to detected date
309
- *@return -1 if no guess -2 if fmt already set else id in TTAIL_DEFAULT_FORMATS
308
+ *@return -1 if no guess else id in TTAIL_DEFAULT_FORMATS
310 309
  */
311
-int ttail_format_guess(ttail_t*, const char*, struct tm*);
310
+int ttail_format_guess(const char*, struct tm*);
312 311
 
313 312
 
314 313
 void ttail_free(ttail_t*);

+ 12
- 22
src/ttail_init.c View File

@@ -443,11 +443,12 @@ int _ttail_set_date_fmt(ttail_t* res, char* date, int c)
443 443
 {
444 444
 	int ret;
445 445
 	char *endp;
446
+	char *fmt[] = TTAIL_DEFAULT_FORMATS;
446 447
 
447 448
 	if(!(res->flag & TTAIL_FLAG_FORMAT))
448 449
 	{
449 450
 		/* no format specified */
450
-		ret = ttail_format_guess(res, date,
451
+		ret = ttail_format_guess(date,
451 452
 			c==0?&(res->date_min):&(res->date_max));
452 453
 		if(ret < 0)
453 454
 		{
@@ -455,6 +456,15 @@ int _ttail_set_date_fmt(ttail_t* res, char* date, int c)
455 456
 date '%s'\n", date);
456 457
 			return -1;
457 458
 		}
459
+		res->fmt = malloc(sizeof(char)*(strlen(fmt[ret])+1));
460
+		if(!res->fmt)
461
+		{
462
+			perror("Unable to allocate memory for date format");
463
+			res->flag ^= TTAIL_FLAG_FORMAT;
464
+		}
465
+		strcpy(res->fmt, fmt[ret]);
466
+		res->flag |= TTAIL_FLAG_FORMAT;
467
+
458 468
 		res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
459 469
 		return 0;
460 470
 	}
@@ -650,7 +660,7 @@ we just strftime() !\n");
650 660
 	return 0;
651 661
 }
652 662
 
653
-int ttail_format_guess(ttail_t* t, const char* date_str, struct tm* tm)
663
+int ttail_format_guess(const char* date_str, struct tm* tm)
654 664
 {
655 665
 	int i, res;
656 666
 	char *res_ret, *ret;
@@ -659,11 +669,6 @@ int ttail_format_guess(ttail_t* t, const char* date_str, struct tm* tm)
659 669
 	
660 670
 	memset(&dte, 0, sizeof(struct tm));
661 671
 
662
-	if(t->flag & TTAIL_FLAG_FORMAT)
663
-	{
664
-		fprintf(stderr, "Format allready set\n");
665
-		return -2;
666
-	}
667 672
 	res = -1;
668 673
 	res_ret = NULL;
669 674
 	i=0;
@@ -680,21 +685,6 @@ int ttail_format_guess(ttail_t* t, const char* date_str, struct tm* tm)
680 685
 				{
681 686
 					memcpy(tm, &dte, sizeof(struct tm));
682 687
 				}
683
-				if(t->fmt)
684
-				{
685
-					free(t->fmt);
686
-				}
687
-				t->fmt = malloc(
688
-					sizeof(char)*(strlen(fmt[i])+1));
689
-				if(!t->fmt)
690
-				{
691
-					perror("Unable to allocate memory\
692
-for date format");
693
-					t->flag ^= TTAIL_FLAG_FORMAT;
694
-					return -1;
695
-				}
696
-				strcpy(t->fmt, fmt[i]);
697
-				t->flag |= TTAIL_FLAG_FORMAT;
698 688
 			}
699 689
 		}
700 690
 		i++;

+ 6
- 34
tests/ttail_init_format_guess.c View File

@@ -15,12 +15,9 @@ START_TEST (test_init_guess_date)
15 15
 	int ret;
16 16
 	struct tm tm;
17 17
 	char res[] = "%Y/%m/%d";
18
-	ret = ttail_format_guess(ttail, "1988/10/22", &tm);
18
+	ret = ttail_format_guess("1988/10/22", &tm);
19 19
 	ck_assert_int_ne(ret, -1);
20 20
 	ck_assert_str_eq(fmt[ret], res);
21
-	ck_assert(ttail->fmt != NULL);
22
-	ck_assert_str_eq(ttail->fmt, res);
23
-	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
24 21
 	ck_assert_int_eq(tm.tm_year, 88);
25 22
 	ck_assert_int_eq(tm.tm_mon, 9);
26 23
 	ck_assert_int_eq(tm.tm_mday, 22);
@@ -35,12 +32,9 @@ START_TEST (test_init_guess_datetime)
35 32
 	int ret;
36 33
 	struct tm tm;
37 34
 	char res[] = "%Y/%m/%d:%H:%M";
38
-	ret = ttail_format_guess(ttail, "1988/10/22:22:10:00", &tm);
35
+	ret = ttail_format_guess("1988/10/22:22:10:00", &tm);
39 36
 	ck_assert_int_ne(ret, -1);
40 37
 	ck_assert_str_eq(fmt[ret], res);
41
-	ck_assert(ttail->fmt != NULL);
42
-	ck_assert_str_eq(ttail->fmt, res);
43
-	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
44 38
 	ck_assert_int_eq(tm.tm_year, 88);
45 39
 	ck_assert_int_eq(tm.tm_mon, 9);
46 40
 	ck_assert_int_eq(tm.tm_mday, 22);
@@ -55,12 +49,9 @@ START_TEST (test_init_guess_datetime2)
55 49
 	int ret;
56 50
 	struct tm tm;
57 51
 	char res[] = "%B%n%d %H:%M:%S";
58
-	ret = ttail_format_guess(ttail, "Mar  6 00:01:39 pilgrim dhclient", &tm);
52
+	ret = ttail_format_guess("Mar  6 00:01:39 pilgrim dhclient", &tm);
59 53
 	ck_assert_int_ne(ret, -1);
60 54
 	ck_assert_str_eq(fmt[ret], res);
61
-	ck_assert(ttail->fmt != NULL);
62
-	ck_assert_str_eq(ttail->fmt, res);
63
-	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
64 55
 	ck_assert_int_eq(tm.tm_year, 0);
65 56
 	ck_assert_int_eq(tm.tm_mon, 2);
66 57
 	ck_assert_int_eq(tm.tm_mday, 6);
@@ -74,10 +65,8 @@ START_TEST (test_init_guess_noguess)
74 65
 {
75 66
 	int ret;
76 67
 	struct tm tm;
77
-	ret = ttail_format_guess(ttail, "notadate", &tm);
68
+	ret = ttail_format_guess("notadate", &tm);
78 69
 	ck_assert_int_eq(ret, -1);
79
-	ck_assert(ttail->fmt == NULL);
80
-	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == 0);
81 70
 	ck_assert_int_eq(tm.tm_year, 0);
82 71
 	ck_assert_int_eq(tm.tm_mon, 0);
83 72
 	ck_assert_int_eq(tm.tm_mday, 0);
@@ -91,12 +80,9 @@ START_TEST (test_init_guess_date_notm)
91 80
 {
92 81
 	int ret;
93 82
 	char res[] = "%Y/%m/%d";
94
-	ret = ttail_format_guess(ttail, "1988/10/22", NULL);
83
+	ret = ttail_format_guess("1988/10/22", NULL);
95 84
 	ck_assert_int_ne(ret, -1);
96 85
 	ck_assert_str_eq(fmt[ret], res);
97
-	ck_assert(ttail->fmt != NULL);
98
-	ck_assert_str_eq(ttail->fmt, res);
99
-	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
100 86
 }
101 87
 END_TEST
102 88
 
@@ -104,26 +90,13 @@ START_TEST (test_init_guess_noguess_notm)
104 90
 {
105 91
 	int ret;
106 92
 	struct tm tm;
107
-	ret = ttail_format_guess(ttail, "notadate", &tm);
93
+	ret = ttail_format_guess("notadate", &tm);
108 94
 	ck_assert_int_eq(ret, -1);
109 95
 	ck_assert(ttail->fmt == NULL);
110 96
 	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == 0);
111 97
 }
112 98
 END_TEST
113 99
 
114
-START_TEST (test_init_guess_fmt_set)
115
-{
116
-	int ret;
117
-	char res[] = "%Y/%m/%d";
118
-	ret = ttail_format_guess(ttail, "1988/10/22", NULL);
119
-	ck_assert_str_eq(fmt[ret], res);
120
-	ret = ttail_format_guess(ttail, "1988/10/22", NULL);
121
-	ck_assert_int_eq(ret, -2);
122
-	ck_assert_str_eq(ttail->fmt, res);
123
-	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
124
-}
125
-END_TEST
126
-
127 100
 TTAIL_CHECK_START("ttail init checks", "date format guess init checks")
128 101
 	TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail);
129 102
 	TTAIL_ADD_TEST(test_init_guess_date);
@@ -132,6 +105,5 @@ TTAIL_CHECK_START("ttail init checks", "date format guess init checks")
132 105
 	TTAIL_ADD_TEST(test_init_guess_noguess);
133 106
 	TTAIL_ADD_TEST(test_init_guess_date_notm);
134 107
 	TTAIL_ADD_TEST(test_init_guess_noguess_notm);
135
-	TTAIL_ADD_TEST(test_init_guess_fmt_set);
136 108
 TTAIL_CHECK_END
137 109
 

Loading…
Cancel
Save