fty_int.c revision 50276
1 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***************************************************************************/ 13 14#include "form.priv.h" 15 16MODULE_ID("$Id: fty_int.c,v 1.10 1999/05/16 17:23:22 juergen Exp $") 17 18typedef struct { 19 int precision; 20 long low; 21 long high; 22} integerARG; 23 24/*--------------------------------------------------------------------------- 25| Facility : libnform 26| Function : static void *Make_Integer_Type( va_list * ap ) 27| 28| Description : Allocate structure for integer type argument. 29| 30| Return Values : Pointer to argument structure or NULL on error 31+--------------------------------------------------------------------------*/ 32static void *Make_Integer_Type(va_list * ap) 33{ 34 integerARG *argp = (integerARG *)malloc(sizeof(integerARG)); 35 36 if (argp) 37 { 38 argp->precision = va_arg(*ap,int); 39 argp->low = va_arg(*ap,long); 40 argp->high = va_arg(*ap,long); 41 } 42 return (void *)argp; 43} 44 45/*--------------------------------------------------------------------------- 46| Facility : libnform 47| Function : static void *Copy_Integer_Type(const void * argp) 48| 49| Description : Copy structure for integer type argument. 50| 51| Return Values : Pointer to argument structure or NULL on error. 52+--------------------------------------------------------------------------*/ 53static void *Copy_Integer_Type(const void * argp) 54{ 55 const integerARG *ap = (const integerARG *)argp; 56 integerARG *result = (integerARG *)0; 57 58 if (argp) 59 { 60 result = (integerARG *)malloc(sizeof(integerARG)); 61 if (result) 62 *result = *ap; 63 } 64 return (void *)result; 65} 66 67/*--------------------------------------------------------------------------- 68| Facility : libnform 69| Function : static void Free_Integer_Type(void * argp) 70| 71| Description : Free structure for integer type argument. 72| 73| Return Values : - 74+--------------------------------------------------------------------------*/ 75static void Free_Integer_Type(void * argp) 76{ 77 if (argp) 78 free(argp); 79} 80 81/*--------------------------------------------------------------------------- 82| Facility : libnform 83| Function : static bool Check_Integer_Field( 84| FIELD * field, 85| const void * argp) 86| 87| Description : Validate buffer content to be a valid integer value 88| 89| Return Values : TRUE - field is valid 90| FALSE - field is invalid 91+--------------------------------------------------------------------------*/ 92static bool Check_Integer_Field(FIELD * field, const void * argp) 93{ 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]; 102 103 while( *bp && *bp==' ') bp++; 104 if (*bp) 105 { 106 if (*bp=='-') bp++; 107 while (*bp) 108 { 109 if (!isdigit(*bp)) break; 110 bp++; 111 } 112 while(*bp && *bp==' ') bp++; 113 if (*bp=='\0') 114 { 115 val = atol(s); 116 if (low<high) 117 { 118 if (val<low || val>high) return FALSE; 119 } 120 sprintf(buf,"%.*ld",(prec>0?prec:0),val); 121 set_field_buffer(field,0,buf); 122 return TRUE; 123 } 124 } 125 return FALSE; 126} 127 128/*--------------------------------------------------------------------------- 129| Facility : libnform 130| Function : static bool Check_Integer_Character( 131| int c, 132| const void * argp) 133| 134| Description : Check a character for the integer type. 135| 136| Return Values : TRUE - character is valid 137| FALSE - character is invalid 138+--------------------------------------------------------------------------*/ 139static bool Check_Integer_Character(int c, const void * argp GCC_UNUSED) 140{ 141 return ((isdigit(c) || (c=='-')) ? TRUE : FALSE); 142} 143 144static 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 156}; 157 158FIELDTYPE* TYPE_INTEGER = &typeINTEGER; 159 160/* fty_int.c ends here */ 161