]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/fty_alnum.c
ncurses 5.3
[ncurses.git] / form / fty_alnum.c
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                                                *
11 *  Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en              *
12 *                                                                          *
13 ***************************************************************************/
14
15 #include "form.priv.h"
16
17 MODULE_ID("$Id: fty_alnum.c,v 1.11 2002/07/06 15:33:27 juergen Exp $")
18
19 typedef struct {
20   int width;
21 } alnumARG;
22
23 /*---------------------------------------------------------------------------
24 |   Facility      :  libnform  
25 |   Function      :  static void *Make_AlphaNumeric_Type(va_list *ap)
26 |   
27 |   Description   :  Allocate structure for alphanumeric type argument.
28 |
29 |   Return Values :  Pointer to argument structure or NULL on error
30 +--------------------------------------------------------------------------*/
31 static void *Make_AlphaNumeric_Type(va_list * ap)
32 {
33   alnumARG *argp = (alnumARG *)malloc(sizeof(alnumARG));
34
35   if (argp)
36     argp->width = va_arg(*ap,int);
37
38   return ((void *)argp);
39 }
40
41 /*---------------------------------------------------------------------------
42 |   Facility      :  libnform  
43 |   Function      :  static void *Copy_AlphaNumericType(const void *argp)
44 |   
45 |   Description   :  Copy structure for alphanumeric type argument.  
46 |
47 |   Return Values :  Pointer to argument structure or NULL on error.
48 +--------------------------------------------------------------------------*/
49 static void *Copy_AlphaNumeric_Type(const void *argp)
50 {
51   const alnumARG *ap = (const alnumARG *)argp;
52   alnumARG *result = (alnumARG *)malloc(sizeof(alnumARG));
53
54   if (result)
55     *result = *ap;
56
57   return ((void *)result);
58 }
59
60 /*---------------------------------------------------------------------------
61 |   Facility      :  libnform  
62 |   Function      :  static void Free_AlphaNumeric_Type(void *argp)
63 |   
64 |   Description   :  Free structure for alphanumeric type argument.
65 |
66 |   Return Values :  -
67 +--------------------------------------------------------------------------*/
68 static void Free_AlphaNumeric_Type(void * argp)
69 {
70   if (argp) 
71     free(argp);
72 }
73
74 /*---------------------------------------------------------------------------
75 |   Facility      :  libnform  
76 |   Function      :  static bool Check_AlphaNumeric_Field(
77 |                                      FIELD *field,
78 |                                      const void *argp)
79 |   
80 |   Description   :  Validate buffer content to be a valid alphanumeric value
81 |
82 |   Return Values :  TRUE  - field is valid
83 |                    FALSE - field is invalid
84 +--------------------------------------------------------------------------*/
85 static bool Check_AlphaNumeric_Field(FIELD * field, const void * argp)
86 {
87   int width = ((const alnumARG *)argp)->width;
88   unsigned char *bp  = (unsigned char *)field_buffer(field,0);
89   int  l = -1;
90   unsigned char *s;
91
92   while(*bp && *bp==' ') 
93     bp++;
94   if (*bp)
95     {
96       s = bp;
97       while(*bp && isalnum(*bp)) 
98         bp++;
99       l = (int)(bp-s);
100       while(*bp && *bp==' ') 
101         bp++;
102     }
103   return ((*bp || (l < width)) ? FALSE : TRUE);
104 }
105
106 /*---------------------------------------------------------------------------
107 |   Facility      :  libnform  
108 |   Function      :  static bool Check_AlphaNumeric_Character(
109 |                                      int c, 
110 |                                      const void *argp )
111 |   
112 |   Description   :  Check a character for the alphanumeric type.
113 |
114 |   Return Values :  TRUE  - character is valid
115 |                    FALSE - character is invalid
116 +--------------------------------------------------------------------------*/
117 static bool Check_AlphaNumeric_Character(int c, const void * argp GCC_UNUSED)
118 {
119   return (isalnum(c) ? TRUE : FALSE);
120 }
121
122 static FIELDTYPE typeALNUM = {
123   _HAS_ARGS | _RESIDENT,
124   1,                           /* this is mutable, so we can't be const */
125   (FIELDTYPE *)0,
126   (FIELDTYPE *)0,
127   Make_AlphaNumeric_Type,
128   Copy_AlphaNumeric_Type,
129   Free_AlphaNumeric_Type,
130   Check_AlphaNumeric_Field,
131   Check_AlphaNumeric_Character,
132   NULL,
133   NULL
134 };
135
136 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_ALNUM = &typeALNUM;
137
138 /* fty_alnum.c ends here */