Fix Xcode project references to the source tree
[cmake.git] / Source / CursesDialog / form / fty_int.c
blob11b1ac547a5ac9d4289c8e11cada4a82ca534da4
2 /*
3 * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
4 * You may freely copy it for use as a template for your own field types.
5 * If you develop a field type that might be of general use, please send
6 * it back to the ncurses maintainers for inclusion in the next version.
7 */
8 /***************************************************************************
9 * *
10 * Author : Juergen Pfeifer, juergen.pfeifer@gmx.net *
11 * *
12 ***************************************************************************/
14 #include "form.priv.h"
16 MODULE_ID("$Id: fty_int.c,v 1.2 2002-06-18 21:19:38 king Exp $")
18 typedef struct {
19 int precision;
20 long low;
21 long high;
22 } integerARG;
24 /*---------------------------------------------------------------------------
25 | Facility : libnform
26 | Function : static void *Make_Integer_Type( va_list * ap )
28 | Description : Allocate structure for integer type argument.
30 | Return Values : Pointer to argument structure or NULL on error
31 +--------------------------------------------------------------------------*/
32 static void *Make_Integer_Type(va_list * ap)
34 integerARG *argp = (integerARG *)malloc(sizeof(integerARG));
36 if (argp)
38 argp->precision = va_arg(*ap,int);
39 argp->low = va_arg(*ap,long);
40 argp->high = va_arg(*ap,long);
42 return (void *)argp;
45 /*---------------------------------------------------------------------------
46 | Facility : libnform
47 | Function : static void *Copy_Integer_Type(const void * argp)
49 | Description : Copy structure for integer type argument.
51 | Return Values : Pointer to argument structure or NULL on error.
52 +--------------------------------------------------------------------------*/
53 static void *Copy_Integer_Type(const void * argp)
55 const integerARG *ap = (const integerARG *)argp;
56 integerARG *result = (integerARG *)0;
58 if (argp)
60 result = (integerARG *)malloc(sizeof(integerARG));
61 if (result)
62 *result = *ap;
64 return (void *)result;
67 /*---------------------------------------------------------------------------
68 | Facility : libnform
69 | Function : static void Free_Integer_Type(void * argp)
71 | Description : Free structure for integer type argument.
73 | Return Values : -
74 +--------------------------------------------------------------------------*/
75 static void Free_Integer_Type(void * argp)
77 if (argp)
78 free(argp);
81 /*---------------------------------------------------------------------------
82 | Facility : libnform
83 | Function : static bool Check_Integer_Field(
84 | FIELD * field,
85 | const void * argp)
87 | Description : Validate buffer content to be a valid integer value
89 | Return Values : TRUE - field is valid
90 | FALSE - field is invalid
91 +--------------------------------------------------------------------------*/
92 static bool Check_Integer_Field(FIELD * field, const void * argp)
94 const integerARG *argi = (const integerARG *)argp;
95 long low = argi->low;
96 long high = argi->high;
97 int prec = argi->precision;
98 unsigned char *bp = (unsigned char *)field_buffer(field,0);
99 char *s = (char *)bp;
100 long val;
101 char buf[100];
103 while( *bp && *bp==' ') bp++;
104 if (*bp)
106 if (*bp=='-') bp++;
107 while (*bp)
109 if (!isdigit(*bp)) break;
110 bp++;
112 while(*bp && *bp==' ') bp++;
113 if (*bp=='\0')
115 val = atol(s);
116 if (low<high)
118 if (val<low || val>high) return FALSE;
120 sprintf(buf,"%.*ld",(prec>0?prec:0),val);
121 set_field_buffer(field,0,buf);
122 return TRUE;
125 return FALSE;
128 /*---------------------------------------------------------------------------
129 | Facility : libnform
130 | Function : static bool Check_Integer_Character(
131 | int c,
132 | const void * argp)
134 | Description : Check a character for the integer type.
136 | Return Values : TRUE - character is valid
137 | FALSE - character is invalid
138 +--------------------------------------------------------------------------*/
139 static bool Check_Integer_Character(int c, const void * argp)
141 argp=0; /* Silence unused parameter warning. */
142 return ((isdigit(c) || (c=='-')) ? TRUE : FALSE);
145 static FIELDTYPE typeINTEGER = {
146 _HAS_ARGS | _RESIDENT,
147 1, /* this is mutable, so we can't be const */
148 (FIELDTYPE *)0,
149 (FIELDTYPE *)0,
150 Make_Integer_Type,
151 Copy_Integer_Type,
152 Free_Integer_Type,
153 Check_Integer_Field,
154 Check_Integer_Character,
155 NULL,
156 NULL
159 FIELDTYPE* TYPE_INTEGER = &typeINTEGER;
161 /* fty_int.c ends here */