fty_enum.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. char **kwds;
  16. int count;
  17. bool checkcase;
  18. bool checkunique;
  19. } enumARG;
  20. /*---------------------------------------------------------------------------
  21. | Facility : libnform
  22. | Function : static void *Make_Enum_Type( va_list * ap )
  23. |
  24. | Description : Allocate structure for enumeration type argument.
  25. |
  26. | Return Values : Pointer to argument structure or NULL on error
  27. +--------------------------------------------------------------------------*/
  28. static void *Make_Enum_Type(va_list * ap)
  29. {
  30. enumARG *argp = (enumARG *)malloc(sizeof(enumARG));
  31. if (argp)
  32. {
  33. int cnt = 0;
  34. char **kp = (char **)0;
  35. int ccase, cunique;
  36. argp->kwds = va_arg(*ap,char **);
  37. ccase = va_arg(*ap,int);
  38. cunique = va_arg(*ap,int);
  39. argp->checkcase = ccase ? TRUE : FALSE;
  40. argp->checkunique = cunique ? TRUE : FALSE;
  41. kp = argp->kwds;
  42. while( kp && (*kp++) ) cnt++;
  43. argp->count = cnt;
  44. }
  45. return (void *)argp;
  46. }
  47. /*---------------------------------------------------------------------------
  48. | Facility : libnform
  49. | Function : static void *Copy_Enum_Type( const void * argp )
  50. |
  51. | Description : Copy structure for enumeration type argument.
  52. |
  53. | Return Values : Pointer to argument structure or NULL on error.
  54. +--------------------------------------------------------------------------*/
  55. static void *Copy_Enum_Type(const void * argp)
  56. {
  57. enumARG *result = (enumARG *)0;
  58. if (argp)
  59. {
  60. const enumARG *ap = (const enumARG *)argp;
  61. result = (enumARG *)malloc(sizeof(enumARG));
  62. if (result)
  63. *result = *ap;
  64. }
  65. return (void *)result;
  66. }
  67. /*---------------------------------------------------------------------------
  68. | Facility : libnform
  69. | Function : static void Free_Enum_Type( void * argp )
  70. |
  71. | Description : Free structure for enumeration type argument.
  72. |
  73. | Return Values : -
  74. +--------------------------------------------------------------------------*/
  75. static void Free_Enum_Type(void * argp)
  76. {
  77. if (argp)
  78. free(argp);
  79. }
  80. #define SKIP_SPACE(x) while(((*(x))!='\0') && (is_blank(*(x)))) (x)++
  81. #define NOMATCH 0
  82. #define PARTIAL 1
  83. #define EXACT 2
  84. /*---------------------------------------------------------------------------
  85. | Facility : libnform
  86. | Function : static int Compare(const unsigned char * s,
  87. | const unsigned char * buf,
  88. | bool ccase )
  89. |
  90. | Description : Check whether or not the text in 'buf' matches the
  91. | text in 's', at least partial.
  92. |
  93. | Return Values : NOMATCH - buffer doesn't match
  94. | PARTIAL - buffer matches partially
  95. | EXACT - buffer matches exactly
  96. +--------------------------------------------------------------------------*/
  97. static int Compare(const unsigned char *s, const unsigned char *buf,
  98. bool ccase)
  99. {
  100. SKIP_SPACE(buf); /* Skip leading spaces in both texts */
  101. SKIP_SPACE(s);
  102. if (*buf=='\0')
  103. {
  104. return (((*s)!='\0') ? NOMATCH : EXACT);
  105. }
  106. else
  107. {
  108. if (ccase)
  109. {
  110. while(*s++ == *buf)
  111. {
  112. if (*buf++=='\0') return EXACT;
  113. }
  114. }
  115. else
  116. {
  117. while(toupper(*s++)==toupper(*buf))
  118. {
  119. if (*buf++=='\0') return EXACT;
  120. }
  121. }
  122. }
  123. /* At this location buf points to the first character where it no longer
  124. matches with s. So if only blanks are following, we have a partial
  125. match otherwise there is no match */
  126. SKIP_SPACE(buf);
  127. if (*buf)
  128. return NOMATCH;
  129. /* If it happens that the reference buffer is at its end, the partial
  130. match is actually an exact match. */
  131. return ((s[-1]!='\0') ? PARTIAL : EXACT);
  132. }
  133. /*---------------------------------------------------------------------------
  134. | Facility : libnform
  135. | Function : static bool Check_Enum_Field(
  136. | FIELD * field,
  137. | const void * argp)
  138. |
  139. | Description : Validate buffer content to be a valid enumeration value
  140. |
  141. | Return Values : TRUE - field is valid
  142. | FALSE - field is invalid
  143. +--------------------------------------------------------------------------*/
  144. static bool Check_Enum_Field(FIELD * field, const void * argp)
  145. {
  146. char **kwds = ((const enumARG *)argp)->kwds;
  147. bool ccase = ((const enumARG *)argp)->checkcase;
  148. bool unique = ((const enumARG *)argp)->checkunique;
  149. unsigned char *bp = (unsigned char *)field_buffer(field,0);
  150. char *s, *t, *p;
  151. int res;
  152. while( kwds && (s=(*kwds++)) )
  153. {
  154. if ((res=Compare((unsigned char *)s,bp,ccase))!=NOMATCH)
  155. {
  156. p=t=s; /* t is at least a partial match */
  157. if ((unique && res!=EXACT))
  158. {
  159. while( kwds && (p = *kwds++) )
  160. {
  161. if ((res=Compare((unsigned char *)p,bp,ccase))!=NOMATCH)
  162. {
  163. if (res==EXACT)
  164. {
  165. t = p;
  166. break;
  167. }
  168. else
  169. t = (char *)0;
  170. }
  171. }
  172. }
  173. if (t)
  174. {
  175. set_field_buffer(field,0,t);
  176. return TRUE;
  177. }
  178. if (!p)
  179. break;
  180. }
  181. }
  182. return FALSE;
  183. }
  184. static const char *dummy[] = { (char *)0 };
  185. /*---------------------------------------------------------------------------
  186. | Facility : libnform
  187. | Function : static bool Next_Enum(FIELD * field,
  188. | const void * argp)
  189. |
  190. | Description : Check for the next enumeration value
  191. |
  192. | Return Values : TRUE - next value found and loaded
  193. | FALSE - no next value loaded
  194. +--------------------------------------------------------------------------*/
  195. static bool Next_Enum(FIELD * field, const void * argp)
  196. {
  197. const enumARG *args = (const enumARG *)argp;
  198. char **kwds = args->kwds;
  199. bool ccase = args->checkcase;
  200. int cnt = args->count;
  201. unsigned char *bp = (unsigned char *)field_buffer(field,0);
  202. if (kwds) {
  203. while(cnt--)
  204. {
  205. if (Compare((unsigned char *)(*kwds++),bp,ccase)==EXACT)
  206. break;
  207. }
  208. if (cnt<=0)
  209. kwds = args->kwds;
  210. if ((cnt>=0) || (Compare((const unsigned char *)dummy,bp,ccase)==EXACT))
  211. {
  212. set_field_buffer(field,0,*kwds);
  213. return TRUE;
  214. }
  215. }
  216. return FALSE;
  217. }
  218. /*---------------------------------------------------------------------------
  219. | Facility : libnform
  220. | Function : static bool Previous_Enum(
  221. | FIELD * field,
  222. | const void * argp)
  223. |
  224. | Description : Check for the previous enumeration value
  225. |
  226. | Return Values : TRUE - previous value found and loaded
  227. | FALSE - no previous value loaded
  228. +--------------------------------------------------------------------------*/
  229. static bool Previous_Enum(FIELD * field, const void * argp)
  230. {
  231. const enumARG *args = (const enumARG *)argp;
  232. int cnt = args->count;
  233. char **kwds = &args->kwds[cnt-1];
  234. bool ccase = args->checkcase;
  235. unsigned char *bp = (unsigned char *)field_buffer(field,0);
  236. if (kwds) {
  237. while(cnt--)
  238. {
  239. if (Compare((unsigned char *)(*kwds--),bp,ccase)==EXACT)
  240. break;
  241. }
  242. if (cnt<=0)
  243. kwds = &args->kwds[args->count-1];
  244. if ((cnt>=0) || (Compare((const unsigned char *)dummy,bp,ccase)==EXACT))
  245. {
  246. set_field_buffer(field,0,*kwds);
  247. return TRUE;
  248. }
  249. }
  250. return FALSE;
  251. }
  252. static FIELDTYPE typeENUM = {
  253. _HAS_ARGS | _HAS_CHOICE | _RESIDENT,
  254. 1, /* this is mutable, so we can't be const */
  255. (FIELDTYPE *)0,
  256. (FIELDTYPE *)0,
  257. Make_Enum_Type,
  258. Copy_Enum_Type,
  259. Free_Enum_Type,
  260. Check_Enum_Field,
  261. NULL,
  262. Next_Enum,
  263. Previous_Enum
  264. };
  265. FIELDTYPE* TYPE_ENUM = &typeENUM;
  266. /* fty_enum.c ends here */