From dc5babf8c3fb943a22c440f5afd6957b121926c5 Mon Sep 17 00:00:00 2001 From: gepard1984 Date: Tue, 13 Mar 2012 14:46:28 +0000 Subject: Enhanced `DBMap` implementation to allow storing integer type data in addition to void pointers. - Added enum for data: `int`, `unsigned int` and `void*` - Replaced generic `void*` data with `DBData` struct to hold `int`, `unsigned int` or `void*` (member of `DBNode`) - Added `db_i2data`, `db_ui2data` and `db_ptr2data` functions to cast from `int`/`uint`/`void*` to `DBData` (used in `DBMap::put()`) - Added `db_data2i`, `db_data2ui` and `db_data2ptr` functions to get `int`/`uint`/`void*` from `DBData` - Enabled statistics for new functions in `db_stats` struct - `DBCreateData` functions (used in `DBMap::ensure()`) now return `DBData` instead of `void*` - `DBApply` functions (used in `DBMap::foreach()` and `DBMap::destroy()`) now take `DBData*` as a parameter instead of `void*` - `DBMatcher` functions (used in `DBMap::getall()`) now take `DBData` as a parameter instead of `void*` - `DBReleaser` functions now take `DBData` as parameter instead of `void*` - Default releasers release data if it is `void*` (`DB_DATA_PTR`) type - `DBIterator` functions: `first()`, `last()`, `next()` and `prev()` now return `DBData*` instead of `void*` - `DBIterator::remove()` now returns `int` (1 if node was found and removed, 0 otherwise) instead of `void*` and takes an extra `DBData*` parameter for the data of removed entry - `DBMap::get()` and `DBMap::ensure()` now return `DBData*` instead of `void*` - `DBMap::remove()` and `DBMap::put()` now return `int` (1 if node already existed, 0 otherwise) instead of `void*` and take an extra `DBData*` parameter for the data of removed entry - `DBMap::put()` now takes `DBData` as parameter instead of `void*` - `DBMap::getall()` now puts data into `DBData**` buffer instead of `void**` buffer - Updated macros: - (`i`/`ui`/`str`)`db_get` and (`i`/`ui`/`str`)`db_ensure` were wrapped with `db_data2ptr` to extract data from `DBData*` that is now returned by `DBMap::get()` - added (`i`/`ui`/`str`)`db_iget` and (`i`/`ui`/`str`)`db_uiget` that get `DBData` from `DBMap` and extract `int`/`unsigned int` from it (with `db_data2i`/`db_data2ui`) - `db_put`, `idb_put`, `uidb_put` and `strdb_put` data params were wrapped with `db_ptr2data` to match new signature of `DBMap::put()` (`DBData` instead of `void*`) - added (`i`/`ui`/`str`)`db_iput` and (`i`/`ui`/`str`)`db_uiput` that put `int`/`unsigned int` into `DBMap` (first wrapping it with `DBData`) - added `NULL` in place of extra parameter for removed data in `db_remove` macros - `dbi_first`, `dbi_last`, `dbi_next` and `dbi_prev` were wrapped with `db_data2ptr` to extract data from `DBData*` that is now returned by these `DBIterator` functions - Updated `DBMap` documentation, and fixed a dozen of typos in it. - Updated rest of rA code to reflect `DBMap` changes (mostly required signature changes of `DBCreateData` and `DBApply` functions). - Fixed a bug where `DBMap::put()` would return data of a deleted entry. - Removed some unnecessary pointer casts. - Increased `showmsg.c` static buffer size to fit entire DBMap stats report. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@15682 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/map/npc.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'src/map/npc.c') diff --git a/src/map/npc.c b/src/map/npc.c index 86d649cde..fb806cdd2 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -284,17 +284,19 @@ static int npc_event_export(struct npc_data *nd, int i) CREATE(ev, struct event_data, 1); ev->nd = nd; ev->pos = pos; - if (strdb_put(ev_db, buf, ev) != NULL) // There was already another event of the same name? + if (strdb_put(ev_db, buf, ev)) // There was already another event of the same name? return 1; } return 0; } int npc_event_sub(struct map_session_data* sd, struct event_data* ev, const char* eventname); //[Lance] -/*========================================== + +/** * 全てのNPCのOn*イベント実行 - *------------------------------------------*/ -int npc_event_doall_sub(DBKey key, void* data, va_list ap) + * @see DBApply + */ +int npc_event_doall_sub(DBKey key, DBData *data, va_list ap) { const char* p = key.str; struct event_data* ev; @@ -302,7 +304,7 @@ int npc_event_doall_sub(DBKey key, void* data, va_list ap) const char* name; int rid; - nullpo_ret(ev = (struct event_data *)data); + nullpo_ret(ev = db_data2ptr(data)); nullpo_ret(c = va_arg(ap, int *)); nullpo_ret(name = va_arg(ap, const char *)); rid = va_arg(ap, int); @@ -320,14 +322,17 @@ int npc_event_doall_sub(DBKey key, void* data, va_list ap) return 0; } -static int npc_event_do_sub(DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int npc_event_do_sub(DBKey key, DBData *data, va_list ap) { const char* p = key.str; struct event_data* ev; int* c; const char* name; - nullpo_ret(ev = (struct event_data *)data); + nullpo_ret(ev = db_data2ptr(data)); nullpo_ret(c = va_arg(ap, int *)); nullpo_ret(name = va_arg(ap, const char *)); @@ -1675,9 +1680,12 @@ int npc_remove_map(struct npc_data* nd) return 0; } -static int npc_unload_ev(DBKey key, void* data, va_list ap) +/** + * @see DBApply + */ +static int npc_unload_ev(DBKey key, DBData *data, va_list ap) { - struct event_data* ev = (struct event_data *)data; + struct event_data* ev = db_data2ptr(data); char* npcname = va_arg(ap, char *); if(strcmp(ev->nd->exname,npcname)==0){ @@ -2133,13 +2141,14 @@ static const char* npc_parse_shop(char* w1, char* w2, char* w3, char* w4, const return strchr(start,'\n');// continue } -/*========================================== +/** * NPCのラベルデータコンバート - *------------------------------------------*/ -int npc_convertlabel_db(DBKey key, void* data, va_list ap) + * @see DBApply + */ +int npc_convertlabel_db(DBKey key, DBData *data, va_list ap) { const char* lname = (const char*)key.str; - int lpos = (int)data; + int lpos = (int)db_data2ptr(data); struct npc_label_list** label_list; int* label_list_num; const char* filepath; @@ -2709,8 +2718,8 @@ void npc_setclass(struct npc_data* nd, short class_) static const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath) { DBMap* func_db; + DBData old_data; struct script_code *script; - struct script_code *oldscript; const char* end; const char* script_start; @@ -2732,9 +2741,9 @@ static const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, co return end; func_db = script_get_userfunc_db(); - oldscript = (struct script_code*)strdb_put(func_db, w3, script); - if( oldscript != NULL ) + if (func_db->put(func_db, db_str2key(w3), db_ptr2data(script), &old_data)) { + struct script_code *oldscript = db_data2ptr(&old_data); ShowInfo("npc_parse_function: Overwriting user function [%s] (%s:%d)\n", w3, filepath, strline(buffer,start-buffer)); script_free_vars(&oldscript->script_vars); aFree(oldscript->script_buf); @@ -3358,7 +3367,7 @@ void npc_read_event_script(void) { DBIterator* iter; DBKey key; - void* data; + DBData *data; char name[64]="::"; strncpy(name+2,config[i].event_name,62); @@ -3368,7 +3377,7 @@ void npc_read_event_script(void) for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) ) { const char* p = key.str; - struct event_data* ed = (struct event_data*) data; + struct event_data* ed = db_data2ptr(data); unsigned char count = script_event[i].event_count; if( count >= ARRAYLENGTH(script_event[i].event) ) -- cgit v1.2.3-60-g2f50