fld_newftyp.c revision 174994
1169691Skan/****************************************************************************
2169691Skan * Copyright (c) 1998-2004,2007 Free Software Foundation, Inc.              *
3169691Skan *                                                                          *
4169691Skan * Permission is hereby granted, free of charge, to any person obtaining a  *
5169691Skan * copy of this software and associated documentation files (the            *
6169691Skan * "Software"), to deal in the Software without restriction, including      *
7169691Skan * without limitation the rights to use, copy, modify, merge, publish,      *
8169691Skan * distribute, distribute with modifications, sublicense, and/or sell       *
9169691Skan * copies of the Software, and to permit persons to whom the Software is    *
10169691Skan * furnished to do so, subject to the following conditions:                 *
11169691Skan *                                                                          *
12169691Skan * The above copyright notice and this permission notice shall be included  *
13169691Skan * in all copies or substantial portions of the Software.                   *
14169691Skan *                                                                          *
15169691Skan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16169691Skan * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17169691Skan * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18169691Skan * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19169691Skan * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20169691Skan * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21169691Skan * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22169691Skan *                                                                          *
23169691Skan * Except as contained in this notice, the name(s) of the above copyright   *
24169691Skan * holders shall not be used in advertising or otherwise to promote the     *
25169691Skan * sale, use or other dealings in this Software without prior written       *
26169691Skan * authorization.                                                           *
27169691Skan ****************************************************************************/
28169691Skan
29169691Skan/****************************************************************************
30169691Skan *   Author:  Juergen Pfeifer, 1995,1997                                    *
31169691Skan ****************************************************************************/
32169691Skan
33169691Skan#include "form.priv.h"
34169691Skan
35169691SkanMODULE_ID("$Id: fld_newftyp.c,v 1.15 2007/10/13 19:30:55 tom Exp $")
36169691Skan
37169691Skanstatic FIELDTYPE const default_fieldtype =
38169691Skan{
39169691Skan  0,				/* status                                      */
40169691Skan  0L,				/* reference count                             */
41169691Skan  (FIELDTYPE *)0,		/* pointer to left  operand                    */
42169691Skan  (FIELDTYPE *)0,		/* pointer to right operand                    */
43169691Skan  NULL,				/* makearg function                            */
44169691Skan  NULL,				/* copyarg function                            */
45169691Skan  NULL,				/* freearg function                            */
46169691Skan  NULL,				/* field validation function                   */
47169691Skan  NULL,				/* Character check function                    */
48169691Skan  NULL,				/* enumerate next function                     */
49169691Skan  NULL				/* enumerate previous function                 */
50169691Skan};
51169691Skan
52169691SkanNCURSES_EXPORT_VAR(const FIELDTYPE *)
53169691Skan_nc_Default_FieldType = &default_fieldtype;
54169691Skan
55169691Skan/*---------------------------------------------------------------------------
56169691Skan|   Facility      :  libnform
57169691Skan|   Function      :  FIELDTYPE *new_fieldtype(
58169691Skan|                       bool (* const field_check)(FIELD *,const void *),
59169691Skan|                       bool (* const char_check) (int, const void *) )
60169691Skan|
61169691Skan|   Description   :  Create a new fieldtype. The application programmer must
62169691Skan|                    write a field_check and a char_check function and give
63169691Skan|                    them as input to this call.
64169691Skan|                    If an error occurs, errno is set to
65169691Skan|                       E_BAD_ARGUMENT  - invalid arguments
66169691Skan|                       E_SYSTEM_ERROR  - system error (no memory)
67169691Skan|
68169691Skan|   Return Values :  Fieldtype pointer or NULL if error occurred
69169691Skan+--------------------------------------------------------------------------*/
70169691SkanNCURSES_EXPORT(FIELDTYPE *)
71169691Skannew_fieldtype(bool (*const field_check) (FIELD *, const void *),
72169691Skan	      bool (*const char_check) (int, const void *))
73169691Skan{
74169691Skan  FIELDTYPE *nftyp = (FIELDTYPE *)0;
75169691Skan
76169691Skan  T((T_CALLED("new_fieldtype(%p,%p)"), field_check, char_check));
77169691Skan  if ((field_check) || (char_check))
78169691Skan    {
79169691Skan      nftyp = typeMalloc(FIELDTYPE, 1);
80169691Skan
81169691Skan      if (nftyp)
82169691Skan	{
83169691Skan	  T((T_CREATE("fieldtype %p"), nftyp));
84169691Skan	  *nftyp = default_fieldtype;
85169691Skan	  nftyp->fcheck = field_check;
86169691Skan	  nftyp->ccheck = char_check;
87169691Skan	}
88169691Skan      else
89169691Skan	{
90169691Skan	  SET_ERROR(E_SYSTEM_ERROR);
91169691Skan	}
92169691Skan    }
93169691Skan  else
94169691Skan    {
95169691Skan      SET_ERROR(E_BAD_ARGUMENT);
96169691Skan    }
97169691Skan  returnFieldType(nftyp);
98169691Skan}
99169691Skan
100169691Skan/*---------------------------------------------------------------------------
101169691Skan|   Facility      :  libnform
102169691Skan|   Function      :  int free_fieldtype(FIELDTYPE *typ)
103169691Skan|
104169691Skan|   Description   :  Release the memory associated with this fieldtype.
105169691Skan|
106169691Skan|   Return Values :  E_OK            - success
107169691Skan|                    E_CONNECTED     - there are fields referencing the type
108169691Skan|                    E_BAD_ARGUMENT  - invalid fieldtype pointer
109169691Skan+--------------------------------------------------------------------------*/
110NCURSES_EXPORT(int)
111free_fieldtype(FIELDTYPE *typ)
112{
113  T((T_CALLED("free_fieldtype(%p)"), typ));
114
115  if (!typ)
116    RETURN(E_BAD_ARGUMENT);
117
118  if (typ->ref != 0)
119    RETURN(E_CONNECTED);
120
121  if (typ->status & _RESIDENT)
122    RETURN(E_CONNECTED);
123
124  if (typ->status & _LINKED_TYPE)
125    {
126      if (typ->left)
127	typ->left->ref--;
128      if (typ->right)
129	typ->right->ref--;
130    }
131  free(typ);
132  RETURN(E_OK);
133}
134
135/* fld_newftyp.c ends here */
136