]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/fty_alpha.c
ncurses 5.0
[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, juergen.pfeifer@gmx.net                       *
11 *                                                                          *
12 ***************************************************************************/
13
14 #include "form.priv.h"
15
16 MODULE_ID("$Id: fty_alpha.c,v 1.9 1999/05/16 17:22:58 juergen Exp $")
17
18 typedef struct {
19   int width;
20 } alphaARG;
21
22 /*---------------------------------------------------------------------------
23 |   Facility      :  libnform  
24 |   Function      :  static void *Make_Alpha_Type(va_list *ap)
25 |   
26 |   Description   :  Allocate structure for alpha type argument.
27 |
28 |   Return Values :  Pointer to argument structure or NULL on error
29 +--------------------------------------------------------------------------*/
30 static void *Make_Alpha_Type(va_list * ap)
31 {
32   alphaARG *argp = (alphaARG *)malloc(sizeof(alphaARG));
33   if (argp)
34     {
35       argp->width = va_arg(*ap,int);
36     }
37   return ((void *)argp);
38 }
39
40 /*---------------------------------------------------------------------------
41 |   Facility      :  libnform  
42 |   Function      :  static void *Copy_Alpha_Type(const void * argp)
43 |   
44 |   Description   :  Copy structure for alpha type argument.  
45 |
46 |   Return Values :  Pointer to argument structure or NULL on error.
47 +--------------------------------------------------------------------------*/
48 static void *Copy_Alpha_Type(const void * argp)
49 {
50   const alphaARG *ap = (const alphaARG *)argp;
51   alphaARG *result = (alphaARG *)malloc(sizeof(alphaARG));
52   
53   if (result)
54     {
55       *result = *ap;
56     }
57   return ((void *)result);
58 }
59
60 /*---------------------------------------------------------------------------
61 |   Facility      :  libnform  
62 |   Function      :  static void Free_Alpha_Type( void * argp )
63 |   
64 |   Description   :  Free structure for alpha type argument.
65 |
66 |   Return Values :  -
67 +--------------------------------------------------------------------------*/
68 static void Free_Alpha_Type(void * argp)
69 {
70   if (argp) 
71     free(argp);
72 }
73
74 /*---------------------------------------------------------------------------
75 |   Facility      :  libnform  
76 |   Function      :  static bool Check_Alpha_Field(
77 |                                      FIELD * field,
78 |                                      const void * argp)
79 |   
80 |   Description   :  Validate buffer content to be a valid alpha value
81 |
82 |   Return Values :  TRUE  - field is valid
83 |                    FALSE - field is invalid
84 +--------------------------------------------------------------------------*/
85 static bool Check_Alpha_Field(FIELD * field, const void * argp)
86 {
87   int width = ((const alphaARG *)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 && isalpha(*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_Alpha_Character(
109 |                                      int c,
110 |                                      const void * argp)
111 |   
112 |   Description   :  Check a character for the alpha type.
113 |
114 |   Return Values :  TRUE  - character is valid
115 |                    FALSE - character is invalid
116 +--------------------------------------------------------------------------*/
117 static bool Check_Alpha_Character(int c, const void * argp GCC_UNUSED)
118 {
119   return (isalpha(c) ? TRUE : FALSE);
120 }
121
122 static FIELDTYPE typeALPHA = {
123   _HAS_ARGS | _RESIDENT,
124   1,                           /* this is mutable, so we can't be const */
125   (FIELDTYPE *)0,
126   (FIELDTYPE *)0,
127   Make_Alpha_Type,
128   Copy_Alpha_Type,
129   Free_Alpha_Type,
130   Check_Alpha_Field,
131   Check_Alpha_Character,
132   NULL,
133   NULL
134 };
135
136 FIELDTYPE* TYPE_ALPHA = &typeALPHA;
137
138 /* fty_alpha.c ends here */