db.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*****************************************************************************\
  2. * Copyright (c) Athena Dev Teams - Licensed under GNU GPL *
  3. * For more information, see LICENCE in the main folder *
  4. * *
  5. * This file is separated in two sections: *
  6. * (1) public typedefs, enums, unions, structures and defines *
  7. * (2) public functions *
  8. * *
  9. * <B>Notes on the release system:</B> *
  10. * Whenever an entry is removed from the database both the key and the *
  11. * data are requested to be released. *
  12. * At least one entry is removed when replacing an entry, removing an *
  13. * entry, clearing the database or destroying the database. *
  14. * What is actually released is defined by the release function, the *
  15. * functions of the database only ask for the key and/or data to be *
  16. * released. *
  17. * *
  18. * TODO: *
  19. * - create an enum for the data (with int, unsigned int and void *) *
  20. * - create a custom database allocator *
  21. * - see what functions need or should be added to the database interface *
  22. * *
  23. * HISTORY: *
  24. * 2.1 (Athena build #???#) - Portability fix *
  25. * - Fixed the portability of casting to union and added the functions *
  26. * {@link DB#ensure(DB,DBKey,DBCreateData,...)} and *
  27. * {@link DB#clear(DB,DBApply,...)}. *
  28. * 2.0 (Athena build 4859) - Transition version *
  29. * - Almost everything recoded with a strategy similar to objects, *
  30. * database structure is maintained. *
  31. * 1.0 (up to Athena build 4706) *
  32. * - Previous database system. *
  33. * *
  34. * @version 2.1 (Athena build #???#) - Portability fix *
  35. * @author (Athena build 4859) Flavio @ Amazon Project *
  36. * @author (up to Athena build 4706) Athena Dev Teams *
  37. * @encoding US-ASCII *
  38. * @see common#db.c *
  39. \*****************************************************************************/
  40. #ifndef _DB_H_
  41. #define _DB_H_
  42. #include <stdarg.h>
  43. /*****************************************************************************\
  44. * (1) Section with public typedefs, enums, unions, structures and defines. *
  45. * DB_MANUAL_CAST_TO_UNION - Define when the compiler doesn't allow casting *
  46. * to unions. *
  47. * DBRelease - Enumeration of release options. *
  48. * DBType - Enumeration of database types. *
  49. * DBOptions - Bitfield enumeration of database options. *
  50. * DBKey - Union of used key types. *
  51. * DBApply - Format of functions applyed to the databases. *
  52. * DBMatcher - Format of matchers used in DB::getall. *
  53. * DBComparator - Format of the comparators used by the databases. *
  54. * DBHasher - Format of the hashers used by the databases. *
  55. * DBReleaser - Format of the releasers used by the databases. *
  56. * DB - Database interface. *
  57. \*****************************************************************************/
  58. /**
  59. * Define this to enable the functions that cast to unions.
  60. * Required when the compiler doesn't support casting to unions.
  61. * NOTE: It is recommened that the conditional tests to determine if this
  62. * should be defined be located in a makefile or a header file specific for
  63. * of compatibility and portability issues.
  64. * @public
  65. * @see #db_i2key(int)
  66. * @see #db_ui2key(unsigned int)
  67. * @see #db_str2key(unsigned char *)
  68. */
  69. //#define DB_MANUAL_CAST_TO_UNION
  70. /**
  71. * Bitfield with what should be released by the releaser function (if the
  72. * function supports it).
  73. * @public
  74. * @see #DBReleaser
  75. * @see #db_custom_release(DBRelease)
  76. */
  77. typedef enum {
  78. DB_RELEASE_NOTHING = 0,
  79. DB_RELEASE_KEY = 1,
  80. DB_RELEASE_DATA = 2,
  81. DB_RELEASE_BOTH = 3
  82. } DBRelease;
  83. /**
  84. * Supported types of database.
  85. * See {@link #db_fix_options(DBType,DBOptions)} for restrictions of the
  86. * types of databases.
  87. * @param DB_INT Uses int's for keys
  88. * @param DB_UINT Uses unsigned int's for keys
  89. * @param DB_STRING Uses strings for keys.
  90. * @param DB_ISTRING Uses case insensitive strings for keys.
  91. * @public
  92. * @see #DBOptions
  93. * @see #DBKey
  94. * @see #db_fix_options(DBType,DBOptions)
  95. * @see #db_default_cmp(DBType)
  96. * @see #db_default_hash(DBType)
  97. * @see #db_default_release(DBType,DBOptions)
  98. * @see #db_alloc(const char *,int,DBType,DBOptions,unsigned short)
  99. */
  100. typedef enum {
  101. DB_INT,
  102. DB_UINT,
  103. DB_STRING,
  104. DB_ISTRING
  105. } DBType;
  106. /**
  107. * Bitfield of options that define the behaviour of the database.
  108. * See {@link #db_fix_options(DBType,DBOptions)} for restrictions of the
  109. * types of databases.
  110. * @param DB_OPT_BASE Base options: does not duplicate keys, releases nothing
  111. * and does not allow NULL keys or NULL data.
  112. * @param DB_OPT_DUP_KEY Duplicates the keys internally. If DB_OPT_RELEASE_KEY
  113. * is defined, the real key is freed as soon as the entry is added.
  114. * @param DB_OPT_RELEASE_KEY Releases the key.
  115. * @param DB_OPT_RELEASE_DATA Releases the data whenever an entry is removed
  116. * from the database.
  117. * WARNING: for funtions that return the data (like DB::remove),
  118. * a dangling pointer will be returned.
  119. * @param DB_OPT_RELEASE_BOTH Releases both key and data.
  120. * @param DB_OPT_ALLOW_NULL_KEY Allow NULL keys in the database.
  121. * @param DB_OPT_ALLOW_NULL_DATA Allow NULL data in the database.
  122. * @public
  123. * @see #db_fix_options(DBType,DBOptions)
  124. * @see #db_default_release(DBType,DBOptions)
  125. * @see #db_alloc(const char *,int,DBType,DBOptions,unsigned short)
  126. */
  127. typedef enum {
  128. DB_OPT_BASE = 0,
  129. DB_OPT_DUP_KEY = 1,
  130. DB_OPT_RELEASE_KEY = 2,
  131. DB_OPT_RELEASE_DATA = 4,
  132. DB_OPT_RELEASE_BOTH = 6,
  133. DB_OPT_ALLOW_NULL_KEY = 8,
  134. DB_OPT_ALLOW_NULL_DATA = 16,
  135. } DBOptions;
  136. /**
  137. * Union of key types used by the database.
  138. * @param i Type of key for DB_INT databases
  139. * @param ui Type of key for DB_UINT databases
  140. * @param str Type of key for DB_STRING and DB_ISTRING databases
  141. * @public
  142. * @see #DBType
  143. * @see DB#get
  144. * @see DB#put
  145. * @see DB#remove
  146. */
  147. typedef union dbkey {
  148. int i;
  149. unsigned int ui;
  150. unsigned char *str;//## TODO change to 'const char *'
  151. } DBKey;
  152. /**
  153. * Format of funtions that create the data for the key when the entry doesn't
  154. * exist in the database yet.
  155. * @param key Key of the database entry
  156. * @param args Extra arguments of the funtion
  157. * @return Data identified by the key to be put in the database
  158. * @public
  159. * @see DB#vensure
  160. * @see DB#ensure
  161. */
  162. typedef void *(*DBCreateData)(DBKey key, va_list args);
  163. /**
  164. * Format of functions to be applyed to an unspecified quantity of entries of
  165. * a database.
  166. * Any function that applyes this function to the database will return the sum
  167. * of values returned by this function.
  168. * @param key Key of the database entry
  169. * @param data Data of the database entry
  170. * @param args Extra arguments of the funtion
  171. * @return Value to be added up by the funtion that is applying this
  172. * @public
  173. * @see DB#vforeach
  174. * @see DB#foreach
  175. * @see DB#vdestroy
  176. * @see DB#destroy
  177. */
  178. typedef int (*DBApply)(DBKey key, void *data, va_list args);
  179. /**
  180. * Format of functions that match database entries.
  181. * The purpose of the match depends on the function that is calling the matcher.
  182. * Returns 0 if it is a match, another number otherwise.
  183. * @param key Key of the database entry
  184. * @param data Data of the database entry
  185. * @param args Extra arguments of the function
  186. * @return 0 if a match, another number otherwise
  187. * @public
  188. * @see DB#getall
  189. */
  190. typedef int (*DBMatcher)(DBKey key, void *data, va_list args);
  191. /**
  192. * Format of the comparators used internally by the database system.
  193. * Compares key1 to key2.
  194. * <code>maxlen</code> is the maximum number of character used in DB_STRING and
  195. * DB_ISTRING databases. If 0, the maximum number of maxlen is used (64K).
  196. * Returns 0 is equal, negative if lower and positive is higher.
  197. * @param key1 Key being compared
  198. * @param key2 Key we are comparing to
  199. * @param maxlen Maximum number of characters used in DB_STRING and DB_ISTRING
  200. * databases.
  201. * @return 0 if equal, negative if lower and positive if higher
  202. * @public
  203. * @see #db_default_cmp(DBType)
  204. */
  205. typedef int (*DBComparator)(DBKey key1, DBKey key2, unsigned short maxlen);
  206. /**
  207. * Format of the hashers used internally by the database system.
  208. * Creates the hash of the key.
  209. * <code>maxlen</code> is the maximum number of character used in DB_STRING and
  210. * DB_ISTRING databases. If 0, the maximum number of maxlen is used (64K).
  211. * @param key Key being hashed
  212. * @param maxlen Maximum number of characters used in DB_STRING and DB_ISTRING
  213. * databases.
  214. * @return Hash of the key
  215. * @public
  216. * @see #db_default_hash(DBType)
  217. */
  218. typedef unsigned int (*DBHasher)(DBKey key, unsigned short maxlen);
  219. /**
  220. * Format of the releaser used by the database system.
  221. * Releases nothing, the key, the data or both.
  222. * All standard releasers use aFree to release.
  223. * @param key Key of the database entry
  224. * @param data Data of the database entry
  225. * @param which What is being requested to be released
  226. * @public
  227. * @see #DBRelease
  228. * @see #db_default_releaser(DBType,DBOptions)
  229. * @see #db_custom_release(DBRelease)
  230. */
  231. typedef void (*DBReleaser)(DBKey key, void *data, DBRelease which);
  232. /**
  233. * Public interface of a database. Only contains funtions.
  234. * All the functions take the interface as the first argument.
  235. * @public
  236. * @see DB#get(DB,DBKey)
  237. * @see DB#getall(DB,void **,unsigned int,DBMatch,...)
  238. * @see DB#vgetall(DB,void **,unsigned int,DBMatch,va_list)
  239. * @see DB#put(DB,DBKey,void *)
  240. * @see DB#remove(DB,DBKey)
  241. * @see DB#foreach(DB,DBApply,...)
  242. * @see DB#vforeach(DB,DBApply,va_list)
  243. * @see DB#destroy(DB,DBApply,...)
  244. * @see DB#destroy(DB,DBApply,va_list)
  245. * @see DB#size(DB)
  246. * @see DB#type(DB)
  247. * @see DB#options(DB)
  248. * @see #db_alloc(const char *,int,DBType,DBOptions,unsigned short)
  249. */
  250. typedef struct dbt *DB;
  251. struct dbt {
  252. /**
  253. * Get the data of the entry identifid by the key.
  254. * @param dbi Interface of the database
  255. * @param key Key that identifies the entry
  256. * @return Data of the entry or NULL if not found
  257. * @protected
  258. * @see #db_get(DB,DBKey)
  259. */
  260. void *(*get)(struct dbt *dbi, DBKey key);
  261. /**
  262. * Just calls {@link DB#vgetall(DB,void **,unsigned int,DBMatch,va_list)}.
  263. * Get the data of the entries matched by <code>match</code>.
  264. * It puts a maximum of <code>max</code> entries into <code>buf</code>.
  265. * If <code>buf</code> is NULL, it only counts the matches.
  266. * Returns the number of entries that matched.
  267. * NOTE: if the value returned is greater than <code>max</code>, only the
  268. * first <code>max</code> entries found are put into the buffer.
  269. * @param dbi Interface of the database
  270. * @param buf Buffer to put the data of the matched entries
  271. * @param max Maximum number of data entries to be put into buf
  272. * @param match Function that matches the database entries
  273. * @param ... Extra arguments for match
  274. * @return The number of entries that matched
  275. * @protected
  276. * @see DB#vgetall
  277. * @see #db_getall(DB,void **,unsigned int,DBMatch,...)
  278. */
  279. unsigned int (*getall)(DB self, void **buf, unsigned int max, DBMatcher match, ...);
  280. /**
  281. * Get the data of the entries matched by <code>match</code>.
  282. * It puts a maximum of <code>max</code> entries into <code>buf</code>.
  283. * If <code>buf</code> is NULL, it only counts the matches.
  284. * Returns the number of entries that matched.
  285. * NOTE: if the value returned is greater than <code>max</code>, only the
  286. * first <code>max</code> entries found are put into the buffer.
  287. * @param dbi Interface of the database
  288. * @param buf Buffer to put the data of the matched entries
  289. * @param max Maximum number of data entries to be put into buf
  290. * @param match Function that matches the database entries
  291. * @param ... Extra arguments for match
  292. * @return The number of entries that matched
  293. * @protected
  294. * @see DB#getall
  295. * @see #db_vgetall(DB,void **,unsigned int,DBMatch,va_list)
  296. */
  297. unsigned int (*vgetall)(DB self, void **buf, unsigned int max, DBMatcher match, va_list args);
  298. /**
  299. * Just calls {@link common\db.h\DB#vensure(DB,DBKey,DBCreateData,va_list)}.
  300. * Get the data of the entry identified by the key.
  301. * If the entry does not exist, an entry is added with the data returned by
  302. * <code>create</code>.
  303. * @param dbi Interface of the database
  304. * @param key Key that identifies the entry
  305. * @param create Function used to create the data if the entry doesn't exist
  306. * @param ... Extra arguments for create
  307. * @return Data of the entry
  308. * @protected
  309. * @see DB#vensure(DB,DBKey,DBCreateData,va_list)
  310. * @see #db_ensure(DB,DBKey,DBCreateData,...)
  311. */
  312. void *(*ensure)(DB self, DBKey key, DBCreateData create, ...);
  313. /**
  314. * Get the data of the entry identified by the key.
  315. * If the entry does not exist, an entry is added with the data returned by
  316. * <code>create</code>.
  317. * @param dbi Interface of the database
  318. * @param key Key that identifies the entry
  319. * @param create Function used to create the data if the entry doesn't exist
  320. * @param args Extra arguments for create
  321. * @return Data of the entry
  322. * @protected
  323. * @see DB#ensure(DB,DBKey,DBCreateData,...)
  324. * @see #db_vensure(DB,DBKey,DBCreateData,va_list)
  325. */
  326. void *(*vensure)(DB self, DBKey key, DBCreateData create, va_list args);
  327. /**
  328. * Put the data identified by the key in the database.
  329. * Returns the previous data if the entry exists or NULL.
  330. * NOTE: Uses the new key, the old one is released.
  331. * @param dbi Interface of the database
  332. * @param key Key that identifies the data
  333. * @param data Data to be put in the database
  334. * @return The previous data if the entry exists or NULL
  335. * @protected
  336. * @see #db_put(DB,DBKey,void *)
  337. */
  338. void *(*put)(DB self, DBKey key, void *data);
  339. /**
  340. * Remove an entry from the database.
  341. * Returns the data of the entry.
  342. * NOTE: The key (of the database) is released.
  343. * @param dbi Interface of the database
  344. * @param key Key that identifies the entry
  345. * @return The data of the entry or NULL if not found
  346. * @protected
  347. * @see #db_remove(DB,DBKey)
  348. */
  349. void *(*remove)(DB self, DBKey key);
  350. /**
  351. * Just calls {@link DB#vforeach(DB,DBApply,va_list)}.
  352. * Apply <code>func</code> to every entry in the database.
  353. * Returns the sum of values returned by func.
  354. * @param dbi Interface of the database
  355. * @param func Function to be applyed
  356. * @param ... Extra arguments for func
  357. * @return Sum of the values returned by func
  358. * @protected
  359. * @see DB#vforeach
  360. * @see #db_foreach(DB,DBApply,...)
  361. */
  362. int (*foreach)(DB self, DBApply func, ...);
  363. /**
  364. * Apply <code>func</code> to every entry in the database.
  365. * Returns the sum of values returned by func.
  366. * @param dbi Interface of the database
  367. * @param func Function to be applyed
  368. * @param args Extra arguments for func
  369. * @return Sum of the values returned by func
  370. * @protected
  371. * @see DB#foreach
  372. * @see #db_vforeach(DB,DBApply,va_list)
  373. */
  374. int (*vforeach)(DB self, DBApply func, va_list args);
  375. /**
  376. * Just calls {@link DB#vclear(DB,DBApply,va_list)}.
  377. * Removes all entries from the database.
  378. * Before deleting an entry, func is applyed to it.
  379. * Releases the key and the data.
  380. * Returns the sum of values returned by func, if it exists.
  381. * @param dbi Interface of the database
  382. * @param func Function to be applyed to every entry before deleting
  383. * @param ... Extra arguments for func
  384. * @return Sum of values returned by func
  385. * @protected
  386. * @see DB#vclear
  387. * @see #db_clear(DB,DBApply,...)
  388. */
  389. int (*clear)(DB self, DBApply func, ...);
  390. /**
  391. * Removes all entries from the database.
  392. * Before deleting an entry, func is applyed to it.
  393. * Releases the key and the data.
  394. * Returns the sum of values returned by func, if it exists.
  395. * @param dbi Interface of the database
  396. * @param func Function to be applyed to every entry before deleting
  397. * @param args Extra arguments for func
  398. * @return Sum of values returned by func
  399. * @protected
  400. * @see DB#clear
  401. * @see #vclear(DB,DBApply,va_list)
  402. */
  403. int (*vclear)(DB self, DBApply func, va_list args);
  404. /**
  405. * Just calls {@link DB#vdestroy(DB,DBApply,va_list)}.
  406. * Finalize the database, feeing all the memory it uses.
  407. * Before deleting an entry, func is applyed to it.
  408. * Releases the key and the data.
  409. * Returns the sum of values returned by func, if it exists.
  410. * NOTE: This locks the database globally. Any attempt to insert or remove
  411. * a database entry will give an error and be aborted (except for clearing).
  412. * @param dbi Interface of the database
  413. * @param func Function to be applyed to every entry before deleting
  414. * @param ... Extra arguments for func
  415. * @return Sum of values returned by func
  416. * @protected
  417. * @see DB#vdestroy
  418. * @see #db_destroy(DB,DBApply,...)
  419. */
  420. int (*destroy)(DB self, DBApply func, ...);
  421. /**
  422. * Finalize the database, feeing all the memory it uses.
  423. * Before deleting an entry, func is applyed to it.
  424. * Returns the sum of values returned by func, if it exists.
  425. * NOTE: This locks the database globally. Any attempt to insert or remove
  426. * a database entry will give an error and be aborted (except for clearing).
  427. * @param dbi Interface of the database
  428. * @param func Function to be applyed to every entry before deleting
  429. * @param args Extra arguments for func
  430. * @return Sum of values returned by func
  431. * @protected
  432. * @see DB#destroy
  433. * @see #db_vdestroy(DB,DBApply,va_list)
  434. */
  435. int (*vdestroy)(DB self, DBApply func, va_list args);
  436. /**
  437. * Return the size of the database (number of items in the database).
  438. * @param dbi Interface of the database
  439. * @return Size of the database
  440. * @protected
  441. * @see #db_size(DB)
  442. */
  443. unsigned int (*size)(DB self);
  444. /**
  445. * Return the type of the database.
  446. * @param dbi Interface of the database
  447. * @return Type of the database
  448. * @protected
  449. * @see #db_type(DB)
  450. */
  451. DBType (*type)(DB self);
  452. /**
  453. * Return the options of the database.
  454. * @param dbi Interface of the database
  455. * @return Options of the database
  456. * @protected
  457. * @see #db_options(DB)
  458. */
  459. DBOptions (*options)(DB self);
  460. };
  461. //For easy access to the common functions.
  462. #ifdef DB_MANUAL_CAST_TO_UNION
  463. # define i2key db_i2key
  464. # define ui2key db_ui2key
  465. # define str2key db_str2key
  466. #else /* not DB_MANUAL_CAST_TO_UNION */
  467. # define i2key(k) ((DBKey)(int)(k))
  468. # define ui2key(k) ((DBKey)(unsigned int)(k))
  469. # define str2key(k) ((DBKey)(unsigned char *)(k))
  470. #endif /* DB_MANUAL_CAST_TO_UNION / not DB_MANUAL_CAST_TO_UNION */
  471. #define db_get(db,k) (db)->get((db),(k))
  472. #define idb_get(db,k) (db)->get((db),i2key(k))
  473. #define uidb_get(db,k) (db)->get((db),ui2key(k))
  474. #define strdb_get(db,k) (db)->get((db),str2key(k))
  475. #define db_put(db,k,d) (db)->put((db),(k),(d))
  476. #define idb_put(db,k,d) (db)->put((db),i2key(k),(d))
  477. #define uidb_put(db,k,d) (db)->put((db),ui2key(k),(d))
  478. #define strdb_put(db,k,d) (db)->put((db),str2key(k),(d))
  479. #define db_remove(db,k) (db)->remove((db),(k))
  480. #define idb_remove(db,k) (db)->remove((db),i2key(k))
  481. #define uidb_remove(db,k) (db)->remove((db),ui2key(k))
  482. #define strdb_remove(db,k) (db)->remove((db),str2key(k))
  483. //These are discarding the possible vargs you could send to the function, so those
  484. //that require vargs must not use these defines.
  485. #define db_ensure(db,k,f) (db)->ensure((db),(k),f)
  486. #define idb_ensure(db,k,f) (db)->ensure((db),i2key(k),f)
  487. #define uidb_ensure(db,k,f) (db)->ensure((db),ui2key(k),f)
  488. #define strdb_ensure(db,k,f) (db)->ensure((db),str2key(k),f)
  489. /*****************************************************************************\
  490. * (2) Section with public functions. *
  491. * db_fix_options - Fix the options for a type of database. *
  492. * db_default_cmp - Get the default comparator for a type of database. *
  493. * db_default_hash - Get the default hasher for a type of database. *
  494. * db_default_release - Get the default releaser for a type of database *
  495. * with the fixed options. *
  496. * db_custom_release - Get the releaser that behaves as specified. *
  497. * db_alloc - Allocate a new database. *
  498. * db_i2key - Manual cast from 'int' to 'DBKey'. *
  499. * db_ui2key - Manual cast from 'unsigned int' to 'DBKey'. *
  500. * db_str2key - Manual cast from 'unsigned char *' to 'DBKey'. *
  501. * db_init - Initialise the database system. *
  502. * db_final - Finalise the database system. *
  503. \*****************************************************************************/
  504. /**
  505. * Returns the fixed options according to the database type.
  506. * Sets required options and unsets unsupported options.
  507. * For numeric databases DB_OPT_DUP_KEY and DB_OPT_RELEASE_KEY are unset.
  508. * @param type Type of the database
  509. * @param options Original options of the database
  510. * @return Fixed options of the database
  511. * @private
  512. * @see #DBType
  513. * @see #DBOptions
  514. * @see #db_default_release(DBType,DBOptions)
  515. * @see common\db.c#db_fix_options(DBType,DBOptions)
  516. */
  517. DBOptions db_fix_options(DBType type, DBOptions options);
  518. /**
  519. * Returns the default comparator for the type of database.
  520. * @param type Type of database
  521. * @return Comparator for the type of database or NULL if unknown database
  522. * @public
  523. * @see #DBType
  524. * @see #DBComparator
  525. * @see common\db.c#db_default_cmp(DBType)
  526. */
  527. DBComparator db_default_cmp(DBType type);
  528. /**
  529. * Returns the default hasher for the specified type of database.
  530. * @param type Type of database
  531. * @return Hasher of the type of database or NULL if unknown database
  532. * @public
  533. * @see #DBType
  534. * @see #DBHasher
  535. * @see common\db.c#db_default_hash(DBType)
  536. */
  537. DBHasher db_default_hash(DBType type);
  538. /**
  539. * Returns the default releaser for the specified type of database with the
  540. * specified options.
  541. * NOTE: the options are fixed by {@link #db_fix_options(DBType,DBOptions)}
  542. * before choosing the releaser
  543. * @param type Type of database
  544. * @param options Options of the database
  545. * @return Default releaser for the type of database with the fixed options
  546. * @public
  547. * @see #DBType
  548. * @see #DBOptions
  549. * @see #DBReleaser
  550. * @see #db_fix_options(DBType,DBOptions)
  551. * @see #db_custom_release(DBRelease)
  552. * @see common\db.c#db_default_release(DBType,DBOptions)
  553. */
  554. DBReleaser db_default_release(DBType type, DBOptions options);
  555. /**
  556. * Returns the releaser that behaves as <code>which</code> specifies.
  557. * @param which Defines what the releaser releases
  558. * @return Releaser for the specified release options
  559. * @public
  560. * @see #DBRelease
  561. * @see #DBReleaser
  562. * @see #db_default_release(DBType,DBOptions)
  563. * @see common\db.c#db_custom_release(DBRelease)
  564. */
  565. DBReleaser db_custom_release(DBRelease which);
  566. /**
  567. * Allocate a new database of the specified type.
  568. * It uses the default comparator, hasher and releaser of the specified
  569. * database type and fixed options.
  570. * NOTE: the options are fixed by {@link #db_fix_options(DBType,DBOptions)}
  571. * before creating the database.
  572. * @param file File where the database is being allocated
  573. * @param line Line of the file where the database is being allocated
  574. * @param type Type of database
  575. * @param options Options of the database
  576. * @param maxlen Maximum length of the string to be used as key in string
  577. * databases
  578. * @return The interface of the database
  579. * @public
  580. * @see #DBType
  581. * @see #DB
  582. * @see #db_default_cmp(DBType)
  583. * @see #db_default_hash(DBType)
  584. * @see #db_default_release(DBType,DBOptions)
  585. * @see #db_fix_options(DBType,DBOptions)
  586. * @see common\db.c#db_alloc(const char *,int,DBType,DBOptions,unsigned short)
  587. */
  588. DB db_alloc(const char *file, int line, DBType type, DBOptions options, unsigned short maxlen);
  589. #ifdef DB_MANUAL_CAST_TO_UNION
  590. /**
  591. * Manual cast from 'int' to the union DBKey.
  592. * Created for compilers that don't support casting to unions.
  593. * @param key Key to be casted
  594. * @return The key as a DBKey union
  595. * @public
  596. * @see #DB_MANUAL_CAST_TO_UNION
  597. * @see #db_ui2key(unsigned int)
  598. * @see #db_str2key(unsigned char *)
  599. * @see common\db.c#db_i2key(int)
  600. */
  601. DBKey db_i2key(int key);
  602. /**
  603. * Manual cast from 'unsigned int' to the union DBKey.
  604. * Created for compilers that don't support casting to unions.
  605. * @param key Key to be casted
  606. * @return The key as a DBKey union
  607. * @public
  608. * @see #DB_MANUAL_CAST_TO_UNION
  609. * @see #db_i2key(int)
  610. * @see #db_str2key(unsigned char *)
  611. * @see common\db.c#db_ui2key(unsigned int)
  612. */
  613. DBKey db_ui2key(unsigned int key);
  614. /**
  615. * Manual cast from 'unsigned char *' to the union DBKey.
  616. * Created for compilers that don't support casting to unions.
  617. * @param key Key to be casted
  618. * @return The key as a DBKey union
  619. * @public
  620. * @see #DB_MANUAL_CAST_TO_UNION
  621. * @see #db_i2key(int)
  622. * @see #db_ui2key(unsigned int)
  623. * @see common\db.c#db_str2key(unsigned char *)
  624. */
  625. DBKey db_str2key(unsigned char *key);
  626. #endif /* DB_MANUAL_CAST_TO_UNION */
  627. /**
  628. * Initialize the database system.
  629. * @public
  630. * @see #db_final(void)
  631. * @see common\db.c#db_init(void)
  632. */
  633. void db_init(void);
  634. /**
  635. * Finalize the database system.
  636. * Frees the memory used by the block reusage system.
  637. * @public
  638. * @see #db_init(void)
  639. * @see common\db.c#db_final(void)
  640. */
  641. void db_final(void);
  642. // Link DB System - From jAthena
  643. struct linkdb_node {
  644. struct linkdb_node *next;
  645. struct linkdb_node *prev;
  646. void *key;
  647. void *data;
  648. };
  649. void linkdb_insert ( struct linkdb_node** head, void *key, void* data); // 重複を考慮しない
  650. void linkdb_replace( struct linkdb_node** head, void *key, void* data); // 重複を考慮する
  651. void* linkdb_search ( struct linkdb_node** head, void *key);
  652. void* linkdb_erase ( struct linkdb_node** head, void *key);
  653. void linkdb_final ( struct linkdb_node** head );
  654. #endif