1 /*        $NetBSD: option-value-type.c,v 1.6 2024/08/18 20:47:25 christos Exp $ */
2 
3 /*   -*- buffer-read-only: t -*- vi: set ro:
4  *
5  *  DO NOT EDIT THIS FILE   (stdin.c)
6  *
7  *  It has been AutoGen-ed
8  *  From the definitions    stdin
9  *  and the template file   str2enum
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name ``Bruce Korb'' nor the name of any other
20  *    contributor may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * str2enum IS PROVIDED BY Bruce Korb ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL Bruce Korb OR ANY OTHER CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include "option-value-type.h"
36 /* ANSI-C code produced by gperf version 3.1 */
37 /* Command-line: gperf option-value-type.gp  */
38 /* Computed positions: -k'1' */
39 
40 
41 
42 # if 0 /* gperf build options: */
43 // %struct-type
44 // %language=ANSI-C
45 // %includes
46 // %global-table
47 // %omit-struct-type
48 // %readonly-tables
49 // %compare-strncmp
50 //
51 // %define slot-name               vtp_name
52 // %define hash-function-name      option_value_type_hash
53 // %define lookup-function-name    find_option_value_type_name
54 // %define word-array-name         option_value_type_table
55 // %define initializer-suffix      ,VTP_COUNT_CMD
56 //
57 # endif
58 
59 #include "option-value-type.h"
60 typedef struct {
61     char const *    vtp_name;
62     option_value_type_enum_t vtp_id;
63 } option_value_type_map_t;
64 #include <string.h>
65 
66 /* maximum key range = 15, duplicates = 0 */
67 
68 static unsigned int
option_value_type_hash(register const char * str,register size_t len)69 option_value_type_hash (register const char *str, register size_t len)
70 {
71   static const unsigned char asso_values[] =
72     {
73       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
74       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
75       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
76       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
77       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
78       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
79       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
80       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
81       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
82       18, 18, 18, 18, 18, 18, 18, 18,  0, 18,
83       18, 18, 18, 18,  0, 10, 18,  5, 18, 18,
84        5, 18, 18, 18, 18,  0, 18, 18, 18, 18,
85       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
86       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
87       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
88       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
89       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
90       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
91       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
92       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
93       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
94       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
95       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
96       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
97       18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
98       18, 18, 18, 18, 18, 18
99     };
100   return len + asso_values[(unsigned char)str[0]];
101 }
102 
103 static const option_value_type_map_t option_value_type_table[] =
104   {
105     {"",VTP_COUNT_CMD}, {"",VTP_COUNT_CMD},
106     {"",VTP_COUNT_CMD},
107     {"set",            VTP_CMD_SET},
108     {"bool",           VTP_CMD_BOOL},
109     {"",VTP_COUNT_CMD},
110     {"string",         VTP_CMD_STRING},
111     {"boolean",        VTP_CMD_BOOLEAN},
112     {"",VTP_COUNT_CMD},
113     {"hierarchy",      VTP_CMD_HIERARCHY},
114     {"",VTP_COUNT_CMD},
115     {"nested",         VTP_CMD_NESTED},
116     {"keyword",        VTP_CMD_KEYWORD},
117     {"",VTP_COUNT_CMD},
118     {"set-membership", VTP_CMD_SET_MEMBERSHIP},
119     {"",VTP_COUNT_CMD}, {"",VTP_COUNT_CMD},
120     {"integer",        VTP_CMD_INTEGER}
121   };
122 
123 static inline const option_value_type_map_t *
find_option_value_type_name(register const char * str,register size_t len)124 find_option_value_type_name (register const char *str, register size_t len)
125 {
126   if (len <= 14 && len >= 3)
127     {
128       register unsigned int key = (int)option_value_type_hash (str, len);
129 
130       if (key <= 17)
131         {
132           register const char *s = option_value_type_table[key].vtp_name;
133 
134           if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')
135             return &option_value_type_table[key];
136         }
137     }
138   return 0;
139 }
140 
141 /**
142  * Convert a command (keyword) to a option_value_type_enum_t enumeration value.
143  *
144  * @param[in] str   a string that should start with a known key word.
145  * @param[in] len   the provided length of the keyword at \a str.
146  * @returns the enumeration value.
147  * If not found, that value is VTP_INVALID_CMD.
148  */
149 option_value_type_enum_t
find_option_value_type_cmd(char const * str,size_t len)150 find_option_value_type_cmd(char const * str, size_t len)
151 {
152     option_value_type_map_t const * map;
153 
154     map = find_option_value_type_name(str, (unsigned int)len);
155     return (map == NULL) ? VTP_INVALID_CMD : map->vtp_id;
156 }
157 
158 /* end of option-value-type.c */
159