strmap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * strmap version 2.0.1
  3. *
  4. * ANSI C hash table for strings.
  5. *
  6. * Version history:
  7. * 1.0.0 - initial release
  8. * 2.0.0 - changed function prefix from strmap to sm to ensure
  9. * ANSI C compatibility
  10. * 2.0.1 - improved documentation
  11. *
  12. * strmap.h
  13. *
  14. * Copyright (c) 2009, 2011, 2013 Per Ola Kristensson.
  15. *
  16. * Per Ola Kristensson <pok21@cam.ac.uk>
  17. * Inference Group, Department of Physics
  18. * University of Cambridge
  19. * Cavendish Laboratory
  20. * JJ Thomson Avenue
  21. * CB3 0HE Cambridge
  22. * United Kingdom
  23. *
  24. * strmap is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Lesser General Public License as published by
  26. * the Free Software Foundation, either version 3 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * strmap is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Lesser General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Lesser General Public License
  35. * along with strmap. If not, see <http://www.gnu.org/licenses/>.
  36. */
  37. #ifndef _STRMAP_H_
  38. #define _STRMAP_H_
  39. #ifdef __cplusplus
  40. extern "C"
  41. {
  42. #endif
  43. #include <stdlib.h>
  44. #include <string.h>
  45. typedef struct StrMap StrMap;
  46. /*
  47. * This callback function is called once per key-value when iterating over
  48. * all keys associated to values.
  49. *
  50. * Parameters:
  51. *
  52. * key: A pointer to a null-terminated C string. The string must not
  53. * be modified by the client.
  54. *
  55. * value: A pointer to a null-terminated C string. The string must
  56. * not be modified by the client.
  57. *
  58. * obj: A pointer to a client-specific object. This parameter may be
  59. * null.
  60. *
  61. * Return value: None.
  62. */
  63. typedef void(*sm_enum_func)(const char *key, void *value, const void *obj);
  64. /*
  65. * Creates a string map.
  66. *
  67. * Parameters:
  68. *
  69. * capacity: The number of top-level slots this string map
  70. * should allocate. This parameter must be > 0.
  71. *
  72. * Return value: A pointer to a string map object,
  73. * or null if a new string map could not be allocated.
  74. */
  75. StrMap * sm_new(unsigned int capacity);
  76. /*
  77. * Releases all memory held by a string map object.
  78. *
  79. * Parameters:
  80. *
  81. * map: A pointer to a string map. This parameter cannot be null.
  82. * If the supplied string map has been previously released, the
  83. * behaviour of this function is undefined.
  84. *
  85. * Return value: None.
  86. */
  87. void sm_delete(StrMap *map);
  88. /*
  89. * Returns the value associated with the supplied key.
  90. *
  91. * Parameters:
  92. *
  93. * map: A pointer to a string map. This parameter cannot be null.
  94. *
  95. * key: A pointer to a null-terminated C string. This parameter cannot
  96. * be null.
  97. *
  98. * out_buf: A pointer to an output buffer which will contain the value,
  99. * if it exists and fits into the buffer.
  100. *
  101. * n_out_buf: The size of the output buffer in bytes.
  102. *
  103. * Return value: If out_buf is set to null and n_out_buf is set to 0 the return
  104. * value will be the number of bytes required to store the value (if it exists)
  105. * and its null-terminator. For all other parameter configurations the return value
  106. * is 1 if an associated value was found and completely copied into the output buffer,
  107. * 0 otherwise.
  108. */
  109. int sm_get(const StrMap *map, const char *key, void **out_buf);
  110. /*
  111. * Queries the existence of a key.
  112. *
  113. * Parameters:
  114. *
  115. * map: A pointer to a string map. This parameter cannot be null.
  116. *
  117. * key: A pointer to a null-terminated C string. This parameter cannot
  118. * be null.
  119. *
  120. * Return value: 1 if the key exists, 0 otherwise.
  121. */
  122. int sm_exists(const StrMap *map, const char *key);
  123. /*
  124. * Associates a value with the supplied key. If the key is already
  125. * associated with a value, the previous value is replaced.
  126. *
  127. * Parameters:
  128. *
  129. * map: A pointer to a string map. This parameter cannot be null.
  130. *
  131. * key: A pointer to a null-terminated C string. This parameter
  132. * cannot be null. The string must have a string length > 0. The
  133. * string will be copied.
  134. *
  135. * value: A pointer to a null-terminated C string. This parameter
  136. * cannot be null. The string must have a string length > 0. The
  137. * string will be copied.
  138. *
  139. * Return value: 1 if the association succeeded, 0 otherwise.
  140. */
  141. int sm_put(StrMap *map, const char *key, void *value);
  142. /*
  143. * Returns the number of associations between keys and values.
  144. *
  145. * Parameters:
  146. *
  147. * map: A pointer to a string map. This parameter cannot be null.
  148. *
  149. * Return value: The number of associations between keys and values.
  150. */
  151. int sm_get_count(const StrMap *map);
  152. /*
  153. * An enumerator over all associations between keys and values.
  154. *
  155. * Parameters:
  156. *
  157. * map: A pointer to a string map. This parameter cannot be null.
  158. *
  159. * enum_func: A pointer to a callback function that will be
  160. * called by this procedure once for every key associated
  161. * with a value. This parameter cannot be null.
  162. *
  163. * obj: A pointer to a client-specific object. This parameter will be
  164. * passed back to the client's callback function. This parameter can
  165. * be null.
  166. *
  167. * Return value: 1 if enumeration completed, 0 otherwise.
  168. */
  169. int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj);
  170. #ifdef __cplusplus
  171. }
  172. #endif
  173. #endif
  174. /*
  175. GNU LESSER GENERAL PUBLIC LICENSE
  176. Version 3, 29 June 2007
  177. Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
  178. Everyone is permitted to copy and distribute verbatim copies
  179. of this license document, but changing it is not allowed.
  180. This version of the GNU Lesser General Public License incorporates
  181. the terms and conditions of version 3 of the GNU General Public
  182. License, supplemented by the additional permissions listed below.
  183. 0. Additional Definitions.
  184. As used herein, "this License" refers to version 3 of the GNU Lesser
  185. General Public License, and the "GNU GPL" refers to version 3 of the GNU
  186. General Public License.
  187. "The Library" refers to a covered work governed by this License,
  188. other than an Application or a Combined Work as defined below.
  189. An "Application" is any work that makes use of an interface provided
  190. by the Library, but which is not otherwise based on the Library.
  191. Defining a subclass of a class defined by the Library is deemed a mode
  192. of using an interface provided by the Library.
  193. A "Combined Work" is a work produced by combining or linking an
  194. Application with the Library. The particular version of the Library
  195. with which the Combined Work was made is also called the "Linked
  196. Version".
  197. The "Minimal Corresponding Source" for a Combined Work means the
  198. Corresponding Source for the Combined Work, excluding any source code
  199. for portions of the Combined Work that, considered in isolation, are
  200. based on the Application, and not on the Linked Version.
  201. The "Corresponding Application Code" for a Combined Work means the
  202. object code and/or source code for the Application, including any data
  203. and utility programs needed for reproducing the Combined Work from the
  204. Application, but excluding the System Libraries of the Combined Work.
  205. 1. Exception to Section 3 of the GNU GPL.
  206. You may convey a covered work under sections 3 and 4 of this License
  207. without being bound by section 3 of the GNU GPL.
  208. 2. Conveying Modified Versions.
  209. If you modify a copy of the Library, and, in your modifications, a
  210. facility refers to a function or data to be supplied by an Application
  211. that uses the facility (other than as an argument passed when the
  212. facility is invoked), then you may convey a copy of the modified
  213. version:
  214. a) under this License, provided that you make a good faith effort to
  215. ensure that, in the event an Application does not supply the
  216. function or data, the facility still operates, and performs
  217. whatever part of its purpose remains meaningful, or
  218. b) under the GNU GPL, with none of the additional permissions of
  219. this License applicable to that copy.
  220. 3. Object Code Incorporating Material from Library Header Files.
  221. The object code form of an Application may incorporate material from
  222. a header file that is part of the Library. You may convey such object
  223. code under terms of your choice, provided that, if the incorporated
  224. material is not limited to numerical parameters, data structure
  225. layouts and accessors, or small macros, inline functions and templates
  226. (ten or fewer lines in length), you do both of the following:
  227. a) Give prominent notice with each copy of the object code that the
  228. Library is used in it and that the Library and its use are
  229. covered by this License.
  230. b) Accompany the object code with a copy of the GNU GPL and this license
  231. document.
  232. 4. Combined Works.
  233. You may convey a Combined Work under terms of your choice that,
  234. taken together, effectively do not restrict modification of the
  235. portions of the Library contained in the Combined Work and reverse
  236. engineering for debugging such modifications, if you also do each of
  237. the following:
  238. a) Give prominent notice with each copy of the Combined Work that
  239. the Library is used in it and that the Library and its use are
  240. covered by this License.
  241. b) Accompany the Combined Work with a copy of the GNU GPL and this license
  242. document.
  243. c) For a Combined Work that displays copyright notices during
  244. execution, include the copyright notice for the Library among
  245. these notices, as well as a reference directing the user to the
  246. copies of the GNU GPL and this license document.
  247. d) Do one of the following:
  248. 0) Convey the Minimal Corresponding Source under the terms of this
  249. License, and the Corresponding Application Code in a form
  250. suitable for, and under terms that permit, the user to
  251. recombine or relink the Application with a modified version of
  252. the Linked Version to produce a modified Combined Work, in the
  253. manner specified by section 6 of the GNU GPL for conveying
  254. Corresponding Source.
  255. 1) Use a suitable shared library mechanism for linking with the
  256. Library. A suitable mechanism is one that (a) uses at run time
  257. a copy of the Library already present on the user's computer
  258. system, and (b) will operate properly with a modified version
  259. of the Library that is interface-compatible with the Linked
  260. Version.
  261. e) Provide Installation Information, but only if you would otherwise
  262. be required to provide such information under section 6 of the
  263. GNU GPL, and only to the extent that such information is
  264. necessary to install and execute a modified version of the
  265. Combined Work produced by recombining or relinking the
  266. Application with a modified version of the Linked Version. (If
  267. you use option 4d0, the Installation Information must accompany
  268. the Minimal Corresponding Source and Corresponding Application
  269. Code. If you use option 4d1, you must provide the Installation
  270. Information in the manner specified by section 6 of the GNU GPL
  271. for conveying Corresponding Source.)
  272. 5. Combined Libraries.
  273. You may place library facilities that are a work based on the
  274. Library side by side in a single library together with other library
  275. facilities that are not Applications and are not covered by this
  276. License, and convey such a combined library under terms of your
  277. choice, if you do both of the following:
  278. a) Accompany the combined library with a copy of the same work based
  279. on the Library, uncombined with any other library facilities,
  280. conveyed under the terms of this License.
  281. b) Give prominent notice with the combined library that part of it
  282. is a work based on the Library, and explaining where to find the
  283. accompanying uncombined form of the same work.
  284. 6. Revised Versions of the GNU Lesser General Public License.
  285. The Free Software Foundation may publish revised and/or new versions
  286. of the GNU Lesser General Public License from time to time. Such new
  287. versions will be similar in spirit to the present version, but may
  288. differ in detail to address new problems or concerns.
  289. Each version is given a distinguishing version number. If the
  290. Library as you received it specifies that a certain numbered version
  291. of the GNU Lesser General Public License "or any later version"
  292. applies to it, you have the option of following the terms and
  293. conditions either of that published version or of any later version
  294. published by the Free Software Foundation. If the Library as you
  295. received it does not specify a version number of the GNU Lesser
  296. General Public License, you may choose any version of the GNU Lesser
  297. General Public License ever published by the Free Software Foundation.
  298. If the Library as you received it specifies that a proxy can decide
  299. whether future versions of the GNU Lesser General Public License shall
  300. apply, that proxy's public statement of acceptance of any version is
  301. permanent authorization for you to choose that version for the
  302. Library.
  303. */