osi_vsmap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_VSMAP_H_
  13. #define _OSI_VSMAP_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #ifndef _MSC_VER
  18. #include "kernel_config.h"
  19. #endif
  20. #include "osi_compiler.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * helper macro for constant map
  26. *
  27. * For constants by *\#define* or *enum*, the followings can be used:
  28. * \code{.cpp}
  29. * osiValueStrMap_t array[] = {
  30. * {OSI_VSMAP_CONST_DECL(SOME_MACRO)},
  31. * {OSI_VSMAP_CONST_DECL(SOME_ENUM)},
  32. * };
  33. * \endcode
  34. */
  35. #define OSI_VSMAP_CONST_DECL(name) name, #name
  36. /**
  37. * helper data structure for integer string map
  38. */
  39. typedef struct
  40. {
  41. uint32_t value; ///< integer value
  42. const char *str; ///< string value
  43. } osiValueStrMap_t;
  44. /**
  45. * @brief function for uint32_t comparison
  46. *
  47. * In bsearch(3) and qsort(3), a comparison function is needed.
  48. * When the key for comparing struct is \p uint32_t and it is the
  49. * first field, this helper function can be used. For example:
  50. *
  51. * \code{.cpp}
  52. * struct {
  53. * uint32_t id;
  54. * // ......
  55. * };
  56. * \endcode
  57. *
  58. * @param key key to be compared
  59. * @param p value to be compared
  60. * @return
  61. * - -1 if key < value, though negative can conform
  62. * - 0 if key == value
  63. * - 1 if key > value, though positive can conform
  64. */
  65. int osiUintIdCompare(const void *key, const void *p);
  66. /**
  67. * @brief function for uint16_t comparison
  68. *
  69. * @param key key to be compared
  70. * @param p value to be compared
  71. * @return
  72. * - -1 if key < value, though negative can conform
  73. * - 0 if key == value
  74. * - 1 if key > value, though positive can conform
  75. */
  76. int osiUint16IdCompare(const void *key, const void *p);
  77. /**
  78. * @brief check whether array is sorted
  79. *
  80. * The parameters follow \p bsearch.
  81. *
  82. * @param base array base address
  83. * @param nmemb element count
  84. * @param size element size
  85. * @param compar comparison function
  86. * @return
  87. * - true if the array is sorted
  88. * - false if not sorted, or invalid parameters
  89. */
  90. bool osiArrayIsSorted(const void *base, size_t nmemb, size_t size,
  91. int (*compar)(const void *, const void *));
  92. /**
  93. * @brief bsearch in value-string map
  94. *
  95. * The map must be sorted ascending by \p value.
  96. *
  97. * When \p value isn't found in the map, \p defval will be returned.
  98. *
  99. * @param value value to be searched
  100. * @param vs value-string map array, must be valid and sorted
  101. * @param count value-string map count
  102. * @param defval default string when not found
  103. * @return
  104. * - found mapped string
  105. * - \p defval if not found
  106. */
  107. const char *osiVsmapBsearch(uint32_t value, const osiValueStrMap_t *vs, size_t count, const char *defval);
  108. /**
  109. * @brief check whether value-string map is sorted
  110. *
  111. * In \p osiVsmapBsearch, value-string map must be sorted ascending
  112. * by \p value. This is to check whether the map is sorted.
  113. *
  114. * @param vs value-string map array, must be valid
  115. * @param count value-string map count
  116. * @return
  117. * - true if the map is sorted ascending
  118. * - false if not sorted
  119. */
  120. bool osiVsmapIsSorted(const osiValueStrMap_t *vs, size_t count);
  121. /**
  122. * @brief customized bsearch in value-string map
  123. *
  124. * The real data structure of \p vs is not \p osiValueStrMap_t, but it is
  125. * started with \p osiValueStrMap_t, with customized size.
  126. *
  127. * @param value value to be searched
  128. * @param vs value-string map array, must be valid and sorted
  129. * @param count value-string map count
  130. * @param size real data structure size
  131. * @param defval default string when not found
  132. * @return
  133. * - found mapped string
  134. * - \p defval if not found
  135. */
  136. const char *osiVsmapBsearchEx(uint32_t value, const osiValueStrMap_t *vs, size_t count, size_t size, const char *defval);
  137. /**
  138. * @brief check whether customized value-string map is sorted
  139. *
  140. * The real data structure of \p vs is not \p osiValueStrMap_t, but it is
  141. * started with \p osiValueStrMap_t, with customized size.
  142. *
  143. * @param vs value-string map array, must be valid
  144. * @param count value-string map count
  145. * @param size real data structure size
  146. * @return
  147. * - true if the map is sorted ascending
  148. * - false if not sorted
  149. */
  150. bool osiVsmapIsSortedEx(const osiValueStrMap_t *vs, size_t count, size_t size);
  151. /**
  152. * @brief find value by string, case insensitive
  153. *
  154. * The map is ended by NULL of \a str
  155. *
  156. * @param vsmap integer/string map
  157. * @param str string value
  158. * @return
  159. * - a map item if found
  160. * - NULL if not found
  161. */
  162. const osiValueStrMap_t *osiVsmapFindByIStr(const osiValueStrMap_t *vsmap, const char *str);
  163. /**
  164. * @brief find value by string, case sensitive
  165. *
  166. * The map is ended by NULL of \a str
  167. *
  168. * @param vsmap integer/string map
  169. * @param str string value
  170. * @return
  171. * - a map item if found
  172. * - NULL if not found
  173. */
  174. const osiValueStrMap_t *osiVsmapFindByStr(const osiValueStrMap_t *vsmap, const char *str);
  175. /**
  176. * @brief find string by value
  177. *
  178. * The map is ended by NULL of \a str
  179. *
  180. * @param vsmap integer/string map
  181. * @param value integer value
  182. * @return
  183. * - a map item if found
  184. * - NULL if not found
  185. */
  186. const osiValueStrMap_t *osiVsmapFindByVal(const osiValueStrMap_t *vsmap, uint32_t value);
  187. /**
  188. * @brief find string by value, with default string
  189. *
  190. * The map is ended by NULL of \a str. When \p value is not found, \p defval
  191. * is returned.
  192. *
  193. * @param vsmap integer/string map
  194. * @param value integer value
  195. * @param defval default string at not found
  196. * @return string value
  197. */
  198. const char *osiVsmapFindStr(const osiValueStrMap_t *vsmap, uint32_t value, const char *defval);
  199. /**
  200. * @brief find value by string, with default value
  201. *
  202. * The map is ended by NULL of \a str. When \p str is not found, \p defval
  203. * is returned.
  204. *
  205. * @param vsmap integer/string map
  206. * @param str string value
  207. * @param defval default integer value at not found
  208. * @return
  209. * - a map item if found
  210. * - NULL if not found
  211. */
  212. uint32_t osiVsmalFindVal(const osiValueStrMap_t *vsmap, const char *str, uint32_t defval);
  213. /**
  214. * @brief find value by case insensitive string, with default value
  215. *
  216. * The map is ended by NULL of \a str. When \p str is not found, \p defval
  217. * is returned.
  218. *
  219. * @param vsmap integer/string map
  220. * @param str string value
  221. * @param defval default integer value at not found
  222. * @return
  223. * - a map item if found
  224. * - NULL if not found
  225. */
  226. uint32_t osiVsmalFindIVal(const osiValueStrMap_t *vsmap, const char *str, uint32_t defval);
  227. /**
  228. * little helper to check wether an unsigned integer in list
  229. *
  230. * \param value value to be checked
  231. * \param varlist value list
  232. * \param count value list count
  233. * \return
  234. * - true if value in the list
  235. * - false if value not in the list
  236. */
  237. bool osiIsUintInList(uint32_t value, const uint32_t *varlist, unsigned count);
  238. /**
  239. * little helper to check wether an unsigned integer in range
  240. *
  241. * \param value value to be checked
  242. * \param minval minimal value, inclusive
  243. * \param maxval maximum value, inclusive
  244. * \return
  245. * - true if value in the range
  246. * - false if value not in the range
  247. */
  248. bool osiIsUintInRange(uint32_t value, uint32_t minval, uint32_t maxval);
  249. /**
  250. * little helper to check wether an unsigned integer in range list
  251. *
  252. * \param value value to be checked
  253. * \param ranges valid range list
  254. * \param count valid range list count
  255. * \return
  256. * - true if value in the range list
  257. * - false if value not in the range list
  258. */
  259. bool osiIsUintInRanges(uint32_t value, const osiUintRange_t *ranges, unsigned count);
  260. /**
  261. * little helper to check wether a signed integer in list
  262. *
  263. * \param value value to be checked
  264. * \param varlist value list
  265. * \param count value list count
  266. * \return
  267. * - true if value in the list
  268. * - false if value not in the list
  269. */
  270. bool osiIsIntInList(int value, const int *varlist, unsigned count);
  271. /**
  272. * little helper to check wether a signed integer in range
  273. *
  274. * \param value value to be checked
  275. * \param minval minimal value, inclusive
  276. * \param maxval maximum value, inclusive
  277. * \return
  278. * - true if value in the range
  279. * - false if value not in the range
  280. */
  281. bool osiIsIntInRange(int value, int minval, int maxval);
  282. /**
  283. * little helper to check wether a signed integer in range list
  284. *
  285. * \param value value to be checked
  286. * \param ranges valid range list
  287. * \param count valid range list count
  288. * \return
  289. * - true if value in the range list
  290. * - false if value not in the range list
  291. */
  292. bool osiIsIntInRanges(int value, const osiIntRange_t *ranges, unsigned count);
  293. /**
  294. * little helper to check wether an unsigned 64bits integer in list
  295. *
  296. * \param value value to be checked
  297. * \param varlist value list
  298. * \param count value list count
  299. * \return
  300. * - true if value in the list
  301. * - false if value not in the list
  302. */
  303. bool osiIsUint64InList(uint64_t value, const uint64_t *varlist, unsigned count);
  304. /**
  305. * little helper to check wether an unsigned 64bits integer in range
  306. *
  307. * \param value value to be checked
  308. * \param minval minimal value, inclusive
  309. * \param maxval maximum value, inclusive
  310. * \return
  311. * - true if value in the range
  312. * - false if value not in the range
  313. */
  314. bool osiIsUint64InRange(uint64_t value, uint64_t minval, uint64_t maxval);
  315. /**
  316. * little helper to check wether an unsigned 64bits integer in range list
  317. *
  318. * \param value value to be checked
  319. * \param ranges valid range list
  320. * \param count valid range list count
  321. * \return
  322. * - true if value in the range list
  323. * - false if value not in the range list
  324. */
  325. bool osiIsUint64InRanges(uint64_t value, const osiUint64Range_t *ranges, unsigned count);
  326. /**
  327. * little helper to check wether a signed 64bits integer in list
  328. *
  329. * \param value value to be checked
  330. * \param varlist value list
  331. * \param count value list count
  332. * \return
  333. * - true if value in the list
  334. * - false if value not in the list
  335. */
  336. bool osiIsInt64InList(int64_t value, const int64_t *varlist, unsigned count);
  337. /**
  338. * little helper to check wether a signed 64bits integer in range
  339. *
  340. * \param value value to be checked
  341. * \param minval minimal value, inclusive
  342. * \param maxval maximum value, inclusive
  343. * \return
  344. * - true if value in the range
  345. * - false if value not in the range
  346. */
  347. bool osiIsInt64InRange(int64_t value, int64_t minval, int64_t maxval);
  348. /**
  349. * little helper to check wether a signed 64bits integer in range list
  350. *
  351. * \param value value to be checked
  352. * \param ranges valid range list
  353. * \param count valid range list count
  354. * \return
  355. * - true if value in the range list
  356. * - false if value not in the range list
  357. */
  358. bool osiIsInt64InRanges(int64_t value, const osiInt64Range_t *ranges, unsigned count);
  359. #ifdef __cplusplus
  360. }
  361. #endif
  362. #endif