1 /* $OpenBSD: fty_int.c,v 1.7 2003/04/05 13:41:43 espie Exp $ */ 2 3 4 /* 5 * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT. 6 * You may freely copy it for use as a template for your own field types. 7 * If you develop a field type that might be of general use, please send 8 * it back to the ncurses maintainers for inclusion in the next version. 9 */ 10 /*************************************************************************** 11 * * 12 * Author : Juergen Pfeifer, juergen.pfeifer@gmx.net * 13 * * 14 ***************************************************************************/ 15 16 #include "form.priv.h" 17 18 MODULE_ID("$From: fty_int.c,v 1.11 2000/12/09 23:46:12 tom Exp $") 19 20 typedef struct { 21 int precision; 22 long low; 23 long high; 24 } integerARG; 25 26 /*--------------------------------------------------------------------------- 27 | Facility : libnform 28 | Function : static void *Make_Integer_Type( va_list * ap ) 29 | 30 | Description : Allocate structure for integer type argument. 31 | 32 | Return Values : Pointer to argument structure or NULL on error 33 +--------------------------------------------------------------------------*/ 34 static void *Make_Integer_Type(va_list * ap) 35 { 36 integerARG *argp = (integerARG *)malloc(sizeof(integerARG)); 37 38 if (argp) 39 { 40 argp->precision = va_arg(*ap,int); 41 argp->low = va_arg(*ap,long); 42 argp->high = va_arg(*ap,long); 43 } 44 return (void *)argp; 45 } 46 47 /*--------------------------------------------------------------------------- 48 | Facility : libnform 49 | Function : static void *Copy_Integer_Type(const void * argp) 50 | 51 | Description : Copy structure for integer type argument. 52 | 53 | Return Values : Pointer to argument structure or NULL on error. 54 +--------------------------------------------------------------------------*/ 55 static void *Copy_Integer_Type(const void * argp) 56 { 57 const integerARG *ap = (const integerARG *)argp; 58 integerARG *result = (integerARG *)0; 59 60 if (argp) 61 { 62 result = (integerARG *)malloc(sizeof(integerARG)); 63 if (result) 64 *result = *ap; 65 } 66 return (void *)result; 67 } 68 69 /*--------------------------------------------------------------------------- 70 | Facility : libnform 71 | Function : static void Free_Integer_Type(void * argp) 72 | 73 | Description : Free structure for integer type argument. 74 | 75 | Return Values : - 76 +--------------------------------------------------------------------------*/ 77 static void Free_Integer_Type(void * argp) 78 { 79 if (argp) 80 free(argp); 81 } 82 83 /*--------------------------------------------------------------------------- 84 | Facility : libnform 85 | Function : static bool Check_Integer_Field( 86 | FIELD * field, 87 | const void * argp) 88 | 89 | Description : Validate buffer content to be a valid integer value 90 | 91 | Return Values : TRUE - field is valid 92 | FALSE - field is invalid 93 +--------------------------------------------------------------------------*/ 94 static bool Check_Integer_Field(FIELD * field, const void * argp) 95 { 96 const integerARG *argi = (const integerARG *)argp; 97 long low = argi->low; 98 long high = argi->high; 99 int prec = argi->precision; 100 unsigned char *bp = (unsigned char *)field_buffer(field,0); 101 char *s = (char *)bp; 102 long val; 103 char buf[100]; 104 105 while( *bp && *bp==' ') bp++; 106 if (*bp) 107 { 108 if (*bp=='-') bp++; 109 while (*bp) 110 { 111 if (!isdigit(*bp)) break; 112 bp++; 113 } 114 while(*bp && *bp==' ') bp++; 115 if (*bp=='\0') 116 { 117 val = atol(s); 118 if (low<high) 119 { 120 if (val<low || val>high) return FALSE; 121 } 122 snprintf(buf, sizeof(buf), "%.*ld",(prec>0?prec:0),val); 123 set_field_buffer(field,0,buf); 124 return TRUE; 125 } 126 } 127 return FALSE; 128 } 129 130 /*--------------------------------------------------------------------------- 131 | Facility : libnform 132 | Function : static bool Check_Integer_Character( 133 | int c, 134 | const void * argp) 135 | 136 | Description : Check a character for the integer type. 137 | 138 | Return Values : TRUE - character is valid 139 | FALSE - character is invalid 140 +--------------------------------------------------------------------------*/ 141 static bool Check_Integer_Character(int c, const void * argp GCC_UNUSED) 142 { 143 return ((isdigit(c) || (c=='-')) ? TRUE : FALSE); 144 } 145 146 static FIELDTYPE typeINTEGER = { 147 _HAS_ARGS | _RESIDENT, 148 1, /* this is mutable, so we can't be const */ 149 (FIELDTYPE *)0, 150 (FIELDTYPE *)0, 151 Make_Integer_Type, 152 Copy_Integer_Type, 153 Free_Integer_Type, 154 Check_Integer_Field, 155 Check_Integer_Character, 156 NULL, 157 NULL 158 }; 159 160 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeINTEGER; 161 162 /* fty_int.c ends here */ 163