Add /etc/bluetooth/ with common files.
[dragonfly.git] / contrib / ncurses-5.4 / form / fty_int.c
blob5f5c5ab066b227b640331b37fdb10da27b33ad8e
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 *
11 * *
12 ***************************************************************************/
14 #include "form.priv.h"
16 MODULE_ID("$Id: fty_int.c,v 1.13 2003/10/25 14:54:48 tom 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 GCC_UNUSED)
141 return ((isdigit(c) || (c=='-')) ? TRUE : FALSE);
144 static FIELDTYPE typeINTEGER = {
145 _HAS_ARGS | _RESIDENT,
146 1, /* this is mutable, so we can't be const */
147 (FIELDTYPE *)0,
148 (FIELDTYPE *)0,
149 Make_Integer_Type,
150 Copy_Integer_Type,
151 Free_Integer_Type,
152 Check_Integer_Field,
153 Check_Integer_Character,
154 NULL,
155 NULL
158 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeINTEGER;
160 /* fty_int.c ends here */