Browse Source

Implemented 2 utility functions + tests

Yann Weber 7 years ago
parent
commit
b74ee6957f
3 changed files with 206 additions and 1 deletions
  1. 24
    0
      src/include/ttail_search_files.h
  2. 112
    0
      src/ttail_search_files.c
  3. 70
    1
      tests/ttail_search_check.c

+ 24
- 0
src/include/ttail_search_files.h View File

@@ -41,6 +41,14 @@ int _ttail_search_closest_files(ttail_t*);
41 41
 int _ttail_search_closest_files_init(ttail_t*);
42 42
 int _ttail_search_closest_files_set_fsizes(ttail_t*);
43 43
 
44
+/**@brief Test if files are sorted
45
+ *
46
+ *If not sorted attempt to sort them
47
+ *@param ttail_t*
48
+ *@return -1 not sorted 0 sorted
49
+ */
50
+int _ttail_search_file_sorted(ttail_t*);
51
+
44 52
 /**@brief Attempt to reopen a file
45 53
  *@param ttail_t* ttail
46 54
  *@param size_t id file id in ttail
@@ -49,6 +57,22 @@ int _ttail_search_closest_files_set_fsizes(ttail_t*);
49 57
  */
50 58
 int _ttail_file_reopen(ttail_t*, size_t);
51 59
 
60
+/**@brief Search next line
61
+ *
62
+ *Set f pos to next line begining and return the position
63
+ *@param FILE* f 
64
+ *@return -1 on error 0 on EOF else return the next line position
65
+ */
66
+long _ttail_file_next_line(FILE*);
67
+
68
+/**@brief Search line start
69
+ *
70
+ *Set f pos to line begining and return the position
71
+ *@param FILE* f 
72
+ *@return -1 on error else return the next line position
73
+ */
74
+long _ttail_file_start_line(FILE*, const off_t);
75
+
52 76
 /**@brief Free the ttail_search_file_t session
53 77
  *@param ttail_t* ttail
54 78
  */

+ 112
- 0
src/ttail_search_files.c View File

@@ -8,6 +8,7 @@ int _ttail_search_closest_files(ttail_t* t)
8 8
 	{
9 9
 		return ret;
10 10
 	}
11
+	/* Checking that files are sorted well */
11 12
 
12 13
 	return 0;
13 14
 }
@@ -102,6 +103,11 @@ int _ttail_search_closest_files_set_fsizes(ttail_t* t)
102 103
 	return 0;
103 104
 }
104 105
 
106
+int _ttail_search_file_sorted(ttail_t* t)
107
+{
108
+	return -1;
109
+}
110
+
105 111
 int _ttail_file_reopen(ttail_t* t, size_t id)
106 112
 {
107 113
 	if(t->logfile[id])
@@ -117,6 +123,112 @@ int _ttail_file_reopen(ttail_t* t, size_t id)
117 123
 	return t->logfile[id]?0:-1;
118 124
 }
119 125
 
126
+long _ttail_file_next_line(FILE* f)
127
+{
128
+	ssize_t s;
129
+	size_t r;
130
+	char *buff;
131
+	long res;
132
+	int c;
133
+	
134
+	r=0;
135
+	buff = NULL;
136
+	s = getline(&buff, &r, f);
137
+	if(s == -1)
138
+	{
139
+		goto _ttail_file_next_line_err;
140
+	}
141
+	while(1)
142
+	{
143
+		c = getc(f);
144
+		if(c == EOF)
145
+		{
146
+			return 0;
147
+		}
148
+		else if(c!='\n')
149
+		{
150
+			if(fseek(f, -1, SEEK_CUR)<0)
151
+			{
152
+				goto _ttail_file_next_line_err;
153
+			}
154
+			break;
155
+		}
156
+	}
157
+
158
+	res = ftell(f);
159
+	free(buff);
160
+	return res;
161
+
162
+	_ttail_file_next_line_err:
163
+	free(buff);
164
+	return -1;
165
+}
166
+
167
+long _ttail_file_start_line(FILE* f, const off_t sz)
168
+{
169
+	#define _STARTLN_BUFFLEN 32
170
+	long res; /* function result */
171
+	long read_beg, cur, last, start;
172
+	char buff[_STARTLN_BUFFLEN];
173
+	char *tmp;
174
+	int read_sz, read_len;
175
+	int c;
176
+
177
+	if((start = ftell(f)) < 0)
178
+	{
179
+		return -1;
180
+	}
181
+	res = 0;
182
+	read_beg = start;
183
+	while(!res && start)
184
+	{
185
+		if(fseek(f, read_beg, SEEK_SET) < 0)
186
+		{
187
+			return -1;
188
+		}
189
+		start = read_beg;
190
+		read_sz = start <= _STARTLN_BUFFLEN?start:_STARTLN_BUFFLEN;
191
+		read_beg = start - read_sz;
192
+		if(fseek(f, read_beg, SEEK_SET) < 0)
193
+		{
194
+			return -1;
195
+		}
196
+		last = -1; /* last pos we saw a '\n' */
197
+		cur = read_beg;
198
+		while(cur < start)
199
+		{
200
+			c = getc(f);
201
+			if(c == EOF)
202
+			{
203
+				if(!res)
204
+				{
205
+					return 0;
206
+				}
207
+				break;
208
+			}
209
+			else if (c =='\n')
210
+			{
211
+				last = cur;
212
+			}
213
+			else if(last >= 0)
214
+			{
215
+				res = cur;
216
+				last = -1;
217
+			}
218
+			cur++;
219
+		}
220
+		if(!read_beg)
221
+		{
222
+			break;
223
+		}
224
+	}
225
+	if(fseek(f, res, SEEK_SET) < 0)
226
+	{
227
+		return -1;
228
+	}
229
+	return res;
230
+}
231
+
120 232
 void _ttail_search_file_free(ttail_t* t)
