osi_vsmap.h 11 KB

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