option-value-type.c 5.5 KB

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