strmap.h 13 KB

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