viewing paste regexp | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//===== Hercules Plugin ======================================
//= *regexp Script Command
//===== By: ==================================================
//= AnnieRuru
//===== Current Version: =====================================
//= 1.0
//===== Compatible With: =====================================
//= Hercules 2014-03-12
//===== Description: =========================================
//= check the string match with regular expression
//===== Topic ================================================
//= none
//===== Additional Comments: =================================  
//= rip off from rathena *preg_match
//============================================================
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../map/npc.h"
#include "../map/script.h"
#include "../common/HPMi.h"
#include "../../3rdparty/pcre/include/pcre.h"
 
#include "../common/HPMDataCheck.h" // should always be the last file included! (if you don't make it last, it'll intentionally break compile time)
 
HPExport struct hplugin_info pinfo = {
        "regexp",               // Plugin name
        SERVER_TYPE_MAP,// Which server types this plugin works with?
        "0.1",                  // Plugin version
        HPM_VERSION,    // HPM Version (don't change, macro is automatically updated)
};
 
BUILDIN(regexp) {
//#ifdef PCRE_SUPPORT
        pcre *re;
        pcre_extra *pcreExtra;
        const char *error;
        int erroffset, r, offset = 0;
        int subStrVec[30];
 
        const char* pattern = script_getstr(st,2);
        const char* subject = script_getstr(st,3);
        if (script_hasdata(st,4))
                offset = script_getnum(st,4);
 
        re = libpcre->compile(pattern, 0, &error, &erroffset, NULL);
        pcreExtra = libpcre->study(re, 0, &error);
 
        r = libpcre->exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
 
        libpcre->free(re);
        if (pcreExtra != NULL)
                libpcre->free(pcreExtra);
 
        if (r < 0)
                script_pushint(st,0);
        else
                script_pushint(st,(r > 0) ? r : 30 / 3);
 
        return true;
//#else
        ShowError( "buildin_regexp: Cannot run because PCRE library are disabled.\n" );
        script_pushint(st,0);
        return false;
//#endif
}
 
HPExport void plugin_init (void) {
        script = GET_SYMBOL("script");
        libpcre = GET_SYMBOL("libpcre");
 
        addScriptCommand( "regexp", "ss?", regexp );
}
Viewed 589 times, submitted by AnnieRuru.