1 /*-----------------------------------------------------------------------------+
2 | The ncurses form library is Copyright (C) 1995-1997 |
3 | by Juergen Pfeifer <Juergen.Pfeifer@T-Online.de> |
4 | All Rights Reserved. |
6 | Permission to use, copy, modify, and distribute this software and its |
7 | documentation for any purpose and without fee is hereby granted, provided |
8 | that the above copyright notice appear in all copies and that both that |
9 | copyright notice and this permission notice appear in supporting |
10 | documentation, and that the name of the above listed copyright holder(s) not |
11 | be used in advertising or publicity pertaining to distribution of the |
12 | software without specific, written prior permission. |
14 | THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO |
15 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT- |
16 | NESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR |
17 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RE- |
18 | SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
19 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH |
20 | THE USE OR PERFORMANCE OF THIS SOFTWARE. |
21 +-----------------------------------------------------------------------------*/
23 #include "form.priv.h"
25 MODULE_ID("$Id: fld_type.c,v 1.4 1997/05/01 16:47:54 juergen Exp $")
27 static FIELDTYPE const default_fieldtype = {
29 0L, /* reference count */
30 (FIELDTYPE *)0, /* pointer to left operand */
31 (FIELDTYPE *)0, /* pointer to right operand */
32 NULL, /* makearg function */
33 NULL, /* copyarg function */
34 NULL, /* freearg function */
35 NULL, /* field validation function */
36 NULL, /* Character check function */
37 NULL, /* enumerate next function */
38 NULL /* enumerate previous function */
41 /*---------------------------------------------------------------------------
43 | Function : FIELDTYPE *new_fieldtype(
44 | bool (* const field_check)(FIELD *,const void *),
45 | bool (* const char_check) (int, const void *) )
47 | Description : Create a new fieldtype. The application programmer must
48 | write a field_check and a char_check function and give
49 | them as input to this call.
50 | If an error occurs, errno is set to
51 | E_BAD_ARGUMENT - invalid arguments
52 | E_SYSTEM_ERROR - system error (no memory)
54 | Return Values : Fieldtype pointer or NULL if error occured
55 +--------------------------------------------------------------------------*/
56 FIELDTYPE *new_fieldtype(
57 bool (* const field_check)(FIELD *,const void *),
58 bool (* const char_check) (int,const void *) )
60 FIELDTYPE *nftyp = (FIELDTYPE *)0;
62 if ( (field_check) && (char_check) )
64 nftyp = (FIELDTYPE *)malloc(sizeof(FIELDTYPE));
67 *nftyp = default_fieldtype;
68 nftyp->fcheck = field_check;
69 nftyp->ccheck = char_check;
73 SET_ERROR( E_SYSTEM_ERROR );
78 SET_ERROR( E_BAD_ARGUMENT );
83 /*---------------------------------------------------------------------------
85 | Function : FIELDTYPE *link_fieldtype(
89 | Description : Create a new fieldtype built from the two given types.
90 | They are connected by an logical 'OR'.
91 | If an error occurs, errno is set to
92 | E_BAD_ARGUMENT - invalid arguments
93 | E_SYSTEM_ERROR - system error (no memory)
95 | Return Values : Fieldtype pointer or NULL if error occured.
96 +--------------------------------------------------------------------------*/
97 FIELDTYPE *link_fieldtype(FIELDTYPE * type1, FIELDTYPE * type2)
99 FIELDTYPE *nftyp = (FIELDTYPE *)0;
101 if ( type1 && type2 )
103 nftyp = (FIELDTYPE *)malloc(sizeof(FIELDTYPE));
106 *nftyp = default_fieldtype;
107 nftyp->status |= _LINKED_TYPE;
108 if ((type1->status & _HAS_ARGS) || (type2->status & _HAS_ARGS) )
109 nftyp->status |= _HAS_ARGS;
110 if ((type1->status & _HAS_CHOICE) || (type2->status & _HAS_CHOICE) )
111 nftyp->status |= _HAS_CHOICE;
113 nftyp->right = type2;
119 SET_ERROR( E_SYSTEM_ERROR );
124 SET_ERROR( E_BAD_ARGUMENT );
129 /*---------------------------------------------------------------------------
130 | Facility : libnform
131 | Function : int free_fieldtype(FIELDTYPE *typ)
133 | Description : Release the memory associated with this fieldtype.
135 | Return Values : E_OK - success
136 | E_CONNECTED - there are fields referencing the type
137 | E_BAD_ARGUMENT - invalid fieldtype pointer
138 +--------------------------------------------------------------------------*/
139 int free_fieldtype(FIELDTYPE *typ)
142 RETURN(E_BAD_ARGUMENT);
147 if (typ->status & _RESIDENT)
150 if (typ->status & _LINKED_TYPE)
152 if (typ->left ) typ->left->ref--;
153 if (typ->right) typ->right->ref--;
159 /*---------------------------------------------------------------------------
160 | Facility : libnform
161 | Function : int set_fieldtype_arg(
163 | void * (* const make_arg)(va_list *),
164 | void * (* const copy_arg)(const void *),
165 | void (* const free_arg)(void *) )
167 | Description : Connects to the type additional arguments necessary
168 | for a set_field_type call. The various function pointer
170 | make_arg : allocates a structure for the field
171 | specific parameters.
172 | copy_arg : duplicate the structure created by
174 | free_arg : Release the memory allocated by make_arg
177 | At least one of those functions must be non-NULL.
179 | Return Values : E_OK - success
180 | E_BAD_ARGUMENT - invalid argument
181 +--------------------------------------------------------------------------*/
182 int set_fieldtype_arg(FIELDTYPE * typ,
183 void * (* const make_arg)(va_list *),
184 void * (* const copy_arg)(const void *),
185 void (* const free_arg)(void *))
187 if ( !typ || !make_arg || !copy_arg || !free_arg )
188 RETURN(E_BAD_ARGUMENT);
190 typ->status |= _HAS_ARGS;
191 typ->makearg = make_arg;
192 typ->copyarg = copy_arg;
193 typ->freearg = free_arg;
197 /*---------------------------------------------------------------------------
198 | Facility : libnform
199 | Function : int set_fieldtype_choice(
201 | bool (* const next_choice)(FIELD *,const void *),
202 | bool (* const prev_choice)(FIELD *,const void *))
204 | Description : Define implementation of enumeration requests.
206 | Return Values : E_OK - success
207 | E_BAD_ARGUMENT - invalid arguments
208 +--------------------------------------------------------------------------*/
209 int set_fieldtype_choice(FIELDTYPE * typ,
210 bool (* const next_choice) (FIELD *,const void *),
211 bool (* const prev_choice) (FIELD *,const void *))
213 if ( !typ || !next_choice || !prev_choice )
214 RETURN(E_BAD_ARGUMENT);
216 typ->status |= _HAS_CHOICE;
217 typ->next = next_choice;
218 typ->prev = prev_choice;
222 /* fld_type.c ends here */