]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/fld_newftyp.c
ncurses 6.1 - patch 20191116
[ncurses.git] / form / fld_newftyp.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2018 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.21 2018/12/16 00:14:22 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   TR_FUNC_BFR(2);
80
81   T((T_CALLED("new_fieldtype(%s,%s)"),
82      TR_FUNC_ARG(0, field_check),
83      TR_FUNC_ARG(1, char_check)));
84
85   if ((field_check) || (char_check))
86     {
87       nftyp = typeMalloc(FIELDTYPE, 1);
88
89       if (nftyp)
90         {
91           T((T_CREATE("fieldtype %p"), (void *)nftyp));
92           *nftyp = default_fieldtype;
93 #if NCURSES_INTEROP_FUNCS
94           nftyp->fieldcheck.ofcheck = field_check;
95           nftyp->charcheck.occheck = char_check;
96 #else
97           nftyp->fcheck = field_check;
98           nftyp->ccheck = char_check;
99 #endif
100         }
101       else
102         {
103           SET_ERROR(E_SYSTEM_ERROR);
104         }
105     }
106   else
107     {
108       SET_ERROR(E_BAD_ARGUMENT);
109     }
110   returnFieldType(nftyp);
111 }
112
113 /*---------------------------------------------------------------------------
114 |   Facility      :  libnform
115 |   Function      :  int free_fieldtype(FIELDTYPE *typ)
116 |
117 |   Description   :  Release the memory associated with this fieldtype.
118 |
119 |   Return Values :  E_OK            - success
120 |                    E_CONNECTED     - there are fields referencing the type
121 |                    E_BAD_ARGUMENT  - invalid fieldtype pointer
122 +--------------------------------------------------------------------------*/
123 NCURSES_EXPORT(int)
124 free_fieldtype(FIELDTYPE *typ)
125 {
126   T((T_CALLED("free_fieldtype(%p)"), (void *)typ));
127
128   if (!typ)
129     RETURN(E_BAD_ARGUMENT);
130
131   if (typ->ref != 0)
132     RETURN(E_CONNECTED);
133
134   if (typ->status & _RESIDENT)
135     RETURN(E_CONNECTED);
136
137   if (typ->status & _LINKED_TYPE)
138     {
139       if (typ->left)
140         typ->left->ref--;
141       if (typ->right)
142         typ->right->ref--;
143     }
144   free(typ);
145   RETURN(E_OK);
146 }
147
148 /* fld_newftyp.c ends here */