Home | History | Annotate | Line # | Download | only in libopts
      1 /*	$NetBSD: option-xat-attribute.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-xat-attribute.h"
     36 /* ANSI-C code produced by gperf version 3.1 */
     37 /* Command-line: gperf option-xat-attribute.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               xat_name
     52 // %define hash-function-name      option_xat_attribute_hash
     53 // %define lookup-function-name    find_option_xat_attribute_name
     54 // %define word-array-name         option_xat_attribute_table
     55 // %define initializer-suffix      ,XAT_COUNT_CMD
     56 //
     57 # endif
     58 
     59 #include "option-xat-attribute.h"
     60 typedef struct {
     61     char const *    xat_name;
     62     option_xat_attribute_enum_t xat_id;
     63 } option_xat_attribute_map_t;
     64 #include <string.h>
     65 
     66 /* maximum key range = 6, duplicates = 0 */
     67 
     68 static unsigned int
     69 option_xat_attribute_hash (register const char *str, register size_t len)
     70 {
     71   static const unsigned char asso_values[] =
     72     {
     73      10,10,10,10,10,10,10,10,10,10,
     74      10,10,10,10,10,10,10,10,10,10,
     75      10,10,10,10,10,10,10,10,10,10,
     76      10,10,10,10,10,10,10,10,10,10,
     77      10,10,10,10,10,10,10,10,10,10,
     78      10,10,10,10,10,10,10,10,10,10,
     79      10,10,10,10,10,10,10,10,10,10,
     80      10,10,10,10,10,10,10,10,10,10,
     81      10,10,10,10,10,10,10,10,10,10,
     82      10,10,10,10,10,10,10,10,10, 0,
     83      10,10,10,10,10,10,10, 5,10, 0,
     84      10,10,10,10,10,10, 0, 0,10, 0,
     85      10,10,10,10,10,10,10,10,10,10,
     86      10,10,10,10,10,10,10,10,10,10,
     87      10,10,10,10,10,10,10,10,10,10,
     88      10,10,10,10,10,10,10,10,10,10,
     89      10,10,10,10,10,10,10,10,10,10,
     90      10,10,10,10,10,10,10,10,10,10,
     91      10,10,10,10,10,10,10,10,10,10,
     92      10,10,10,10,10,10,10,10,10,10,
     93      10,10,10,10,10,10,10,10,10,10,
     94      10,10,10,10,10,10,10,10,10,10,
     95      10,10,10,10,10,10,10,10,10,10,
     96      10,10,10,10,10,10,10,10,10,10,
     97      10,10,10,10,10,10,10,10,10,10,
     98      10,10,10,10,10,10
     99     };
    100   return len + asso_values[(unsigned char)str[0]];
    101 }
    102 
    103 static const option_xat_attribute_map_t option_xat_attribute_table[] =
    104   {
    105     {"",XAT_COUNT_CMD}, {"",XAT_COUNT_CMD},
    106     {"",XAT_COUNT_CMD}, {"",XAT_COUNT_CMD},
    107     {"type",     XAT_CMD_TYPE},
    108     {"words",    XAT_CMD_WORDS},
    109     {"cooked",   XAT_CMD_COOKED},
    110     {"members",  XAT_CMD_MEMBERS},
    111     {"uncooked", XAT_CMD_UNCOOKED},
    112     {"keep",     XAT_CMD_KEEP}
    113   };
    114 
    115 static inline const option_xat_attribute_map_t *
    116 find_option_xat_attribute_name (register const char *str, register size_t len)
    117 {
    118   if (len <= 8 && len >= 4)
    119     {
    120       register unsigned int key = (int)option_xat_attribute_hash (str, len);
    121 
    122       if (key <= 9)
    123         {
    124           register const char *s = option_xat_attribute_table[key].xat_name;
    125 
    126           if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')
    127             return &option_xat_attribute_table[key];
    128         }
    129     }
    130   return 0;
    131 }
    132 
    133 /**
    134  * Convert a command (keyword) to a option_xat_attribute_enum_t enumeration value.
    135  *
    136  * @param[in] str   a string that should start with a known key word.
    137  * @param[in] len   the provided length of the keyword at \a str.
    138  * @returns the enumeration value.
    139  * If not found, that value is XAT_INVALID_CMD.
    140  */
    141 option_xat_attribute_enum_t
    142 find_option_xat_attribute_cmd(char const * str, size_t len)
    143 {
    144     option_xat_attribute_map_t const * map;
    145 
    146     map = find_option_xat_attribute_name(str, (unsigned int)len);
    147     return (map == NULL) ? XAT_INVALID_CMD : map->xat_id;
    148 }
    149 
    150 /* end of option-xat-attribute.c */
    151