Browse Source

Changed headers of _ttail_file_(next|start)_line()

Yann Weber 7 years ago
parent
commit
014d5ffb1f
4 changed files with 44 additions and 49 deletions
  1. 6
    6
      src/include/ttail_search_files.h
  2. 15
    9
      src/ttail_search_files.c
  3. 2
    13
      tests/ttail_check.h
  4. 21
    21
      tests/ttail_search_file_line.c

+ 6
- 6
src/include/ttail_search_files.h View File

@@ -160,20 +160,20 @@ inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
160 160
 /**@brief Search next line
161 161
  *
162 162
  *Set f pos to next line begining and return the position
163
- *@param FILE* f 
163
+ *@param ttail_t* t
164
+ *@param size_t id logfile id
164 165
  *@return -1 on error 0 on EOF else return the next line position
165
- *@todo change header to int _ttail_file_next_line(ttail_t, size_t, size_t*)
166 166
  */
167
-inline long _ttail_file_next_line(FILE*);
167
+inline long _ttail_file_next_line(ttail_t*, size_t);
168 168
 
169 169
 /**@brief Search line start
170 170
  *
171 171
  *Set f pos to line begining and return the position
172
- *@param FILE* f 
172
+ *@param ttail_t* t
173
+ *@param size_t id logfile id
173 174
  *@return -1 on error else return the next line position
174
- *@todo change header to int _ttail_file_start_line(ttail_t, size_t, size_t*)
175 175
  */
176
-inline long _ttail_file_start_line(FILE*);
176
+inline long _ttail_file_start_line(ttail_t*, size_t);
177 177
 
178 178
 /**@brief Search last line with a date < tm from EOF
179 179
  *@param ttail ttail_t*

+ 15
- 9
src/ttail_search_files.c View File

@@ -310,8 +310,8 @@ running binary search algorithm in '%s'\n", t->logfile_name[*id]);
310 310
 					return -1;
311 311
 				}
312 312
 				tmpoff = min?
313
-					_ttail_file_start_line(t->logfile[*id]):
314
-					_ttail_file_next_line(t->logfile[*id]);
313
+					_ttail_file_start_line(t, *id):
314
+					_ttail_file_next_line(t, *id);
315 315
 				if(tmpoff < 0)
316 316
 				{
317 317
 					break;
@@ -373,7 +373,7 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
373 373
 		}
374 374
 		if(cur > prev)
375 375
 		{
376
-			cur = _ttail_file_next_line(t->logfile[id]);
376
+			cur = _ttail_file_next_line(t, id);
377 377
 			if(cur < 0)
378 378
 			{
379 379
 				/* not sure errno is really set */
@@ -383,7 +383,7 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
383 383
 		}
384 384
 		else
