Module Name:    src
Committed By:   christos
Date:           Thu Nov 21 15:40:17 UTC 2013

Modified Files:
        src/lib/libform: field.c

Log Message:
Simplify previous.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/lib/libform/field.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libform/field.c
diff -u src/lib/libform/field.c:1.26 src/lib/libform/field.c:1.27
--- src/lib/libform/field.c:1.26	Thu Nov 21 04:40:19 2013
+++ src/lib/libform/field.c	Thu Nov 21 10:40:17 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: field.c,v 1.26 2013/11/21 09:40:19 blymn Exp $	*/
+/*	$NetBSD: field.c,v 1.27 2013/11/21 15:40:17 christos Exp $	*/
 /*-
  * Copyright (c) 1998-1999 Brett Lymn
  *                         (bl...@baea.com.au, brett_l...@yahoo.com.au)
@@ -29,8 +29,9 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: field.c,v 1.26 2013/11/21 09:40:19 blymn Exp $");
+__RCSID("$NetBSD: field.c,v 1.27 2013/11/21 15:40:17 christos Exp $");
 
+#include <sys/param.h>
 #include <stdlib.h>
 #include <strings.h>
 #include <stdarg.h>
@@ -472,7 +473,7 @@ field_buffer(FIELD *field, int buffer)
 
 	char *reformat, *p;
 	_FORMI_FIELD_LINES *linep;
-	size_t bufsize;
+	size_t bufsize, pos;
 	
 	if (field == NULL)
 		return NULL;
@@ -492,45 +493,35 @@ field_buffer(FIELD *field, int buffer)
 	if (_formi_sync_buffer(field) != E_OK)
 		return NULL;
 	
-	if ((field->opts & O_REFORMAT) != O_REFORMAT) {
+	if ((field->opts & O_REFORMAT) != O_REFORMAT)
 		return field->buffers[buffer].string;
-	} else {
-		if (field->row_count > 1) {
 
-			  /*
-			   * compute reformat buffer size
-			   * each line length plus one line feed
-			   * except for last line without line feed
-			   * but plus one NUL character
-			   */
-			bufsize = 0;
-			for (linep=field->alines; linep; linep=linep->next)
-				bufsize += strlen(linep->string)+1;
+	if (field->row_count <= 1)
+		return strdup(field->buffers[buffer].string);
 
-			reformat = (char *)malloc(bufsize);
-			if (reformat == NULL)
+	/*
+	 * create a single string containing each line,
+	 * separated by newline, last line having no
+	 * newline, but NUL terminated.
+	 */
+	bufsize = pos = 0;
+	reformat = NULL;
+	for (linep = field->alines; linep; linep = linep->next) {
+		size_t len = strlen(linep->string);
+		if (len + 1 >= bufsize - pos) {
+			bufsize += MAX(1024, 2 * len);
+			p = realloc(reformat, bufsize);
+			if (p == NULL) {
+				free(reformat);
 				return NULL;
-
-			  /*
-			   * foreach row copy line, append newline, no
-			   * newline on last row.
-			   */
-			p = reformat;
-			for (linep=field->alines; linep; linep=linep->next) {
-				strcpy(p, linep->string);
-				p += strlen(linep->string);
-				if (linep->next)
-					*p++ = '\n';
 			}
-			*p = '\0';
-
-			return reformat;
-		} else {
-			asprintf(&reformat, "%s",
-				 field->buffers[buffer].string);
-			return reformat;
+			reformat = p;
 		}
+		memcpy(reformat + pos, linep->string, len);
+		pos += len;
+		reformat[pos++] = linep->next ? '\n' : '\0';
 	}
+	return reformat;
 }
 
 /*

Reply via email to