1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /***************************************************************************
31 * Author : Juergen Pfeifer *
33 ***************************************************************************/
35 #include "form.priv.h"
37 MODULE_ID("$Id: fty_enum.c,v 1.26 2010/05/01 21:11:07 tom Exp $")
56 /*---------------------------------------------------------------------------
58 | Function : static void *Generic_Enum_Type(void * arg)
60 | Description : Allocate structure for enumeration type argument.
62 | Return Values : Pointer to argument structure or NULL on error
63 +--------------------------------------------------------------------------*/
65 Generic_Enum_Type(void *arg
)
67 enumARG
*argp
= (enumARG
*)0;
68 enumParams
*params
= (enumParams
*) arg
;
72 argp
= typeMalloc(enumARG
, 1);
77 char **kp
= (char **)0;
78 char **kwds
= (char **)0;
82 T((T_CREATE("enumARG %p"), (void *)argp
));
84 ccase
= params
->ccase
;
85 cunique
= params
->cunique
;
87 argp
->checkcase
= ccase
? TRUE
: FALSE
;
88 argp
->checkunique
= cunique
? TRUE
: FALSE
;
89 argp
->kwds
= (char **)0;
98 /* We copy the keywords, because we can't rely on the fact
99 that the caller doesn't relocate or free the memory used
100 for the keywords (maybe he has GC)
102 argp
->kwds
= typeMalloc(char *, cnt
+ 1);
105 if ((kptarget
= argp
->kwds
) != 0)
109 (*kptarget
++) = strdup(*kp
++);
111 *kptarget
= (char *)0;
119 /*---------------------------------------------------------------------------
120 | Facility : libnform
121 | Function : static void *Make_Enum_Type( va_list * ap )
123 | Description : Allocate structure for enumeration type argument.
125 | Return Values : Pointer to argument structure or NULL on error
126 +--------------------------------------------------------------------------*/
128 Make_Enum_Type(va_list *ap
)
132 params
.kwds
= va_arg(*ap
, char **);
133 params
.ccase
= va_arg(*ap
, int);
134 params
.cunique
= va_arg(*ap
, int);
136 return Generic_Enum_Type((void *)¶ms
);
139 /*---------------------------------------------------------------------------
140 | Facility : libnform
141 | Function : static void *Copy_Enum_Type( const void * argp )
143 | Description : Copy structure for enumeration type argument.
145 | Return Values : Pointer to argument structure or NULL on error.
146 +--------------------------------------------------------------------------*/
148 Copy_Enum_Type(const void *argp
)
150 enumARG
*result
= (enumARG
*)0;
154 const enumARG
*ap
= (const enumARG
*)argp
;
156 result
= typeMalloc(enumARG
, 1);
160 T((T_CREATE("enumARG %p"), (void *)result
));
166 char **kp
= ap
->kwds
;
167 result
->kwds
= typeMalloc(char *, 1 + ap
->count
);
169 if ((kptarget
= result
->kwds
) != 0)
173 (*kptarget
++) = strdup(*kp
++);
175 *kptarget
= (char *)0;
180 return (void *)result
;
183 /*---------------------------------------------------------------------------
184 | Facility : libnform
185 | Function : static void Free_Enum_Type( void * argp )
187 | Description : Free structure for enumeration type argument.
190 +--------------------------------------------------------------------------*/
192 Free_Enum_Type(void *argp
)
196 const enumARG
*ap
= (const enumARG
*)argp
;
198 if (ap
->kwds
&& ap
->count
> 0)
200 char **kp
= ap
->kwds
;
208 assert(cnt
== ap
->count
);
215 #define SKIP_SPACE(x) while(((*(x))!='\0') && (is_blank(*(x)))) (x)++
220 /*---------------------------------------------------------------------------
221 | Facility : libnform
222 | Function : static int Compare(const unsigned char * s,
223 | const unsigned char * buf,
226 | Description : Check whether or not the text in 'buf' matches the
227 | text in 's', at least partial.
229 | Return Values : NOMATCH - buffer doesn't match
230 | PARTIAL - buffer matches partially
231 | EXACT - buffer matches exactly
232 +--------------------------------------------------------------------------*/
234 Compare(const unsigned char *s
, const unsigned char *buf
,
237 SKIP_SPACE(buf
); /* Skip leading spaces in both texts */
242 return (((*s
) != '\0') ? NOMATCH
: EXACT
);
256 while (toupper(*s
++) == toupper(*buf
))
263 /* At this location buf points to the first character where it no longer
264 matches with s. So if only blanks are following, we have a partial
265 match otherwise there is no match */
270 /* If it happens that the reference buffer is at its end, the partial
271 match is actually an exact match. */
272 return ((s
[-1] != '\0') ? PARTIAL
: EXACT
);
275 /*---------------------------------------------------------------------------
276 | Facility : libnform
277 | Function : static bool Check_Enum_Field(
281 | Description : Validate buffer content to be a valid enumeration value
283 | Return Values : TRUE - field is valid
284 | FALSE - field is invalid
285 +--------------------------------------------------------------------------*/
287 Check_Enum_Field(FIELD
*field
, const void *argp
)
289 char **kwds
= ((const enumARG
*)argp
)->kwds
;
290 bool ccase
= ((const enumARG
*)argp
)->checkcase
;
291 bool unique
= ((const enumARG
*)argp
)->checkunique
;
292 unsigned char *bp
= (unsigned char *)field_buffer(field
, 0);
296 while (kwds
&& (s
= (*kwds
++)))
298 if ((res
= Compare((unsigned char *)s
, bp
, ccase
)) != NOMATCH
)
300 p
= t
= s
; /* t is at least a partial match */
301 if ((unique
&& res
!= EXACT
))
303 while (kwds
&& (p
= *kwds
++))
305 if ((res
= Compare((unsigned char *)p
, bp
, ccase
)) != NOMATCH
)
319 set_field_buffer(field
, 0, t
);
329 static const char *dummy
[] =
332 /*---------------------------------------------------------------------------
333 | Facility : libnform
334 | Function : static bool Next_Enum(FIELD * field,
337 | Description : Check for the next enumeration value
339 | Return Values : TRUE - next value found and loaded
340 | FALSE - no next value loaded
341 +--------------------------------------------------------------------------*/
343 Next_Enum(FIELD
*field
, const void *argp
)
345 const enumARG
*args
= (const enumARG
*)argp
;
346 char **kwds
= args
->kwds
;
347 bool ccase
= args
->checkcase
;
348 int cnt
= args
->count
;
349 unsigned char *bp
= (unsigned char *)field_buffer(field
, 0);
355 if (Compare((unsigned char *)(*kwds
++), bp
, ccase
) == EXACT
)
360 if ((cnt
>= 0) || (Compare((const unsigned char *)dummy
, bp
, ccase
) == EXACT
))
362 set_field_buffer(field
, 0, *kwds
);
369 /*---------------------------------------------------------------------------
370 | Facility : libnform
371 | Function : static bool Previous_Enum(
375 | Description : Check for the previous enumeration value
377 | Return Values : TRUE - previous value found and loaded
378 | FALSE - no previous value loaded
379 +--------------------------------------------------------------------------*/
381 Previous_Enum(FIELD
*field
, const void *argp
)
383 const enumARG
*args
= (const enumARG
*)argp
;
384 int cnt
= args
->count
;
385 char **kwds
= &args
->kwds
[cnt
- 1];
386 bool ccase
= args
->checkcase
;
387 unsigned char *bp
= (unsigned char *)field_buffer(field
, 0);
393 if (Compare((unsigned char *)(*kwds
--), bp
, ccase
) == EXACT
)
398 kwds
= &args
->kwds
[args
->count
- 1];
400 if ((cnt
>= 0) || (Compare((const unsigned char *)dummy
, bp
, ccase
) == EXACT
))
402 set_field_buffer(field
, 0, *kwds
);
409 static FIELDTYPE typeENUM
=
411 _HAS_ARGS
| _HAS_CHOICE
| _RESIDENT
,
412 1, /* this is mutable, so we can't be const */
418 INIT_FT_FUNC(Check_Enum_Field
),
420 INIT_FT_FUNC(Next_Enum
),
421 INIT_FT_FUNC(Previous_Enum
),
422 #if NCURSES_INTEROP_FUNCS
427 NCURSES_EXPORT_VAR(FIELDTYPE
*)
428 TYPE_ENUM
= &typeENUM
;
430 #if NCURSES_INTEROP_FUNCS
431 /* The next routines are to simplify the use of ncurses from
432 programming languages with restictions on interop with C level
433 constructs (e.g. variable access or va_list + ellipsis constructs)
435 NCURSES_EXPORT(FIELDTYPE
*)
442 /* fty_enum.c ends here */