Browse code

Added textIterate

git-svn: trunk@1259

Nigel Horne authored on 2005/01/19 14:31:55
Showing 1 changed files
... ...
@@ -16,6 +16,9 @@
16 16
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
  *
18 18
  * $Log: text.c,v $
19
+ * Revision 1.14  2005/01/19 05:31:55  nigelhorne
20
+ * Added textIterate
21
+ *
19 22
  * Revision 1.13  2004/12/08 19:03:41  nigelhorne
20 23
  * Fix compilation error on Solaris
21 24
  *
... ...
@@ -48,7 +51,7 @@
48 48
  *
49 49
  */
50 50
 
51
-static	char	const	rcsid[] = "$Id: text.c,v 1.13 2004/12/08 19:03:41 nigelhorne Exp $";
51
+static	char	const	rcsid[] = "$Id: text.c,v 1.14 2005/01/19 05:31:55 nigelhorne Exp $";
52 52
 
53 53
 #if HAVE_CONFIG_H
54 54
 #include "clamav-config.h"
... ...
@@ -81,6 +84,10 @@ static	char	const	rcsid[] = "$Id: text.c,v 1.13 2004/12/08 19:03:41 nigelhorne E
81 81
 #include "others.h"
82 82
 
83 83
 static	text	*textCopy(const text *t_head);
84
+static	void	addToFileblob(const line_t *line, void *arg);
85
+static	void	getLength(const line_t *line, void *arg);
86
+static	void	addToBlob(const line_t *line, void *arg);
87
+static	void	*textIterate(const text *t_text, void (*cb)(const line_t *line, void *arg), void *arg);
84 88
 
85 89
 void
86 90
 textDestroy(text *t_head)
... ...
@@ -141,6 +148,7 @@ text *
141 141
 textAdd(text *t_head, const text *t)
142 142
 {
143 143
 	text *ret;
144
+	int count;
144 145
 
145 146
 	if(t_head == NULL)
146 147
 		return textCopy(t);
... ...
@@ -150,8 +158,13 @@ textAdd(text *t_head, const text *t)
150 150
 
151 151
 	ret = t_head;
152 152
 
153
-	while(t_head->t_next)
153
+	count = 0;
154
+	while(t_head->t_next) {
155
+		count++;
154 156
 		t_head = t_head->t_next;
157
+	}
158
+
159
+	cli_dbgmsg("textAdd: count = %d\n", count);
155 160
 
156 161
 	while(t) {
157 162
 		t_head->t_next = (text *)cli_malloc(sizeof(text));
... ...
@@ -201,10 +214,17 @@ textAddMessage(text *aText, message *aMessage)
201 201
 blob *
202 202
 textToBlob(const text *t, blob *b)
203 203
 {
204
-	const text *t1;
205
-	size_t s = 0;
204
+	size_t s;
206 205
 
207
-	assert(t != NULL);
206
+	if(t == NULL)
207
+		return NULL;
208
+
209
+	s = 0;
210
+
211
+	textIterate(t, getLength, &s);
212
+
213
+	if(s == 0)
214
+		return b;
208 215
 
209 216
 	if(b == NULL) {
210 217
 		b = blobCreate();
... ...
@@ -213,22 +233,9 @@ textToBlob(const text *t, blob *b)
213 213
 			return NULL;
214 214
 	}
215 215
 
216
-	for(t1 = t; t1; t1 = t1->t_next)
217
-		if(t1->t_line)
218
-			s += strlen(lineGetData(t1->t_line)) + 1;
219
-		else
220
-			s++;
221
-
222 216
 	blobGrow(b, s);
223 217
 
224
-	do {
225
-		if(t->t_line) {
226
-			const char *l = lineGetData(t->t_line);
227
-
228
-			blobAddData(b, (unsigned char *)l, strlen(l));
229
-		}
230
-		blobAddData(b, (unsigned char *)"\n", 1);
231
-	} while((t = t->t_next) != NULL);
218
+	textIterate(t, addToBlob, b);
232 219
 
233 220
 	blobClose(b);
234 221
 
... ...
@@ -248,14 +255,52 @@ textToFileblob(const text *t, fileblob *fb)
248 248
 			return NULL;
249 249
 	}
250 250
 
251
-	do {
252
-		if(t->t_line) {
253
-			const char *l = lineGetData(t->t_line);
251
+	return textIterate(t, addToFileblob, fb);
252
+}
254 253
 
255
-			fileblobAddData(fb, (unsigned char *)l, strlen(l));
256
-		}
257
-		fileblobAddData(fb, (unsigned char *)"\n", 1);
258
-	} while((t = t->t_next) != NULL);
254
+static void
255
+getLength(const line_t *line, void *arg)
256
+{
257
+	size_t *length = (size_t *)arg;
259 258
 
260
-	return fb;
259
+	if(line)
260
+		*length += strlen(lineGetData(line)) + 1;
261
+	else
262
+		(*length)++;
263
+}
264
+
265
+static void
266
+addToBlob(const line_t *line, void *arg)
267
+{
268
+	blob *b = (blob *)arg;
269
+
270
+	if(line) {
271
+		const char *l = lineGetData(line);
272
+
273
+		blobAddData(b, (unsigned char *)l, strlen(l));
274
+	}
275
+	blobAddData(b, (unsigned char *)"\n", 1);
276
+}
277
+
278
+static void
279
+addToFileblob(const line_t *line, void *arg)
280
+{
281
+	fileblob *fb = (fileblob *)arg;
282
+
283
+	if(line) {
284
+		const char *l = lineGetData(line);
285
+
286
+		fileblobAddData(fb, (unsigned char *)l, strlen(l));
287
+	}
288
+	fileblobAddData(fb, (unsigned char *)"\n", 1);
289
+}
290
+
291
+static void *
292
+textIterate(const text *t_text, void (*cb)(const line_t *item, void *arg), void *arg)
293
+{
294
+	while(t_text) {
295
+		(*cb)(t_text->t_line, arg);
296
+		t_text = t_text->t_next;
297
+	}
298
+	return arg;
261 299
 }