From e0a18f0853dd7a0cf0533ae0d10da9d19b116767 Mon Sep 17 00:00:00 2001 From: FlavioJS Date: Mon, 9 Apr 2007 20:21:17 +0000 Subject: * Recoded and renamed the trim function in strlib to normalize_name. (didn't behave like a standard trim function, see function comment for what it does) * Added a proper trim function to strlib. * Other minor cleanups. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@10199 54d463be-8e91-2dee-dedb-b68131a5f0ec --- Changelog-Trunk.txt | 2 ++ src/char/char.c | 2 +- src/char_sql/char.c | 2 +- src/common/strlib.c | 75 +++++++++++++++++++++++++++++++++++++++++------------ src/common/strlib.h | 3 ++- src/map/atcommand.c | 2 +- src/map/npc.c | 36 +++++++++++++------------ src/map/npc.h | 4 +-- src/map/script.c | 4 +-- 9 files changed, 88 insertions(+), 42 deletions(-) diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt index ff3eef8df..ed35d01b3 100644 --- a/Changelog-Trunk.txt +++ b/Changelog-Trunk.txt @@ -4,6 +4,8 @@ AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK. 2007/04/10 + * Recoded and renamed the trim function in strlib to normalize_name. + * Added a proper trim function to strlib. * Cleanups at npc source file adding/removing. * Changed db key from 'unsigned char*' to 'const char*'. * Defined out the dump function in util.h/c. [FlavioJS] diff --git a/src/char/char.c b/src/char/char.c index 705579fbf..0d888f399 100644 --- a/src/char/char.c +++ b/src/char/char.c @@ -1118,7 +1118,7 @@ int make_new_char(int fd, unsigned char *dat) { strncpy(name, dat, NAME_LENGTH); name[NAME_LENGTH-1] = '\0'; //Trunc name to max possible value (23) - trim(name,TRIM_CHARS); //Trim character name. [Skotlex] + normalize_name(name,TRIM_CHARS);//Normalize character name. [Skotlex] //check name != main chat nick [LuzZza] if(strcmpi(name, main_chat_nick) == 0) { diff --git a/src/char_sql/char.c b/src/char_sql/char.c index 62607e4e9..41ce64de7 100644 --- a/src/char_sql/char.c +++ b/src/char_sql/char.c @@ -1256,7 +1256,7 @@ int make_new_char_sql(int fd, unsigned char *dat) { strncpy(name, dat, NAME_LENGTH); name[NAME_LENGTH-1] = '\0'; //Always terminate string. - trim(name,TRIM_CHARS); //Trim character name. [Skotlex] + normalize_name(name,TRIM_CHARS); //Normalize character name. [Skotlex] jstrescapecpy(t_name, name); // disabled until fixed >.> diff --git a/src/common/strlib.c b/src/common/strlib.c index bd49e7b61..9cac2ca4f 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -123,27 +123,70 @@ int remove_control_chars(char* str) return change; } -//Trims a string, also removes illegal characters such as \t and reduces continous spaces to a single one. by [Foruken] -char* trim(char* str, const char* delim) +// Removes characters identified by ISSPACE from the start and end of the string +// NOTE: make sure the string is not const!! +char* trim(char* str) { - char* strp = strtok(str,delim); - char buf[1024]; - char* bufp = buf; - memset(buf,0,sizeof buf); - - while(strp) { - strcpy(bufp, strp); - bufp = bufp + strlen(strp); - strp = strtok(NULL, delim); - if (strp) { - strcpy(bufp," "); - bufp++; - } + size_t start; + size_t end; + + if( str == NULL ) + return str; + + // get start position + for( start = 0; str[start] && ISSPACE(str[start]); ++start ) + ; + // get end position + for( end = strlen(str); start < end && str[end-1] && ISSPACE(str[end-1]); --end ) + ; + // trim + if( start == end ) + *str = '\0';// empty string + else + {// move string with nul terminator + str[end] = '\0'; + memmove(str,str+start,end-start+1); } - strcpy(str,buf); return str; } +// Converts one or more consecutive occurences of the delimiters into a single space +// and removes such occurences from the beginning and end of string +// NOTE: make sure the string is not const!! +char* normalize_name(char* str,const char* delims) +{ + char* in = str; + char* out = str; + int put_space = 0; + + if( str == NULL || delims == NULL ) + return str; + + // trim start of string + while( *in && strchr(delims,*in) ) + ++in; + while( *in ) + { + if( put_space ) + {// replace trim characters with a single space + *out = ' '; + ++out; + } + // copy non trim characters + while( *in && !strchr(delims,*in) ) + { + *out = *in; + ++out; + ++in; + } + // skip trim characters + while( *in && strchr(delims,*in) ) + ++in; + put_space = 1; + } + *out = '\0'; + return str; +} //stristr: Case insensitive version of strstr, code taken from //http://www.daniweb.com/code/snippet313.html, Dave Sinkula diff --git a/src/common/strlib.h b/src/common/strlib.h index bf26c2172..db54d2969 100644 --- a/src/common/strlib.h +++ b/src/common/strlib.h @@ -9,7 +9,8 @@ char* jstrescapecpy (char* pt, const char* spt); int jmemescapecpy (char* pt, const char* spt, int size); int remove_control_chars(char *); -char *trim(char *str, const char *delim); +char* trim(char* str); +char* normalize_name(char* str,const char* delims); const char *stristr(const char *haystack, const char *needle); #ifdef __WIN32 diff --git a/src/map/atcommand.c b/src/map/atcommand.c index 148cd5673..df10655f7 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -5898,7 +5898,7 @@ int atcommand_loadnpc(const int fd, struct map_session_data* sd, const char* com // add to list of script sources and run it npc_addsrcfile(message); - npc_parsesrcfile((char *)message); + npc_parsesrcfile(message); npc_read_event_script(); clif_displaymessage(fd, msg_txt(262)); diff --git a/src/map/npc.c b/src/map/npc.c index 20b7206d4..16152ecfa 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -46,7 +46,7 @@ static int npc_script=0; static int npc_mob=0; static int npc_delay_mob=0; static int npc_cache_mob=0; -char *current_file = NULL; +const char *current_file = NULL; int npc_get_new_npc_id(void){ return npc_id++; } static struct dbt *ev_db; @@ -63,7 +63,7 @@ static struct eri *timer_event_ers; //For the npc timer data. [Skotlex] //For holding the view data of npc classes. [Skotlex] static struct view_data npc_viewdb[MAX_NPC_CLASS]; -static struct +static struct script_event_s { //Holds pointers to the commonly executed scripts for speedup. [Skotlex] struct npc_data *nd; struct event_data *event[UCHAR_MAX]; @@ -2795,7 +2795,7 @@ static int npc_parse_mapcell (char *w1, char *w2, char *w3, char *w4) return 0; } -void npc_parsesrcfile (char *name) +void npc_parsesrcfile(const char* name) { int m, lines = 0; char line[2048]; @@ -2876,6 +2876,7 @@ void npc_parsesrcfile (char *name) } } fclose(fp); + current_file = NULL; return; } @@ -3135,17 +3136,17 @@ static void npc_debug_warps(void) */ int do_init_npc(void) { - struct npc_src_list *nsl; - time_t last_time = time(0); - int busy, i; + struct npc_src_list *file; + time_t last_time = time(NULL); + int busy = 0; + int i; char c = '-'; //Stock view data for normal npcs. memset(&npc_viewdb, 0, sizeof(npc_viewdb)); npc_viewdb[0].class_ = INVISIBLE_CLASS; //Invisible class is stored here. - for (busy = 1; busy < MAX_NPC_CLASS; busy++) - npc_viewdb[busy].class_ = busy; - busy = 0; + for( i = 1; i < MAX_NPC_CLASS; i++ ) + npc_viewdb[i].class_ = i; // comparing only the first 24 chars of labels that are 50 chars long isn't that nice // will cause "duplicated" labels where actually no dup is... @@ -3155,16 +3156,17 @@ int do_init_npc(void) memset(&ev_tm_b, -1, sizeof(ev_tm_b)); timer_event_ers = ers_new(sizeof(struct timer_event_data)); - for (nsl = npc_src_files; nsl; nsl = nsl->next) { - npc_parsesrcfile(nsl->name); - current_file = NULL; + for( file = npc_src_files; file != NULL; file = file->next) { + npc_parsesrcfile(file->name); printf("\r"); if (script_config.verbose_mode) - ShowStatus ("Loading NPCs... %-53s", nsl->name); - else { + ShowStatus("Loading NPCs... %-53s", file->name); + else + { ShowStatus("Loading NPCs... Working: "); - if (last_time != time(0)) { - last_time = time(0); + if (last_time != time(NULL)) + {// change character at least every second + last_time = time(NULL); switch(busy) { case 0: c='\\'; busy++; break; case 1: c='|'; busy++; break; @@ -3197,7 +3199,7 @@ int do_init_npc(void) add_timer_func_list(npc_timerevent,"npc_timerevent"); // Init dummy NPC - fake_nd = (struct npc_data *)aCalloc(sizeof(struct npc_data),1); + fake_nd = (struct npc_data *)aMalloc(sizeof(struct npc_data)); fake_nd->bl.prev = fake_nd->bl.next = NULL; fake_nd->bl.m = -1; fake_nd->bl.x = 0; diff --git a/src/map/npc.h b/src/map/npc.h index accfc13e2..0ef12de7f 100644 --- a/src/map/npc.h +++ b/src/map/npc.h @@ -65,7 +65,7 @@ int npc_get_new_npc_id(void); void npc_addsrcfile(const char* name); void npc_delsrcfile(const char* name); -void npc_parsesrcfile(char *); +void npc_parsesrcfile(const char* name); int do_final_npc(void); int do_init_npc(void); int npc_event_do_oninit(void); @@ -87,8 +87,6 @@ int npc_reload(void); void npc_read_event_script(void); int npc_script_event(TBL_PC* sd, int type); -extern char *current_file; - struct npc_data *fake_nd; #endif /* _NPC_H_ */ diff --git a/src/map/script.c b/src/map/script.c index afa7146b0..f0d491176 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -22,7 +22,6 @@ #include "itemdb.h" #include "pc.h" #include "status.h" -#include "script.h" #include "storage.h" #include "mob.h" #include "npc.h" @@ -40,6 +39,7 @@ #include "unit.h" #include "irc.h" #include "pet.h" +#include "script.h" #include #include @@ -346,7 +346,7 @@ static void check_event(struct script_state *st, const char *evt) * 文字列のハッシュを計算 *------------------------------------------ */ -#define calc_hash(x) calc_hash2(x)%SCRIPT_HASH_SIZE +#define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE) static unsigned int calc_hash2(const unsigned char *p) { #if defined(SCRIPT_HASH_DJB2) -- cgit v1.2.3-60-g2f50