385 385
 		{
386
-			cur = _ttail_file_start_line(t->logfile[id]);
386
+			cur = _ttail_file_start_line(t, id);
387 387
 			if(cur < 0)
388 388
 			{
389 389
 				/* not sure errno is really set */
@@ -414,7 +414,7 @@ to a date directly from a file\n");
414 414
 		}
415 415
 		else if(cmpres < 0)
416 416
 		{
417
-			tmp = _ttail_file_next_line(t->logfile[id]);
417
+			tmp = _ttail_file_next_line(t,id);
418 418
 			ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
419 419
 			if(cmpres >=0)
420 420
 			{
@@ -572,7 +572,7 @@ inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
572 572
 	}
573 573
 	while(1)
574 574
 	{
575
-		if((cur = _ttail_file_start_line(fp)) < 0)
575
+		if((cur = _ttail_file_start_line(t, id)) < 0)
576 576
 		{
577 577
 			return -1;
578 578
 		}
@@ -612,13 +612,16 @@ int _ttail_file_reopen(ttail_t* t, size_t id)
612 612
 	return t->logfile[id]?0:-1;
613 613
 }
614 614
 
615
-inline long _ttail_file_next_line(FILE* f)
615
+inline long _ttail_file_next_line(ttail_t *t, size_t id)
616 616
 {
617
+	FILE *f;
617 618
 	ssize_t s;
618 619
 	size_t r;
619 620
 	char *buff;
620 621
 	long res;
621 622
 	int c;
623
+
624
+	f = t->logfile[id];
622 625
 	
623 626
 	r=0;
624 627
 	buff = NULL;
@@ -654,14 +657,17 @@ inline long _ttail_file_next_line(FILE* f)
654 657
 	return -1;
655 658
 }
656 659
 
657
-inline long _ttail_file_start_line(FILE* f)
660
+inline long _ttail_file_start_line(ttail_t *ttail, size_t id)
658 661
 {
659 662
 	#define _STARTLN_BUFFLEN 32
663
+	FILE *f;
660 664
 	long res; /* function result */
661 665
 	long read_beg, cur, last, start;
662 666
 	int read_sz;
663 667
 	int c;
664 668
 
669
+	f = ttail->logfile[id];
670
+
665 671
 	if((start = ftell(f)) < 0)
666 672
 	{
667 673
 		perror("Unable to get position in file");
@@ -738,7 +744,7 @@ inline off_t _ttail_file_search_from_end(ttail_t* t , size_t id ,
738 744
 	}
739 745
 	while(1)
740 746
 	{
741
-		last = _ttail_file_start_line(f);
747
+		last = _ttail_file_start_line(t, id);
742 748
 		if(last < 0)
743 749
 		{
744 750
 			break;

+ 2
- 13
tests/ttail_check.h View File

@@ -22,8 +22,6 @@ char *fmt[] = TTAIL_DEFAULT_FORMATS;
22 22
 char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
23 23
 	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
24 24
 off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
25
-FILE *fpl;
26
-
27 25
 
28 26
 void teardown_ttail(void)
29 27
 {
@@ -79,17 +77,8 @@ void teardown_fname(void)
79 77
 
80 78
 void setup_file_line(void)
81 79
 {
82
-	fpl = fopen(samples[0], "r");
83
-	if(!fpl)
84
-	{
85
-		perror("Unable to open file for testing :");
86
-		ck_abort();
87
-	}
88
-}
89
-
90
-void teardown_file_line(void)
91
-{
92
-	fclose(fpl);
80
+	setup_ttail_empty();
81
+	ttail_add_logfile(ttail, samples[0]);
93 82
 }
94 83
 
95 84
 void teardown_closest(void)

+ 21
- 21
tests/ttail_search_file_line.c View File

@@ -13,19 +13,19 @@
13 13
 START_TEST (test_file_line_next)
14 14
 {
15 15
 	long res;
16
-	res = _ttail_file_next_line(fpl);
16
+	res = _ttail_file_next_line(ttail, 0);
17 17
 	ck_assert(res == 77);
18
-	res = _ttail_file_next_line(fpl);
18
+	res = _ttail_file_next_line(ttail, 0);
19 19
 	ck_assert(res == 136);
20
-	res = _ttail_file_next_line(fpl);
20
+	res = _ttail_file_next_line(ttail, 0);
21 21
 	ck_assert(res == 221);
22
-	res = _ttail_file_next_line(fpl);
22
+	res = _ttail_file_next_line(ttail, 0);
23 23
 	ck_assert(res == 298);
24
-	res = _ttail_file_next_line(fpl);
24
+	res = _ttail_file_next_line(ttail, 0);
25 25
 	ck_assert(res == 357);
26
-	res = _ttail_file_next_line(fpl);
26
+	res = _ttail_file_next_line(ttail, 0);
27 27
 	ck_assert(res == 0);
28
-	res = _ttail_file_next_line(fpl);
28
+	res = _ttail_file_next_line(ttail, 0);
29 29
 	ck_assert(res == -1);
30 30
 }
31 31
 END_TEST
@@ -33,31 +33,31 @@ END_TEST
33 33
 START_TEST (test_file_line_start)
34 34
 {
35 35
 	long res;
36
-	fseek(fpl, -1, SEEK_END);
37
-	res = _ttail_file_start_line(fpl);
36
+	fseek(ttail->logfile[0], -1, SEEK_END);
37
+	res = _ttail_file_start_line(ttail, 0);
38 38
 	ck_assert(res == 357);
39
-	res = _ttail_file_start_line(fpl);
39
+	res = _ttail_file_start_line(ttail, 0);
40 40
 	ck_assert(res == 357);
41
-	fseek(fpl, -1, SEEK_CUR);
42
-	res = _ttail_file_start_line(fpl);
41
+	fseek(ttail->logfile[0], -1, SEEK_CUR);
42
+	res = _ttail_file_start_line(ttail, 0);
43 43
 	ck_assert(res == 298);
44
-	fseek(fpl, -1, SEEK_CUR);
45
-	res = _ttail_file_start_line(fpl);
44
+	fseek(ttail->logfile[0], -1, SEEK_CUR);
45
+	res = _ttail_file_start_line(ttail, 0);
46 46
 	ck_assert(res == 221);
47
-	fseek(fpl, -1, SEEK_CUR);
48
-	res = _ttail_file_start_line(fpl);
47
+	fseek(ttail->logfile[0], -1, SEEK_CUR);
48
+	res = _ttail_file_start_line(ttail, 0);
49 49
 	ck_assert(res == 136);
50
-	fseek(fpl, -1, SEEK_CUR);
51
-	res = _ttail_file_start_line(fpl);
50
+	fseek(ttail->logfile[0], -1, SEEK_CUR);
51
+	res = _ttail_file_start_line(ttail, 0);
52 52
 	ck_assert(res == 77);
53
-	fseek(fpl, -1, SEEK_CUR);
54
-	res = _ttail_file_start_line(fpl);
53
+	fseek(ttail->logfile[0], -1, SEEK_CUR);
54
+	res = _ttail_file_start_line(ttail, 0);
55 55
 	ck_assert(res == 0);
56 56
 }
57 57
 END_TEST
58 58
 
59 59
 TTAIL_CHECK_START("ttail search_files checks", "ttail_file_*line*() checks")
60
-	TTAIL_SET_FIXTURE(setup_file_line, teardown_file_line);
60
+	TTAIL_SET_FIXTURE(setup_file_line, teardown_ttail);
61 61
 	TTAIL_ADD_TEST(test_file_line_next);
62 62
 	TTAIL_ADD_TEST(test_file_line_start);
63 63
 TTAIL_CHECK_END

Loading…
Cancel
Save