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