Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libform Simplify previous.
details: https://anonhg.NetBSD.org/src/rev/463a0638f78c
branches: trunk
changeset: 791502:463a0638f78c
user: christos <christos%NetBSD.org@localhost>
date: Thu Nov 21 15:40:17 2013 +0000
description:
Simplify previous.
diffstat:
lib/libform/field.c | 63 ++++++++++++++++++++++------------------------------
1 files changed, 27 insertions(+), 36 deletions(-)
diffs (98 lines):
diff -r c297be3bc2c9 -r 463a0638f78c lib/libform/field.c
--- a/lib/libform/field.c Thu Nov 21 14:39:09 2013 +0000
+++ b/lib/libform/field.c Thu Nov 21 15:40:17 2013 +0000
@@ -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
* (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -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 @@
char *reformat, *p;
_FORMI_FIELD_LINES *linep;
- size_t bufsize;
+ size_t bufsize, pos;
if (field == NULL)
return NULL;
@@ -492,45 +493,35 @@
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;
-
- reformat = (char *)malloc(bufsize);
- if (reformat == NULL)
- return NULL;
+ if (field->row_count <= 1)
+ return strdup(field->buffers[buffer].string);
- /*
- * 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';
+ /*
+ * 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;
}
- *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;
}
/*
Home |
Main Index |
Thread Index |
Old Index