121 233
 {
122 234
 	if(!t->session)

+ 70
- 1
tests/ttail_search_check.c View File

@@ -1,4 +1,5 @@
1 1
 #include <check.h>
2
+#include <stdio.h>
2 3
 #include <libgen.h>
3 4
 
4 5
 #include "ttail.h"
@@ -10,6 +11,22 @@ ttail_t *ttail;
10 11
 char *samples[5] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
11 12
 	"./samples/4.log", "./samples/5.log"};
12 13
 off_t samples_sz[5] = { 442, 0, 893, 2587, 2310 };
14
+FILE *fpl;
15
+
16
+void setup_file_line(void)
17
+{
18
+	fpl = fopen(samples[0], "r");
19
+	if(!fpl)
20
+	{
21
+		perror("Unable to open file for testing :");
22
+		ck_abort();
23
+	}
24
+}
25
+
26
+void teardown_file_line(void)
27
+{
28
+	fclose(fpl);
29
+}
13 30
 
14 31
 void teardown_closest(void)
15 32
 {
@@ -80,10 +97,55 @@ START_TEST (test_search_closest_init_vfile)
80 97
 }
81 98
 END_TEST
82 99
 
100
+/*
101
+ * _ttail_file_next_line() & _ttail_file_line_start() tests
102
+ */
103
+START_TEST (test_file_line_next)
104
+{
105
+	long res;
106
+	res = _ttail_file_next_line(fpl);
107
+	ck_assert(res == 77);
108
+	res = _ttail_file_next_line(fpl);
109
+	ck_assert(res == 136);
110
+	res = _ttail_file_next_line(fpl);
111
+	ck_assert(res == 221);
112
+	res = _ttail_file_next_line(fpl);
113
+	ck_assert(res == 298);
114
+	res = _ttail_file_next_line(fpl);
115
+	ck_assert(res == 357);
116
+	res = _ttail_file_next_line(fpl);
117
+	ck_assert(res == 0);
118
+	res = _ttail_file_next_line(fpl);
119
+	ck_assert(res == -1);
120
+}
121
+END_TEST
122
+
123
+START_TEST (test_file_line_start)
124
+{
125
+	long res;
126
+	fseek(fpl, -1, SEEK_END);
127
+	res = _ttail_file_start_line(fpl, samples_sz[0]);
128
+	printf("%ld\n", res);
129
+	ck_assert(res == 357);
130
+	fseek(fpl, -1, SEEK_CUR);
131
+	res = _ttail_file_start_line(fpl, samples_sz[0]);
132
+	printf("%ld\n", res);
133
+	ck_assert(res == 298);
134
+	res = _ttail_file_start_line(fpl, samples_sz[0]);
135
+	ck_assert(res == 221);
136
+	res = _ttail_file_start_line(fpl, samples_sz[0]);
137
+	ck_assert(res == 136);
138
+	res = _ttail_file_start_line(fpl, samples_sz[0]);
139
+	ck_assert(res == 77);
140
+	res = _ttail_file_start_line(fpl, samples_sz[0]);
141
+	ck_assert(res == 0);
142
+}
143
+END_TEST
144
+
83 145
 Suite * ttail_search_suite(void)
84 146
 {
85 147
 	Suite *s;
86
-	TCase *tc_search_closest_fileinit;
148
+	TCase *tc_search_closest_fileinit, *tc_file_line;
87 149
 
88 150
 	s = suite_create("ttail search checks");
89 151
 	tc_search_closest_fileinit = tcase_create("\
@@ -96,7 +158,14 @@ ttail_logline_closest_files_init() checks");
96 158
 	tcase_add_test(tc_search_closest_fileinit,
97 159
 		test_search_closest_init_vfile);
98 160
 
161
+	tc_file_line = tcase_create("ttail_file_*line*() checks");
162
+	tcase_add_checked_fixture(tc_file_line,
163
+		setup_file_line, teardown_file_line);
164
+	tcase_add_test(tc_file_line, test_file_line_next);
165
+	tcase_add_test(tc_file_line, test_file_line_start);
166
+
99 167
 	suite_add_tcase(s, tc_search_closest_fileinit);
168
+	suite_add_tcase(s, tc_file_line);
100 169
 	return s;
101 170
 }
102 171
 

Loading…
Cancel
Save