fty_alpha.c 4.3 KB

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