Browse Source

Add some inline functions + utility function

Set some function inlune and implement an utility function
for file searching functions.
Yann Weber 7 years ago
parent
commit
261248f6a7
4 changed files with 68 additions and 36 deletions
  1. 2
    0
      src/include/config.h.in
  2. 12
    3
      src/include/ttail_search_files.h
  3. 53
    32
      src/ttail_search_files.c
  4. 1
    1
      tests/Makefile.am

+ 2
- 0
src/include/config.h.in View File

@@ -1,5 +1,7 @@
1 1
 /* src/include/config.h.in.  Generated from configure.ac by autoheader.  */
2 2
 
3
+#define inline __inline__
4
+
3 5
 /* Define to 1 if you have the <dlfcn.h> header file. */
4 6
 #undef HAVE_DLFCN_H
5 7
 

+ 12
- 3
src/include/ttail_search_files.h View File

@@ -78,7 +78,7 @@ int _ttail_file_reopen(ttail_t*, size_t);
78 78
  *@param struct tm[2] will be set to min & max
79 79
  *@return 0 on success 1 if no date found or no fp -1 if error
80 80
  */
81
-int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
81
+inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
82 82
 
83 83
 /**@brief Search next line
84 84
  *
@@ -86,7 +86,7 @@ int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
86 86
  *@param FILE* f 
87 87
  *@return -1 on error 0 on EOF else return the next line position
88 88
  */
89
-long _ttail_file_next_line(FILE*);
89
+inline long _ttail_file_next_line(FILE*);
90 90
 
91 91
 /**@brief Search line start
92 92
  *
@@ -94,7 +94,16 @@ long _ttail_file_next_line(FILE*);
94 94
  *@param FILE* f 
95 95
  *@return -1 on error else return the next line position
96 96
  */
97
-long _ttail_file_start_line(FILE*);
97
+inline long _ttail_file_start_line(FILE*);
98
+
99
+/**@brief Search last line with a date < tm from EOF
100
+ *@param ttail ttail_t*
101
+ *@param id size_t
102
+ *@param tm const struct tm*
103
+ *@return -1 on error else the offset of the line first chr
104
+ *@todo checks
105
+ */
106
+inline off_t _ttail_from_search_from_end(ttail_t*, size_t, const struct tm*);
98 107
 
99 108
 /**@brief Free the ttail_search_file_t session
100 109
  *@param ttail_t* ttail

+ 53
- 32
src/ttail_search_files.c View File

@@ -110,36 +110,7 @@ int _ttail_search_closest_files_set_fsizes(ttail_t* t)
110 110
 	return 0;
111 111
 }
112 112
 
113
-int _ttail_search_file_sorted(ttail_t* t)
114
-{
115
-	/* files start & stop log tm, file start id = i*2 file stop = i*2+1 */
116
-	struct tm *ftm;
117
-	size_t i;
118
-
119
-	ftm = malloc(sizeof(struct tm)*t->logfile_sz*2);
120
-	if(!ftm)
121
-	{
122
-		perror("Unable to allocate memory");
123
-		return -3;
124
-	}
125
-	for(i=0; i<t->logfile_sz; i++)
126
-	{
127
-		_ttail_file_minmax(t, 0, ftm+(i*2));
128
-		if(i)
129
-		{
130
-			if(ttail_tm_cmp(ftm+((i-1)*2)+1, ftm+i*2) > 0)
131
-			{
132
-				/* not sorted */
133
-				free(ftm);
134
-				return -1;
135
-			}
136
-		}
137
-	}
138
-	free(ftm);
139
-	return 0;
140
-}
141
-
142
-int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
113
+inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
143 114
 {
144 115
 	FILE *fp;
145 116
 	long cur;
@@ -212,7 +183,7 @@ int _ttail_file_reopen(ttail_t* t, size_t id)
212 183
 	return t->logfile[id]?0:-1;
213 184
 }
214 185
 
215
-long _ttail_file_next_line(FILE* f)
186
+inline long _ttail_file_next_line(FILE* f)
216 187
 {
217 188
 	ssize_t s;
218 189
 	size_t r;
@@ -253,7 +224,7 @@ long _ttail_file_next_line(FILE* f)
253 224
 	return -1;
254 225
 }
255 226
 
256
-long _ttail_file_start_line(FILE* f)
227
+inline long _ttail_file_start_line(FILE* f)
257 228
 {
258 229
 	#define _STARTLN_BUFFLEN 32
259 230
 	long res; /* function result */
@@ -316,6 +287,56 @@ long _ttail_file_start_line(FILE* f)
316 287
 	return res;
317 288
 }
318 289
 
290
+inline off_t _ttail_from_search_from_end(ttail_t* t , size_t id , 
291
+	const struct tm* tm)
292
+{
293
+	FILE *f;
294
+	struct tm curtm;
295
+	off_t last;
296
+	int ret;
297
+
298
+	f = t->logfile[id];
299
+	if(fseek(f, -1, SEEK_END) < 0)
300
+	{
301
+		return -1;
302
+	}
303
+	while(1)
304
+	{
305
+		last = _ttail_file_start_line(f);
306
+		if(last < 0)
307
+		{
308
+			goto _ttail_from_search_from_end_err;
309
+		}
310
+		if(ttail_getline(t, id) < 0)
311
+		{
312
+			goto _ttail_from_search_from_end_err;
313
+		}
314
+		ret = ttail_logline2date(t, ttail_getline_buf(t), &curtm);
315
+		if(ret < 0)
316
+		{
317
+			goto _ttail_from_search_from_end_err;
318
+		}
319
+		if(!ret && !ttail_tm_cmp(&curtm, tm))
320
+		{
321
+			/* found */
322
+			break;
323
+		}
324
+		if(last == 0)
325
+		{
326
+			/* considere the begining of the file as the answer */
327
+			return 0;
328
+		}
329
+		if(fseek(f, last-1, SEEK_CUR))
330
+		{
331
+			goto _ttail_from_search_from_end_err;
332
+		}
333
+	}
334
+	return last;
335
+	
336
+	_ttail_from_search_from_end_err:
337
+	return -1;
338
+}
339
+
319 340
 void _ttail_search_file_free(ttail_t* t)
320 341
 {
321 342
 	if(!t->session)

+ 1
- 1
tests/Makefile.am View File

@@ -10,5 +10,5 @@ ttail_init_check_CFLAGS = @CHECK_CFLAGS@
10 10
 ttail_init_check_LDADD =  $(top_builddir)/src/libttail.a @CHECK_LIBS@
11 11
 
12 12
 ttail_search_check_SOURCES = ttail_search_check.c
13
-ttail_search_check_CFLAGS = @CHECK_CFLAGS@ -D_NOSTATIC
13
+ttail_search_check_CFLAGS = @CHECK_CFLAGS@
14 14
 ttail_search_check_LDADD =  $(top_builddir)/src/libttail.a @CHECK_LIBS@

Loading…
Cancel
Save