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
 /**@brief Search next line
160
 /**@brief Search next line
161
  *
161
  *
162
  *Set f pos to next line begining and return the position
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
  *@return -1 on error 0 on EOF else return the next line position
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
 /**@brief Search line start
169
 /**@brief Search line start
170
  *
170
  *
171
  *Set f pos to line begining and return the position
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
  *@return -1 on error else return the next line position
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
 /**@brief Search last line with a date < tm from EOF
178
 /**@brief Search last line with a date < tm from EOF
179
  *@param ttail ttail_t*
179
  *@param ttail ttail_t*

+ 15
- 9
src/ttail_search_files.c View File

310
 					return -1;
310
 					return -1;
311
 				}
311
 				}
312
 				tmpoff = min?
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
 				if(tmpoff < 0)
315
 				if(tmpoff < 0)
316
 				{
316
 				{
317
 					break;
317
 					break;
373
 		}
373
 		}
374
 		if(cur > prev)
374
 		if(cur > prev)
375
 		{
375
 		{
376
-			cur = _ttail_file_next_line(t->logfile[id]);
376
+			cur = _ttail_file_next_line(t, id);
377
 			if(cur < 0)
377
 			if(cur < 0)
378
 			{
378
 			{
379
 				/* not sure errno is really set */
379
 				/* not sure errno is really set */
383
 		}
383
 		}
384
 		else
384
 		else
385
 		{
385
 		{
386
-			cur = _ttail_file_start_line(t->logfile[id]);
386
+			cur = _ttail_file_start_line(t, id);
387
 			if(cur < 0)
387
 			if(cur < 0)
388
 			{
388
 			{
389
 				/* not sure errno is really set */
389
 				/* not sure errno is really set */
414
 		}
414
 		}
415
 		else if(cmpres < 0)
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
 			ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
418
 			ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
419
 			if(cmpres >=0)
419
 			if(cmpres >=0)
420
 			{
420
 			{
572
 	}
572
 	}
573
 	while(1)
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
 			return -1;
577
 			return -1;
578
 		}
578
 		}
612
 	return t->logfile[id]?0:-1;
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
 	ssize_t s;
618
 	ssize_t s;
618
 	size_t r;
619
 	size_t r;
619
 	char *buff;
620
 	char *buff;
620
 	long res;
621
 	long res;
621
 	int c;
622
 	int c;
623
+
624
+	f = t->logfile[id];
622
 	
625
 	
623
 	r=0;
626
 	r=0;
624
 	buff = NULL;
627
 	buff = NULL;
654
 	return -1;
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
 	#define _STARTLN_BUFFLEN 32
662
 	#define _STARTLN_BUFFLEN 32
663
+	FILE *f;
660
 	long res; /* function result */
664
 	long res; /* function result */
661
 	long read_beg, cur, last, start;
665
 	long read_beg, cur, last, start;
662
 	int read_sz;
666
 	int read_sz;
663
 	int c;
667
 	int c;
664
 
668
 
669
+	f = ttail->logfile[id];
670
+
665
 	if((start = ftell(f)) < 0)
671
 	if((start = ftell(f)) < 0)
666
 	{
672
 	{
667
 		perror("Unable to get position in file");
673
 		perror("Unable to get position in file");
738
 	}
744
 	}
739
 	while(1)
745
 	while(1)
740
 	{
746
 	{
741
-		last = _ttail_file_start_line(f);
747
+		last = _ttail_file_start_line(t, id);
742
 		if(last < 0)
748
 		if(last < 0)
743
 		{
749
 		{
744
 			break;
750
 			break;

+ 2
- 13
tests/ttail_check.h View File

22
 char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
22
 char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
23
 	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
23
 	"./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
24
 off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
24
 off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
25
-FILE *fpl;
26
-
27
 
25
 
28
 void teardown_ttail(void)
26
 void teardown_ttail(void)
29
 {
27
 {
79
 
77
 
80
 void setup_file_line(void)
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
 void teardown_closest(void)
84
 void teardown_closest(void)

+ 21
- 21
tests/ttail_search_file_line.c View File

13
 START_TEST (test_file_line_next)
13
 START_TEST (test_file_line_next)
14
 {
14
 {
15
 	long res;
15
 	long res;
16
-	res = _ttail_file_next_line(fpl);
16
+	res = _ttail_file_next_line(ttail, 0);
17
 	ck_assert(res == 77);
17
 	ck_assert(res == 77);
18
-	res = _ttail_file_next_line(fpl);
18
+	res = _ttail_file_next_line(ttail, 0);
19
 	ck_assert(res == 136);
19
 	ck_assert(res == 136);
20
-	res = _ttail_file_next_line(fpl);
20
+	res = _ttail_file_next_line(ttail, 0);
21
 	ck_assert(res == 221);
21
 	ck_assert(res == 221);
22
-	res = _ttail_file_next_line(fpl);
22
+	res = _ttail_file_next_line(ttail, 0);
23
 	ck_assert(res == 298);
23
 	ck_assert(res == 298);
24
-	res = _ttail_file_next_line(fpl);
24
+	res = _ttail_file_next_line(ttail, 0);
25
 	ck_assert(res == 357);
25
 	ck_assert(res == 357);
26
-	res = _ttail_file_next_line(fpl);
26
+	res = _ttail_file_next_line(ttail, 0);
27
 	ck_assert(res == 0);
27
 	ck_assert(res == 0);
28
-	res = _ttail_file_next_line(fpl);
28
+	res = _ttail_file_next_line(ttail, 0);
29
 	ck_assert(res == -1);
29
 	ck_assert(res == -1);
30
 }
30
 }
31
 END_TEST
31
 END_TEST
33
 START_TEST (test_file_line_start)
33
 START_TEST (test_file_line_start)
34
 {
34
 {
35
 	long res;
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
 	ck_assert(res == 357);
38
 	ck_assert(res == 357);
39
-	res = _ttail_file_start_line(fpl);
39
+	res = _ttail_file_start_line(ttail, 0);
40
 	ck_assert(res == 357);
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
 	ck_assert(res == 298);
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
 	ck_assert(res == 221);
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
 	ck_assert(res == 136);
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
 	ck_assert(res == 77);
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
 	ck_assert(res == 0);
55
 	ck_assert(res == 0);
56
 }
56
 }
57
 END_TEST
57
 END_TEST
58
 
58
 
59
 TTAIL_CHECK_START("ttail search_files checks", "ttail_file_*line*() checks")
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
 	TTAIL_ADD_TEST(test_file_line_next);
61
 	TTAIL_ADD_TEST(test_file_line_next);
62
 	TTAIL_ADD_TEST(test_file_line_start);
62
 	TTAIL_ADD_TEST(test_file_line_start);
63
 TTAIL_CHECK_END
63
 TTAIL_CHECK_END

Loading…
Cancel
Save