diff options
author | shennetsind <ind@henn.et> | 2014-01-18 22:02:33 -0200 |
---|---|---|
committer | shennetsind <ind@henn.et> | 2014-01-18 22:02:33 -0200 |
commit | c32a22c52b75ae8c3bc2064110318f4ad1d6954a (patch) | |
tree | b63a2ba9db0e7825b3365fa53fa8ddd8a35315b3 /src/map | |
parent | 14f84fc044754d22740905b7fa90022b4a04b0d8 (diff) | |
download | hercules-c32a22c52b75ae8c3bc2064110318f4ad1d6954a.tar.gz hercules-c32a22c52b75ae8c3bc2064110318f4ad1d6954a.tar.bz2 hercules-c32a22c52b75ae8c3bc2064110318f4ad1d6954a.tar.xz hercules-c32a22c52b75ae8c3bc2064110318f4ad1d6954a.zip |
PCRE Interface
Plugins may now take advantage of the pcre support within the core.
Thanks to Haruna!
Signed-off-by: shennetsind <ind@henn.et>
Diffstat (limited to 'src/map')
-rw-r--r-- | src/map/map.c | 1 | ||||
-rw-r--r-- | src/map/npc.h | 19 | ||||
-rw-r--r-- | src/map/npc_chat.c | 27 |
3 files changed, 41 insertions, 6 deletions
diff --git a/src/map/map.c b/src/map/map.c index 8301caa5b..bb9e53cdb 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -5371,6 +5371,7 @@ void map_hp_symbols(void) { HPM->share(quest,"quest"); #ifdef PCRE_SUPPORT HPM->share(npc_chat,"npc_chat"); + HPM->share(libpcre,"libpcre"); #endif HPM->share(mapit,"mapit"); HPM->share(mapindex,"mapindex"); diff --git a/src/map/npc.h b/src/map/npc.h index 2f4401bf7..df3c1729b 100644 --- a/src/map/npc.h +++ b/src/map/npc.h @@ -323,6 +323,25 @@ struct npc_chat_interface { struct npc_chat_interface *npc_chat; +/** + * pcre interface (libpcre) + * so that plugins may share and take advantage of the core's pcre + **/ +struct pcre_interface { + pcre *(*compile) (const char *pattern, int options, const char **errptr, int *erroffset, const unsigned char *tableptr); + pcre_extra *(*study) (const pcre *code, int options, const char **errptr); + int (*exec) (const pcre *code, const pcre_extra *extra, PCRE_SPTR subject, int length, int startoffset, int options, int *ovector, int ovecsize); + void (*free) (void *ptr); + int (*copy_substring) (const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int buffersize); + void (*free_substring) (const char *stringptr); + int (*copy_named_substring) (const pcre *code, const char *subject, int *ovector, int stringcount, const char *stringname, char *buffer, int buffersize); +}; + +struct pcre_interface *libpcre; + +/** + * Also defaults libpcre + **/ void npc_chat_defaults(void); #endif diff --git a/src/map/npc_chat.c b/src/map/npc_chat.c index 3f4a1a1c1..896bbae5b 100644 --- a/src/map/npc_chat.c +++ b/src/map/npc_chat.c @@ -22,7 +22,12 @@ #include <string.h> #include <stdarg.h> +/** + * interface sources + **/ struct npc_chat_interface npc_chat_s; +struct pcre_interface libpcre_s; + /** * Written by MouseJstr in a vision... (2/21/2005) @@ -79,8 +84,8 @@ struct npc_chat_interface npc_chat_s; */ void finalize_pcrematch_entry(struct pcrematch_entry* e) { - pcre_free(e->pcre_); - pcre_free(e->pcre_extra_); + libpcre->free(e->pcre_); + libpcre->free(e->pcre_extra_); aFree(e->pattern); aFree(e->label); } @@ -287,8 +292,8 @@ void npc_chat_def_pattern(struct npc_data* nd, int setid, const char* pattern, c struct pcrematch_entry *e = npc_chat->create_pcrematch_entry(s); e->pattern = aStrdup(pattern); e->label = aStrdup(label); - e->pcre_ = pcre_compile(pattern, PCRE_CASELESS, &err, &erroff, NULL); - e->pcre_extra_ = pcre_study(e->pcre_, 0, &err); + e->pcre_ = libpcre->compile(pattern, PCRE_CASELESS, &err, &erroff, NULL); + e->pcre_extra_ = libpcre->study(e->pcre_, 0, &err); } /** @@ -344,7 +349,7 @@ int npc_chat_sub(struct block_list* bl, va_list ap) int offsets[2*10 + 10]; // 1/3 reserved for temp space requred by pcre_exec // perform pattern match - int r = pcre_exec(e->pcre_, e->pcre_extra_, msg, len, 0, 0, offsets, ARRAYLENGTH(offsets)); + int r = libpcre->exec(e->pcre_, e->pcre_extra_, msg, len, 0, 0, offsets, ARRAYLENGTH(offsets)); if (r > 0) { // save out the matched strings @@ -352,7 +357,7 @@ int npc_chat_sub(struct block_list* bl, va_list ap) { char var[6], val[255]; snprintf(var, sizeof(var), "$@p%i$", i); - pcre_copy_substring(msg, offsets, r, i, val, sizeof(val)); + libpcre->copy_substring(msg, offsets, r, i, val, sizeof(val)); script->set_var(sd, var, val); } @@ -425,6 +430,16 @@ void npc_chat_defaults(void) { npc_chat->activate_pcreset = activate_pcreset; npc_chat->lookup_pcreset = lookup_pcreset; npc_chat->finalize_pcrematch_entry = finalize_pcrematch_entry; + + libpcre = &libpcre_s; + + libpcre->compile = pcre_compile; + libpcre->study = pcre_study; + libpcre->exec = pcre_exec; + libpcre->free = pcre_free; + libpcre->copy_substring = pcre_copy_substring; + libpcre->free_substring = pcre_free_substring; + libpcre->copy_named_substring = pcre_copy_named_substring; } #endif //PCRE_SUPPORT |