Annotation of embedaddon/libpdel/structs/type/structs_type_boolean.c, revision 1.1.1.1
1.1 misho 1:
2: /*
3: * Copyright (c) 2001-2002 Packet Design, LLC.
4: * All rights reserved.
5: *
6: * Subject to the following obligations and disclaimer of warranty,
7: * use and redistribution of this software, in source or object code
8: * forms, with or without modifications are expressly permitted by
9: * Packet Design; provided, however, that:
10: *
11: * (i) Any and all reproductions of the source or object code
12: * must include the copyright notice above and the following
13: * disclaimer of warranties; and
14: * (ii) No rights are granted, in any manner or form, to use
15: * Packet Design trademarks, including the mark "PACKET DESIGN"
16: * on advertising, endorsements, or otherwise except as such
17: * appears in the above copyright notice or in the software.
18: *
19: * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
20: * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
21: * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
22: * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
23: * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
24: * OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
25: * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
26: * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
27: * RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE
28: * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
29: * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
30: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
31: * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
32: * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
33: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35: * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
36: * THE POSSIBILITY OF SUCH DAMAGE.
37: *
38: * Author: Archie Cobbs <archie@freebsd.org>
39: */
40:
41: #include <sys/types.h>
42:
43: #include <stdio.h>
44: #include <stdlib.h>
45: #include <stdarg.h>
46: #include <string.h>
47: #include <ctype.h>
48: #include <errno.h>
49:
50: #include "structs/structs.h"
51: #include "structs/type/array.h"
52: #include "structs/type/boolean.h"
53: #include "util/typed_mem.h"
54:
55: /*********************************************************************
56: BOOLEAN TYPES
57: *********************************************************************/
58:
59: /*
60: * Boolean types
61: *
62: * Type-specific arguments:
63: * [int]
64: * 0 = stored in a char
65: * 1 = stored in an int
66: * [int]
67: * 0 = True/False
68: * 1 = Yes/No
69: * 2 = On/Off
70: * 3 = Enabled/Disabled
71: * 4 = 1/0
72: */
73:
74: /* Macro for defining a boolean type */
75: #define STRUCTS_BOOLEAN_TYPE(type, arg1, arg2) { \
76: sizeof(type), \
77: "boolean", \
78: STRUCTS_TYPE_PRIMITIVE, \
79: structs_region_init, \
80: structs_region_copy, \
81: structs_region_equal, \
82: structs_boolean_ascify, \
83: structs_boolean_binify, \
84: structs_region_encode_netorder, \
85: structs_region_decode_netorder, \
86: structs_nothing_free, \
87: { { (void *)arg1 }, { (void *)arg2 } } \
88: }
89:
90: /* ASCII possibilities (not all are used yet) */
91: static const char *boolean_strings[][2] = {
92: { "False", "True" },
93: { "No", "Yes" },
94: { "Off", "On" },
95: { "Disabled", "Enabled" },
96: { "0", "1" },
97: { NULL, NULL }
98: };
99:
100: /* Methods */
101: static structs_ascify_t structs_boolean_ascify;
102: static structs_binify_t structs_boolean_binify;
103:
104: /* Pre-defined types */
105: const struct structs_type structs_type_boolean_char
106: = STRUCTS_BOOLEAN_TYPE(char, 0, 0);
107: const struct structs_type structs_type_boolean_int
108: = STRUCTS_BOOLEAN_TYPE(int, 1, 0);
109: const struct structs_type structs_type_boolean_char_01
110: = STRUCTS_BOOLEAN_TYPE(char, 0, 4);
111: const struct structs_type structs_type_boolean_int_01
112: = STRUCTS_BOOLEAN_TYPE(int, 1, 4);
113:
114: static char *
115: structs_boolean_ascify(const struct structs_type *type,
116: const char *mtype, const void *data)
117: {
118: int truth;
119:
120: truth = type->args[0].i ? *((u_int *)data) : *((u_char *)data);
121: return (STRDUP(mtype, boolean_strings[type->args[1].i][!!truth]));
122: }
123:
124: static int
125: structs_boolean_binify(const struct structs_type *type,
126: const char *ascii, void *data, char *ebuf, size_t emax)
127: {
128: int truth = 0;
129: char buf[64];
130: int i;
131:
132: /* Trim whitespace */
133: while (isspace(*ascii))
134: ascii++;
135: for (i = strlen(ascii); i > 0 && isspace(ascii[i - 1]); i--);
136: if (i > sizeof(buf) - 1)
137: i = sizeof(buf) - 1;
138: strncpy(buf, ascii, i);
139: buf[i] = '\0';
140:
141: /* Compare value */
142: for (i = 0; boolean_strings[i][0] != NULL; i++) {
143: if (strcasecmp(buf, boolean_strings[i][0]) == 0) {
144: truth = 0;
145: break;
146: } else if (strcasecmp(buf, boolean_strings[i][1]) == 0) {
147: truth = 1;
148: break;
149: }
150: }
151: if (boolean_strings[i][0] == NULL) {
152: strlcpy(ebuf, "invalid Boolean value", emax);
153: errno = EINVAL;
154: return (-1);
155: }
156: if (type->args[0].i)
157: *((u_int *)data) = truth;
158: else
159: *((u_char *)data) = truth;
160: return (0);
161: }
162:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>