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