]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/fld_type.c
ncurses 4.1
[ncurses.git] / form / fld_type.c
1 /*-----------------------------------------------------------------------------+
2 |           The ncurses form library is  Copyright (C) 1995-1997               |
3 |             by Juergen Pfeifer <Juergen.Pfeifer@T-Online.de>                 |
4 |                          All Rights Reserved.                                |
5 |                                                                              |
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.                         | 
13 |                                                                              |
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 +-----------------------------------------------------------------------------*/
22
23 #include "form.priv.h"
24
25 MODULE_ID("$Id: fld_type.c,v 1.4 1997/05/01 16:47:54 juergen Exp $")
26
27 static FIELDTYPE const default_fieldtype = {
28   0,                   /* status                                      */
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                 */
39 };
40 \f
41 /*---------------------------------------------------------------------------
42 |   Facility      :  libnform  
43 |   Function      :  FIELDTYPE *new_fieldtype(
44 |                       bool (* const field_check)(FIELD *,const void *),
45 |                       bool (* const char_check) (int, const void *) ) 
46 |   
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)
53 |
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 *) )
59 {
60   FIELDTYPE *nftyp = (FIELDTYPE *)0;
61   
62   if ( (field_check) && (char_check) )
63     {
64       nftyp = (FIELDTYPE *)malloc(sizeof(FIELDTYPE));
65       if (nftyp)
66         {
67           *nftyp = default_fieldtype;
68           nftyp->fcheck = field_check;
69           nftyp->ccheck = char_check;
70         }
71       else
72         {
73           SET_ERROR( E_SYSTEM_ERROR );
74         }
75     }
76   else
77     {
78       SET_ERROR( E_BAD_ARGUMENT );
79     }
80   return nftyp;
81 }
82
83 /*---------------------------------------------------------------------------
84 |   Facility      :  libnform  
85 |   Function      :  FIELDTYPE *link_fieldtype(
86 |                                FIELDTYPE *type1,
87 |                                FIELDTYPE *type2)
88 |   
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)
94 |
95 |   Return Values :  Fieldtype pointer or NULL if error occured.
96 +--------------------------------------------------------------------------*/
97 FIELDTYPE *link_fieldtype(FIELDTYPE * type1, FIELDTYPE * type2)
98 {
99   FIELDTYPE *nftyp = (FIELDTYPE *)0;
100
101   if ( type1 && type2 )
102     {
103       nftyp = (FIELDTYPE *)malloc(sizeof(FIELDTYPE));
104       if (nftyp)
105         {
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;
112           nftyp->left  = type1;
113           nftyp->right = type2; 
114           type1->ref++;
115           type2->ref++;
116         }
117       else
118         {
119           SET_ERROR( E_SYSTEM_ERROR );
120         }
121     }
122   else
123     {
124       SET_ERROR( E_BAD_ARGUMENT );
125     }
126   return nftyp;
127 }
128
129 /*---------------------------------------------------------------------------
130 |   Facility      :  libnform  
131 |   Function      :  int free_fieldtype(FIELDTYPE *typ)
132 |   
133 |   Description   :  Release the memory associated with this fieldtype.
134 |
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)
140 {
141   if (!typ)
142     RETURN(E_BAD_ARGUMENT);
143
144   if (typ->ref!=0)
145     RETURN(E_CONNECTED);
146
147   if (typ->status & _RESIDENT)
148     RETURN(E_CONNECTED);
149
150   if (typ->status & _LINKED_TYPE)
151     {
152       if (typ->left ) typ->left->ref--;
153       if (typ->right) typ->right->ref--;
154     }
155   free(typ);
156   RETURN(E_OK);
157 }
158
159 /*---------------------------------------------------------------------------
160 |   Facility      :  libnform  
161 |   Function      :  int set_fieldtype_arg(
162 |                            FIELDTYPE *typ,
163 |                            void * (* const make_arg)(va_list *),
164 |                            void * (* const copy_arg)(const void *),
165 |                            void   (* const free_arg)(void *) )
166 |   
167 |   Description   :  Connects to the type additional arguments necessary
168 |                    for a set_field_type call. The various function pointer
169 |                    arguments are:
170 |                       make_arg : allocates a structure for the field
171 |                                  specific parameters.
172 |                       copy_arg : duplicate the structure created by
173 |                                  make_arg
174 |                       free_arg : Release the memory allocated by make_arg
175 |                                  or copy_arg
176 |
177 |                    At least one of those functions must be non-NULL.
178 |
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 *))
186 {
187   if ( !typ || !make_arg || !copy_arg || !free_arg )
188     RETURN(E_BAD_ARGUMENT);
189
190   typ->status |= _HAS_ARGS;
191   typ->makearg = make_arg;
192   typ->copyarg = copy_arg;
193   typ->freearg = free_arg;
194   RETURN(E_OK);
195 }
196
197 /*---------------------------------------------------------------------------
198 |   Facility      :  libnform  
199 |   Function      :  int set_fieldtype_choice(
200 |                          FIELDTYPE *typ,
201 |                          bool (* const next_choice)(FIELD *,const void *),
202 |                          bool (* const prev_choice)(FIELD *,const void *))
203 |
204 |   Description   :  Define implementation of enumeration requests.
205 |
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 *))
212 {
213   if ( !typ || !next_choice || !prev_choice ) 
214     RETURN(E_BAD_ARGUMENT);
215
216   typ->status |= _HAS_CHOICE;
217   typ->next = next_choice;
218   typ->prev = prev_choice;
219   RETURN(E_OK);
220 }
221
222 /* fld_type.c ends here */