]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/fld_newftyp.c
ncurses 5.9 - patch 20130309
[ncurses.git] / form / fld_newftyp.c
1 /****************************************************************************
2  * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *   Author:  Juergen Pfeifer, 1995,1997                                    *
31  ****************************************************************************/
32
33 #include "form.priv.h"
34
35 MODULE_ID("$Id: fld_newftyp.c,v 1.19 2010/01/23 21:14:36 tom Exp $")
36
37 static FIELDTYPE default_fieldtype =
38 {
39   0,                            /* status                                      */
40   0L,                           /* reference count                             */
41   (FIELDTYPE *)0,               /* pointer to left  operand                    */
42   (FIELDTYPE *)0,               /* pointer to right operand                    */
43   NULL,                         /* makearg function                            */
44   NULL,                         /* copyarg function                            */
45   NULL,                         /* freearg function                            */
46   INIT_FT_FUNC(NULL),           /* field validation function                   */
47   INIT_FT_FUNC(NULL),           /* Character check function                    */
48   INIT_FT_FUNC(NULL),           /* enumerate next function                     */
49   INIT_FT_FUNC(NULL),           /* enumerate previous function                 */
50 #if NCURSES_INTEROP_FUNCS
51   NULL                          /* generic callback alternative to makearg     */
52 #endif
53 };
54
55 NCURSES_EXPORT_VAR(FIELDTYPE *)
56 _nc_Default_FieldType = &default_fieldtype;
57
58 /*---------------------------------------------------------------------------
59 |   Facility      :  libnform
60 |   Function      :  FIELDTYPE *new_fieldtype(
61 |                       bool (* const field_check)(FIELD *,const void *),
62 |                       bool (* const char_check) (int, const void *) )
63 |
64 |   Description   :  Create a new fieldtype. The application programmer must
65 |                    write a field_check and a char_check function and give
66 |                    them as input to this call.
67 |                    If an error occurs, errno is set to
68 |                       E_BAD_ARGUMENT  - invalid arguments
69 |                       E_SYSTEM_ERROR  - system error (no memory)
70 |
71 |   Return Values :  Fieldtype pointer or NULL if error occurred
72 +--------------------------------------------------------------------------*/
73 NCURSES_EXPORT(FIELDTYPE *)
74 new_fieldtype(bool (*const field_check) (FIELD *, const void *),
75               bool (*const char_check) (int, const void *))
76 {
77   FIELDTYPE *nftyp = (FIELDTYPE *)0;
78
79   T((T_CALLED("new_fieldtype(%p,%p)"), field_check, char_check));
80   if ((field_check) || (char_check))
81     {
82       nftyp = typeMalloc(FIELDTYPE, 1);
83
84       if (nftyp)
85         {
86           T((T_CREATE("fieldtype %p"), (void *)nftyp));
87           *nftyp = default_fieldtype;
88 #if NCURSES_INTEROP_FUNCS
89           nftyp->fieldcheck.ofcheck = field_check;
90           nftyp->charcheck.occheck = char_check;
91 #else
92           nftyp->fcheck = field_check;
93           nftyp->ccheck = char_check;
94 #endif
95         }
96       else
97         {
98           SET_ERROR(E_SYSTEM_ERROR);
99         }
100     }
101   else
102     {
103       SET_ERROR(E_BAD_ARGUMENT);
104     }
105   returnFieldType(nftyp);
106 }
107
108 /*---------------------------------------------------------------------------
109 |   Facility      :  libnform
110 |   Function      :  int free_fieldtype(FIELDTYPE *typ)
111 |
112 |   Description   :  Release the memory associated with this fieldtype.
113 |
114 |   Return Values :  E_OK            - success
115 |                    E_CONNECTED     - there are fields referencing the type
116 |                    E_BAD_ARGUMENT  - invalid fieldtype pointer
117 +--------------------------------------------------------------------------*/
118 NCURSES_EXPORT(int)
119 free_fieldtype(FIELDTYPE *typ)
120 {
121   T((T_CALLED("free_fieldtype(%p)"), (void *)typ));
122
123   if (!typ)
124     RETURN(E_BAD_ARGUMENT);
125
126   if (typ->ref != 0)
127     RETURN(E_CONNECTED);
128
129   if (typ->status & _RESIDENT)
130     RETURN(E_CONNECTED);
131
132   if (typ->status & _LINKED_TYPE)
133     {
134       if (typ->left)
135         typ->left->ref--;
136       if (typ->right)
137         typ->right->ref--;
138     }
139   free(typ);
140   RETURN(E_OK);
141 }
142
143 /* fld_newftyp.c ends here */