Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libform Added extension to interface, set_field_printf w...
details: https://anonhg.NetBSD.org/src/rev/a00ed731a058
branches: trunk
changeset: 535066:a00ed731a058
user: blymn <blymn%NetBSD.org@localhost>
date: Fri Aug 09 14:15:12 2002 +0000
description:
Added extension to interface, set_field_printf which allows a printf
style setting of field buffers.
diffstat:
lib/libform/field.c | 132 +++++++++++++++++++++++++++------------
lib/libform/form.h | 4 +-
lib/libform/form_field_buffer.3 | 19 +++++-
lib/libform/forms.3 | 6 +-
lib/libform/shlib_version | 4 +-
5 files changed, 119 insertions(+), 46 deletions(-)
diffs (285 lines):
diff -r a6b14289b3d6 -r a00ed731a058 lib/libform/field.c
--- a/lib/libform/field.c Fri Aug 09 14:10:30 2002 +0000
+++ b/lib/libform/field.c Fri Aug 09 14:15:12 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: field.c,v 1.20 2002/08/05 12:43:44 blymn Exp $ */
+/* $NetBSD: field.c,v 1.21 2002/08/09 14:15:12 blymn Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
* (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -30,6 +30,7 @@
#include <stdlib.h>
#include <strings.h>
+#include <stdarg.h>
#include <form.h>
#include "internals.h"
@@ -77,6 +78,8 @@
};
/* internal function prototypes */
+static int
+field_buffer_init(FIELD *field, int buffer, size_t len);
static FIELD *
_formi_create_field(FIELD *, int, int, int, int, int, int);
@@ -302,13 +305,98 @@
}
/*
+ * Init all the field variables, perform wrapping and other tasks
+ * after the field buffer is set.
+ */
+static int
+field_buffer_init(FIELD *field, int buffer, size_t len)
+{
+ int status;
+
+ if (buffer == 0) {
+ field->start_char = 0;
+ field->start_line = 0;
+ field->row_xpos = 0;
+ field->cursor_xpos = 0;
+ field->cursor_ypos = 0;
+ field->row_count = 1; /* must be at least one row */
+ field->lines[0].start = 0;
+ field->lines[0].end = (len > 0)? (len - 1) : 0;
+ field->lines[0].length =
+ _formi_tab_expanded_length(field->buffers[0].string,
+ 0, field->lines[0].end);
+
+ /* we have to hope the wrap works - if it does not then the
+ buffer is pretty much borked */
+ status = _formi_wrap_field(field, 0);
+ if (status != E_OK)
+ return status;
+
+ /*
+ * calculate the tabs for a single row field, the
+ * multiline case is handled when the wrap is done.
+ */
+ if (field->row_count == 1)
+ _formi_calculate_tabs(field, 0);
+
+ /* redraw the field to reflect the new contents. If the field
+ * is attached....
+ */
+ if ((field->parent != NULL) && (field->parent->posted == 1)) {
+ _formi_redraw_field(field->parent, field->index);
+ /* make sure cursor goes back to current field */
+ pos_form_cursor(field->parent);
+ }
+ }
+
+ return E_OK;
+}
+
+
+/*
+ * Set the field buffer to the string that results from processing
+ * the given format (fmt) using sprintf.
+ */
+int
+set_field_printf(FIELD *field, int buffer, char *fmt, ...)
+{
+ int len;
+ va_list args;
+
+ if (field == NULL)
+ return E_BAD_ARGUMENT;
+
+ if (buffer >= field->nbuf)
+ return E_BAD_ARGUMENT;
+
+ va_start(args, fmt);
+ /* check for buffer already existing, free the storage */
+ if (field->buffers[buffer].allocated != 0)
+ free(field->buffers[buffer].string);
+
+ len = vasprintf(&field->buffers[buffer].string, fmt, args);
+ va_end(args);
+ if (len < 0)
+ return E_SYSTEM_ERROR;
+
+ field->buffers[buffer].length = len;
+ field->buffers[buffer].allocated = len + 1;
+ if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
+ && ((field->rows + field->nrows) == 1))
+ len = field->cols;
+
+ field->buffers[buffer].string[len] = '\0';
+ return field_buffer_init(field, buffer, (unsigned int) len);
+}
+
+/*
* Set the value of the field buffer to the value given.
*/
int
set_field_buffer(FIELD *field, int buffer, char *value)
{
- unsigned len;
+ size_t len;
int status;
if (field == NULL)
@@ -346,43 +434,7 @@
strlcpy(field->buffers[buffer].string, value, len + 1);
field->buffers[buffer].length = len;
field->buffers[buffer].allocated = len + 1;
-
- if (buffer == 0) {
- field->start_char = 0;
- field->start_line = 0;
- field->row_xpos = 0;
- field->cursor_xpos = 0;
- field->cursor_ypos = 0;
- field->row_count = 1; /* must be at least one row */
- field->lines[0].start = 0;
- field->lines[0].end = (len > 0)? (len - 1) : 0;
- field->lines[0].length =
- _formi_tab_expanded_length(field->buffers[0].string,
- 0, field->lines[0].end);
-
- /* we have to hope the wrap works - if it does not then the
- buffer is pretty much borked */
- status = _formi_wrap_field(field, 0);
- if (status != E_OK)
- return status;
-
- /*
- * calculate the tabs for a single row field, the
- * multiline case is handled when the wrap is done.
- */
- if (field->row_count == 1)
- _formi_calculate_tabs(field, 0);
-
- /* redraw the field to reflect the new contents. If the field
- * is attached....
- */
- if ((field->parent != NULL) && (field->parent->posted == 1)) {
- _formi_redraw_field(field->parent, field->index);
- /* make sure cursor goes back to current field */
- pos_form_cursor(field->parent);
- }
-
- }
+ status = field_buffer_init(field, buffer, len);
#ifdef DEBUG
fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
@@ -393,7 +445,7 @@
field->lines[0].length);
#endif
- return E_OK;
+ return status;
}
/*
diff -r a6b14289b3d6 -r a00ed731a058 lib/libform/form.h
--- a/lib/libform/form.h Fri Aug 09 14:10:30 2002 +0000
+++ b/lib/libform/form.h Fri Aug 09 14:15:12 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: form.h,v 1.16 2002/05/20 15:00:11 blymn Exp $ */
+/* $NetBSD: form.h,v 1.17 2002/08/09 14:15:13 blymn Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@@ -348,6 +348,8 @@
int set_field_just(FIELD *, int);
int set_field_opts(FIELD *, Form_Options);
int set_field_pad(FIELD *, int);
+int set_field_printf(FIELD *, int, char *, ...)
+ __attribute__((__format__(__printf__, 3, 4)));
int set_field_status(FIELD *, int);
int set_field_term(FORM *, Form_Hook);
int set_field_type(FIELD *, FIELDTYPE *, ...);
diff -r a6b14289b3d6 -r a00ed731a058 lib/libform/form_field_buffer.3
--- a/lib/libform/form_field_buffer.3 Fri Aug 09 14:10:30 2002 +0000
+++ b/lib/libform/form_field_buffer.3 Fri Aug 09 14:15:12 2002 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: form_field_buffer.3,v 1.5 2002/08/06 12:44:34 wiz Exp $
+.\" $NetBSD: form_field_buffer.3,v 1.6 2002/08/09 14:15:13 blymn Exp $
.\"
.\" Copyright (c) 2001
.\" Brett Lymn - blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost
@@ -36,6 +36,7 @@
.Nm field_buffer ,
.Nm field_status ,
.Nm set_field_buffer ,
+.Nm set_field_printf ,
.Nm set_field_status ,
.Nm set_max_field
.Nd form library
@@ -50,6 +51,8 @@
.Ft int
.Fn set_field_buffer "FIELD *field" "int buffer" "char *value"
.Ft int
+.Fn set_field_printf "FIELD *field" "int buffer" "char *fmt" "..."
+.Ft int
.Fn set_field_status "FIELD *field" "int status"
.Sh DESCRIPTION
The
@@ -61,7 +64,14 @@
The field buffer may be set by calling
.Fn set_field_buffer
which will set the given buffer number to the contents of the string
-passed. Calling
+passed. A buffer may also be set by calling
+.Fn set_field_printf
+which sets the buffer using the format arg
+.Fa fmt
+after being expanded using the subsequent arguments in the same manner
+as
+.Xr sprintf 3
+does. Calling
.Fn field_status
will return the status of the first buffer attached to the field. If
the field has been modified then the function will return TRUE
@@ -95,3 +105,8 @@
.Pa \*[Lt]curses.h\*[Gt]
and
.Pa \*[Lt]eti.h\*[Gt] .
+The function
+.Fn set_field_printf
+is a
+.Nx
+extension and must not be used in portable code.
diff -r a6b14289b3d6 -r a00ed731a058 lib/libform/forms.3
--- a/lib/libform/forms.3 Fri Aug 09 14:10:30 2002 +0000
+++ b/lib/libform/forms.3 Fri Aug 09 14:15:12 2002 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: forms.3,v 1.8 2002/05/20 15:00:11 blymn Exp $
+.\" $NetBSD: forms.3,v 1.9 2002/08/09 14:15:13 blymn Exp $
.\"
.\" Copyright (c) 2001
.\" Brett Lymn - blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost
@@ -134,6 +134,7 @@
set_field_just form_field_just(3)
set_field_opts form_field_opts(3)
set_field_pad form_field_attributes(3)
+set_field_printf form_field_buffer(3)
set_field_status form_field_buffer(3)
set_field_term form_hook(3)
set_field_type form_field_validation(3)
@@ -223,4 +224,7 @@
field buffer and also will allow TAB to be inserted into a field
buffer via the form driver and correctly calculates the cursor
position allowing for expansion of the TAB character.
+.It set_field_printf
+This function is a NetBSD extension and must not be used in portable
+code.
.El
diff -r a6b14289b3d6 -r a00ed731a058 lib/libform/shlib_version
--- a/lib/libform/shlib_version Fri Aug 09 14:10:30 2002 +0000
+++ b/lib/libform/shlib_version Fri Aug 09 14:15:12 2002 +0000
@@ -1,5 +1,5 @@
-# $NetBSD: shlib_version,v 1.13 2001/12/03 14:03:19 christos Exp $
+# $NetBSD: shlib_version,v 1.14 2002/08/09 14:15:13 blymn Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing
#
major=3
-minor=0
+minor=1
Home |
Main Index |
Thread Index |
Old Index