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