fty_alnum.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
  3. * You may freely copy it for use as a template for your own field types.
  4. * If you develop a field type that might be of general use, please send
  5. * it back to the ncurses maintainers for inclusion in the next version.
  6. */
  7. /***************************************************************************
  8. * *
  9. * Author : Juergen Pfeifer, juergen.pfeifer@gmx.net *
  10. * *
  11. ***************************************************************************/
  12. #include "form.priv.h"
  13. MODULE_ID("$Id$")
  14. typedef struct {
  15. int width;
  16. } alnumARG;
  17. /*---------------------------------------------------------------------------
  18. | Facility : libnform
  19. | Function : static void *Make_AlphaNumeric_Type(va_list *ap)
  20. |
  21. | Description : Allocate structure for alphanumeric type argument.
  22. |
  23. | Return Values : Pointer to argument structure or NULL on error
  24. +--------------------------------------------------------------------------*/
  25. static void *Make_AlphaNumeric_Type(va_list * ap)
  26. {
  27. alnumARG *argp = (alnumARG *)malloc(sizeof(alnumARG));
  28. if (argp)
  29. argp->width = va_arg(*ap,int);
  30. return ((void *)argp);
  31. }
  32. /*---------------------------------------------------------------------------
  33. | Facility : libnform
  34. | Function : static void *Copy_AlphaNumericType(const void *argp)
  35. |
  36. | Description : Copy structure for alphanumeric type argument.
  37. |
  38. | Return Values : Pointer to argument structure or NULL on error.
  39. +--------------------------------------------------------------------------*/
  40. static void *Copy_AlphaNumeric_Type(const void *argp)
  41. {
  42. const alnumARG *ap = (const alnumARG *)argp;
  43. alnumARG *result = (alnumARG *)malloc(sizeof(alnumARG));
  44. if (result)
  45. *result = *ap;
  46. return ((void *)result);
  47. }
  48. /*---------------------------------------------------------------------------
  49. | Facility : libnform
  50. | Function : static void Free_AlphaNumeric_Type(void *argp)
  51. |
  52. | Description : Free structure for alphanumeric type argument.
  53. |
  54. | Return Values : -
  55. +--------------------------------------------------------------------------*/
  56. static void Free_AlphaNumeric_Type(void * argp)
  57. {
  58. if (argp)
  59. free(argp);
  60. }
  61. /*---------------------------------------------------------------------------
  62. | Facility : libnform
  63. | Function : static bool Check_AlphaNumeric_Field(
  64. | FIELD *field,
  65. | const void *argp)
  66. |
  67. | Description : Validate buffer content to be a valid alphanumeric value
  68. |
  69. | Return Values : TRUE - field is valid
  70. | FALSE - field is invalid
  71. +--------------------------------------------------------------------------*/
  72. static bool Check_AlphaNumeric_Field(FIELD * field, const void * argp)
  73. {
  74. int width = ((const alnumARG *)argp)->width;
  75. unsigned char *bp = (unsigned char *)field_buffer(field,0);
  76. int l = -1;
  77. unsigned char *s;
  78. while(*bp && *bp==' ')
  79. bp++;
  80. if (*bp)
  81. {
  82. s = bp;
  83. while(*bp && isalnum(*bp))
  84. bp++;
  85. l = (int)(bp-s);
  86. while(*bp && *bp==' ')
  87. bp++;
  88. }
  89. return ((*bp || (l < width)) ? FALSE : TRUE);
  90. }
  91. /*---------------------------------------------------------------------------
  92. | Facility : libnform
  93. | Function : static bool Check_AlphaNumeric_Character(
  94. | int c,
  95. | const void *argp )
  96. |
  97. | Description : Check a character for the alphanumeric type.
  98. |
  99. | Return Values : TRUE - character is valid
  100. | FALSE - character is invalid
  101. +--------------------------------------------------------------------------*/
  102. static bool Check_AlphaNumeric_Character(int c, const void * argp)
  103. {
  104. argp=0; /* Silence unused parameter warning. */
  105. return (isalnum(c) ? TRUE : FALSE);
  106. }
  107. static FIELDTYPE typeALNUM = {
  108. _HAS_ARGS | _RESIDENT,
  109. 1, /* this is mutable, so we can't be const */
  110. (FIELDTYPE *)0,
  111. (FIELDTYPE *)0,
  112. Make_AlphaNumeric_Type,
  113. Copy_AlphaNumeric_Type,
  114. Free_AlphaNumeric_Type,
  115. Check_AlphaNumeric_Field,
  116. Check_AlphaNumeric_Character,
  117. NULL,
  118. NULL
  119. };
  120. FIELDTYPE* TYPE_ALNUM = &typeALNUM;
  121. /* fty_alnum.c ends here */