Browse Source

Code cleaning, updated tests + getline wrapper

The getline wrapper will allow to use getline without calling malloc
and free all the time
Yann Weber 7 years ago
parent
commit
01c295d459
3 changed files with 34 additions and 11 deletions
  1. 15
    1
      src/include/ttail_search_files.h
  2. 13
    4
      src/ttail_search_files.c
  3. 6
    6
      tests/ttail_search_check.c

+ 15
- 1
src/include/ttail_search_files.h View File

@@ -27,12 +27,26 @@ struct _ttail_search_file_s
27 27
 	off_t vsz;
28 28
 	/*<! Computed position */
29 29
 	off_t vpos;
30
+
31
+	char *buf;
32
+	size_t buf_sz;
30 33
 };
31 34
 
32 35
 #include "config.h"
33 36
 #include "ttail.h"
34 37
 #include "ttail_search.h"
35 38
 
39
+/**@brief Convenient wrapper for getline
40
+ *@param ttail_t* TTAIL
41
+ *@param size_t ID file id
42
+ *@return @ref getline()
43
+ */
44
+#define ttail_getline(TTAIL, ID) (getline(\
45
+	&(TTAIL->session->file.buf), &(TTAIL->session->file.buf_sz),\
46
+	TTAIL->logfile[ID]))
47
+/*<!Accessor to getline wrapper buffer */
48
+#define ttail_getline_buf(TTAIL) (TTAIL->session->file.buf)
49
+
36 50
 /**@brief @ref ttail_search_closest() implementation for logfiles
37 51
  *@param ttail_t*
38 52
  *@return 0 if ok -1 if fatal error 1 if not found
@@ -71,7 +85,7 @@ long _ttail_file_next_line(FILE*);
71 85
  *@param FILE* f 
72 86
  *@return -1 on error else return the next line position
73 87
  */
74
-long _ttail_file_start_line(FILE*, const off_t);
88
+long _ttail_file_start_line(FILE*);
75 89
 
76 90
 /**@brief Free the ttail_search_file_t session
77 91
  *@param ttail_t* ttail

+ 13
- 4
src/ttail_search_files.c View File

@@ -66,6 +66,13 @@ int _ttail_search_closest_files_init(ttail_t* t)
66 66
 		goto _ttail_search_closest_files_err;
67 67
 	}
68 68
 
69
+	t->session->file.buf_sz = 128;
70
+	t->session->file.buf = malloc(t->session->file.buf_sz);
71
+	if(!t->session->file.buf)
72
+	{
73
+		goto _ttail_search_closest_files_err;
74
+	}
75
+
69 76
 	return 0;
70 77
 
71 78
 	_ttail_search_closest_files_err:
@@ -164,14 +171,12 @@ long _ttail_file_next_line(FILE* f)
164 171
 	return -1;
165 172
 }
166 173
 
167
-long _ttail_file_start_line(FILE* f, const off_t sz)
174
+long _ttail_file_start_line(FILE* f)
168 175
 {
169 176
 	#define _STARTLN_BUFFLEN 32
170 177
 	long res; /* function result */
171 178
 	long read_beg, cur, last, start;
172
-	char buff[_STARTLN_BUFFLEN];
173
-	char *tmp;
174
-	int read_sz, read_len;
179
+	int read_sz;
175 180
 	int c;
176 181
 
177 182
 	if((start = ftell(f)) < 0)
@@ -235,6 +240,10 @@ void _ttail_search_file_free(ttail_t* t)
235 240
 	{
236 241
 		return;
237 242
 	}
243
+	if(t->session->file.buf)
244
+	{
245
+		free(t->session->file.buf);
246
+	}
238 247
 	if(t->session->file.file_sz)
239 248
 	{
240 249
 		free(t->session->file.file_sz);

+ 6
- 6
tests/ttail_search_check.c View File

@@ -124,20 +124,20 @@ START_TEST (test_file_line_start)
124 124
 {
125 125
 	long res;
126 126
 	fseek(fpl, -1, SEEK_END);
127
-	res = _ttail_file_start_line(fpl, samples_sz[0]);
127
+	res = _ttail_file_start_line(fpl);
128 128
 	printf("%ld\n", res);
129 129
 	ck_assert(res == 357);
130 130
 	fseek(fpl, -1, SEEK_CUR);
131
-	res = _ttail_file_start_line(fpl, samples_sz[0]);
131
+	res = _ttail_file_start_line(fpl);
132 132
 	printf("%ld\n", res);
133 133
 	ck_assert(res == 298);
134
-	res = _ttail_file_start_line(fpl, samples_sz[0]);
134
+	res = _ttail_file_start_line(fpl);
135 135
 	ck_assert(res == 221);
136
-	res = _ttail_file_start_line(fpl, samples_sz[0]);
136
+	res = _ttail_file_start_line(fpl);
137 137
 	ck_assert(res == 136);
138
-	res = _ttail_file_start_line(fpl, samples_sz[0]);
138
+	res = _ttail_file_start_line(fpl);
139 139
 	ck_assert(res == 77);
140
-	res = _ttail_file_start_line(fpl, samples_sz[0]);
140
+	res = _ttail_file_start_line(fpl);
141 141
 	ck_assert(res == 0);
142 142
 }
143 143
 END_TEST

Loading…
Cancel
Save