fty_int.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 precision;
  16. long low;
  17. long high;
  18. } integerARG;
  19. /*---------------------------------------------------------------------------
  20. | Facility : libnform
  21. | Function : static void *Make_Integer_Type( va_list * ap )
  22. |
  23. | Description : Allocate structure for integer type argument.
  24. |
  25. | Return Values : Pointer to argument structure or NULL on error
  26. +--------------------------------------------------------------------------*/
  27. static void *Make_Integer_Type(va_list * ap)
  28. {
  29. integerARG *argp = (integerARG *)malloc(sizeof(integerARG));
  30. if (argp)
  31. {
  32. argp->precision = va_arg(*ap,int);
  33. argp->low = va_arg(*ap,long);
  34. argp->high = va_arg(*ap,long);
  35. }
  36. return (void *)argp;
  37. }
  38. /*---------------------------------------------------------------------------
  39. | Facility : libnform
  40. | Function : static void *Copy_Integer_Type(const void * argp)
  41. |
  42. | Description : Copy structure for integer type argument.
  43. |
  44. | Return Values : Pointer to argument structure or NULL on error.
  45. +--------------------------------------------------------------------------*/
  46. static void *Copy_Integer_Type(const void * argp)
  47. {
  48. const integerARG *ap = (const integerARG *)argp;
  49. integerARG *result = (integerARG *)0;
  50. if (argp)
  51. {
  52. result = (integerARG *)malloc(sizeof(integerARG));
  53. if (result)
  54. *result = *ap;
  55. }
  56. return (void *)result;
  57. }
  58. /*---------------------------------------------------------------------------
  59. | Facility : libnform
  60. | Function : static void Free_Integer_Type(void * argp)
  61. |
  62. | Description : Free structure for integer type argument.
  63. |
  64. | Return Values : -
  65. +--------------------------------------------------------------------------*/
  66. static void Free_Integer_Type(void * argp)
  67. {
  68. if (argp)
  69. free(argp);
  70. }
  71. /*---------------------------------------------------------------------------
  72. | Facility : libnform
  73. | Function : static bool Check_Integer_Field(
  74. | FIELD * field,
  75. | const void * argp)
  76. |
  77. | Description : Validate buffer content to be a valid integer value
  78. |
  79. | Return Values : TRUE - field is valid
  80. | FALSE - field is invalid
  81. +--------------------------------------------------------------------------*/
  82. static bool Check_Integer_Field(FIELD * field, const void * argp)
  83. {
  84. const integerARG *argi = (const integerARG *)argp;
  85. long low = argi->low;
  86. long high = argi->high;
  87. int prec = argi->precision;
  88. unsigned char *bp = (unsigned char *)field_buffer(field,0);
  89. char *s = (char *)bp;
  90. long val;
  91. char buf[100];
  92. while( *bp && *bp==' ') bp++;
  93. if (*bp)
  94. {
  95. if (*bp=='-') bp++;
  96. while (*bp)
  97. {
  98. if (!isdigit(*bp)) break;
  99. bp++;
  100. }
  101. while(*bp && *bp==' ') bp++;
  102. if (*bp=='\0')
  103. {
  104. val = atol(s);
  105. if (low<high)
  106. {
  107. if (val<low || val>high) return FALSE;
  108. }
  109. sprintf(buf,"%.*ld",(prec>0?prec:0),val);
  110. set_field_buffer(field,0,buf);
  111. return TRUE;
  112. }
  113. }
  114. return FALSE;
  115. }
  116. /*---------------------------------------------------------------------------
  117. | Facility : libnform
  118. | Function : static bool Check_Integer_Character(
  119. | int c,
  120. | const void * argp)
  121. |
  122. | Description : Check a character for the integer type.
  123. |
  124. | Return Values : TRUE - character is valid
  125. | FALSE - character is invalid
  126. +--------------------------------------------------------------------------*/
  127. static bool Check_Integer_Character(int c, const void * argp)
  128. {
  129. argp=0; /* Silence unused parameter warning. */
  130. return ((isdigit(c) || (c=='-')) ? TRUE : FALSE);
  131. }
  132. static FIELDTYPE typeINTEGER = {
  133. _HAS_ARGS | _RESIDENT,
  134. 1, /* this is mutable, so we can't be const */
  135. (FIELDTYPE *)0,
  136. (FIELDTYPE *)0,
  137. Make_Integer_Type,
  138. Copy_Integer_Type,
  139. Free_Integer_Type,
  140. Check_Integer_Field,
  141. Check_Integer_Character,
  142. NULL,
  143. NULL
  144. };
  145. FIELDTYPE* TYPE_INTEGER = &typeINTEGER;
  146. /* fty_int.c ends here */