fty_int.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /****************************************************************************
  2. * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /***************************************************************************
  29. * *
  30. * Author : Juergen Pfeifer *
  31. * *
  32. ***************************************************************************/
  33. #include "form.priv.h"
  34. MODULE_ID("$Id: fty_int.c,v 1.22 2007/10/13 19:32:40 tom Exp $")
  35. #if USE_WIDEC_SUPPORT
  36. #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
  37. #else
  38. #define isDigit(c) isdigit(UChar(c))
  39. #endif
  40. #define thisARG integerARG
  41. typedef struct
  42. {
  43. int precision;
  44. long low;
  45. long high;
  46. }
  47. thisARG;
  48. /*---------------------------------------------------------------------------
  49. | Facility : libnform
  50. | Function : static void *Make_This_Type( va_list * ap )
  51. |
  52. | Description : Allocate structure for integer type argument.
  53. |
  54. | Return Values : Pointer to argument structure or NULL on error
  55. +--------------------------------------------------------------------------*/
  56. static void *
  57. Make_This_Type(va_list *ap)
  58. {
  59. thisARG *argp = typeMalloc(thisARG, 1);
  60. if (argp)
  61. {
  62. T((T_CREATE("thisARG %p"), argp));
  63. argp->precision = va_arg(*ap, int);
  64. argp->low = va_arg(*ap, long);
  65. argp->high = va_arg(*ap, long);
  66. }
  67. return (void *)argp;
  68. }
  69. /*---------------------------------------------------------------------------
  70. | Facility : libnform
  71. | Function : static void *Copy_This_Type(const void * argp)
  72. |
  73. | Description : Copy structure for integer type argument.
  74. |
  75. | Return Values : Pointer to argument structure or NULL on error.
  76. +--------------------------------------------------------------------------*/
  77. static void *
  78. Copy_This_Type(const void *argp)
  79. {
  80. const thisARG *ap = (const thisARG *)argp;
  81. thisARG *result = (thisARG *) 0;
  82. if (argp)
  83. {
  84. result = typeMalloc(thisARG, 1);
  85. if (result)
  86. {
  87. T((T_CREATE("thisARG %p"), result));
  88. *result = *ap;
  89. }
  90. }
  91. return (void *)result;
  92. }
  93. /*---------------------------------------------------------------------------
  94. | Facility : libnform
  95. | Function : static void Free_This_Type(void * argp)
  96. |
  97. | Description : Free structure for integer type argument.
  98. |
  99. | Return Values : -
  100. +--------------------------------------------------------------------------*/
  101. static void
  102. Free_This_Type(void *argp)
  103. {
  104. if (argp)
  105. free(argp);
  106. }
  107. /*---------------------------------------------------------------------------
  108. | Facility : libnform
  109. | Function : static bool Check_This_Field(
  110. | FIELD * field,
  111. | const void * argp)
  112. |
  113. | Description : Validate buffer content to be a valid integer value
  114. |
  115. | Return Values : TRUE - field is valid
  116. | FALSE - field is invalid
  117. +--------------------------------------------------------------------------*/
  118. static bool
  119. Check_This_Field(FIELD *field, const void *argp)
  120. {
  121. const thisARG *argi = (const thisARG *)argp;
  122. long low = argi->low;
  123. long high = argi->high;
  124. int prec = argi->precision;
  125. unsigned char *bp = (unsigned char *)field_buffer(field, 0);
  126. char *s = (char *)bp;
  127. long val;
  128. char buf[100];
  129. bool result = FALSE;
  130. while (*bp && *bp == ' ')
  131. bp++;
  132. if (*bp)
  133. {
  134. if (*bp == '-')
  135. bp++;
  136. #if USE_WIDEC_SUPPORT
  137. if (*bp)
  138. {
  139. bool blank = FALSE;
  140. int len;
  141. int n;
  142. wchar_t *list = _nc_Widen_String((char *)bp, &len);
  143. if (list != 0)
  144. {
  145. result = TRUE;
  146. for (n = 0; n < len; ++n)
  147. {
  148. if (blank)
  149. {
  150. if (list[n] != ' ')
  151. {
  152. result = FALSE;
  153. break;
  154. }
  155. }
  156. else if (list[n] == ' ')
  157. {
  158. blank = TRUE;
  159. }
  160. else if (!isDigit(list[n]))
  161. {
  162. result = FALSE;
  163. break;
  164. }
  165. }
  166. free(list);
  167. }
  168. }
  169. #else
  170. while (*bp)
  171. {
  172. if (!isdigit(UChar(*bp)))
  173. break;
  174. bp++;
  175. }
  176. while (*bp && *bp == ' ')
  177. bp++;
  178. result = (*bp == '\0');
  179. #endif
  180. if (result)
  181. {
  182. val = atol(s);
  183. if (low < high)
  184. {
  185. if (val < low || val > high)
  186. result = FALSE;
  187. }
  188. if (result)
  189. {
  190. sprintf(buf, "%.*ld", (prec > 0 ? prec : 0), val);
  191. set_field_buffer(field, 0, buf);
  192. }
  193. }
  194. }
  195. return (result);
  196. }
  197. /*---------------------------------------------------------------------------
  198. | Facility : libnform
  199. | Function : static bool Check_This_Character(
  200. | int c,
  201. | const void * argp)
  202. |
  203. | Description : Check a character for the integer type.
  204. |
  205. | Return Values : TRUE - character is valid
  206. | FALSE - character is invalid
  207. +--------------------------------------------------------------------------*/
  208. static bool
  209. Check_This_Character(int c, const void *argp GCC_UNUSED)
  210. {
  211. return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
  212. }
  213. static FIELDTYPE typeTHIS =
  214. {
  215. _HAS_ARGS | _RESIDENT,
  216. 1, /* this is mutable, so we can't be const */
  217. (FIELDTYPE *)0,
  218. (FIELDTYPE *)0,
  219. Make_This_Type,
  220. Copy_This_Type,
  221. Free_This_Type,
  222. Check_This_Field,
  223. Check_This_Character,
  224. NULL,
  225. NULL
  226. };
  227. NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeTHIS;
  228. /* fty_int.c ends here */