bool RegexUtil::replace(string& text, const string& pattern, const string& with, const bool& matchNewLine/* = false*/) { string ttext(text); string rettxt; regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); rettxt += ttext.substr(0, pm.rm_so) + with; } else { rettxt += ttext; break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } if(!cacheRegexes)regfree(®ex); if(ttext!="")rettxt += ttext; if(text==rettxt) return false; text = rettxt; while(replace(text, pattern, with, matchNewLine)) {} return true; }
void FacebookProto::UpdateChat(const char *chat_id, const char *id, const char *name, const char *message, DWORD timestamp, bool is_old) { // replace % to %% to not interfere with chat color codes std::string smessage = message; utils::text::replace_all(&smessage, "%", "%%"); ptrT tid(mir_a2t(id)); ptrT tnick(mir_a2t_cp(name, CP_UTF8)); ptrT ttext(mir_a2t_cp(smessage.c_str(), CP_UTF8)); ptrT tchat_id(mir_a2t(chat_id)); GCDEST gcd = { m_szModuleName, tchat_id, GC_EVENT_MESSAGE }; GCEVENT gce = { sizeof(gce), &gcd }; gce.ptszText = ttext; gce.time = timestamp ? timestamp : ::time(NULL); if (id != NULL) gce.bIsMe = !mir_strcmp(id, facy.self_.user_id.c_str()); gce.dwFlags |= GCEF_ADDTOLOG; if (is_old) { gce.dwFlags |= GCEF_NOTNOTIFY; gce.dwFlags &= ~GCEF_ADDTOLOG; } gce.ptszNick = tnick; gce.ptszUID = tid; CallServiceSync(MS_GC_EVENT, 0, reinterpret_cast<LPARAM>(&gce)); facy.erase_reader(ChatIDToHContact(chat_id)); }
vector<string> RegexUtil::search(const string& text, const string& pattern, const bool& matchNewLine/* = false*/) { vector<string> vec; string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); vec.push_back(match); } else { break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } if(!cacheRegexes)regfree(®ex); return vec; }
string RegexUtil::replaceCopy(const string& text, const string& pattern, const string& with, bool matchNewLine/* = false*/) { string ttext(text); string rettxt; regex_t regex; int reti; /*if(patterns.find(pattern)!=patterns.end()) { regex = patterns[pattern]; } else*/ { /* Compile regular expression */ int cflags = REG_EXTENDED; if(matchNewLine) cflags = REG_NEWLINE | REG_EXTENDED; reti = regcomp(®ex, pattern.c_str(), cflags); if(reti) { cout << ("Could not compile regex - "+pattern) << endl; } else { //patterns[pattern] = regex; } } regmatch_t pm; reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); rettxt += ttext.substr(0, pm.rm_so) + with; } else { rettxt += ttext; break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } regfree(®ex); if(ttext!="")rettxt += ttext; if(text!=rettxt) { while(replace(rettxt, pattern, with, matchNewLine)) {} } return rettxt; }
bool RegexUtil::matches(const string& text, const string& pattern, const bool& matchNewLine/* = false*/) { string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); if(!cacheRegexes)regfree(®ex); if (reti == 0) { /* while matches found */ return true; } return false; }
int RegexUtil::find(const string& text, const string& pattern, const bool& matchNewLine/* = false*/) { string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm; int reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); if(!cacheRegexes)regfree(®ex); if (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ return pm.rm_so; } return -1; }
vector<string> RegexUtil::findWithGroups(const string& text, const string& pattern, const int& groupCount, const bool& matchNewLine /*= false*/) { vector<string> data; string ttext(text); regex_t regex; getRegex(regex, pattern, matchNewLine); regmatch_t pm[groupCount]; int reti = regexec(®ex, ttext.c_str(), groupCount, pm, 0); if(!cacheRegexes)regfree(®ex); if (reti == 0) { /* while matches found */ for (int i = 0; pm[i].rm_so != -1; i++) { string co = ttext.substr(pm[i].rm_so, pm[i].rm_eo-pm[i].rm_so); data.push_back(co); } } return data; }
vector<string> RegexUtil::search(const string& text, const string& pattern, bool matchNewLine/* = false*/) { vector<string> vec; string ttext(text); regex_t regex; int reti; /*if(patterns.find(pattern)!=patterns.end()) { regex = patterns[pattern]; } else*/ { /* Compile regular expression */ int cflags = REG_EXTENDED; if(matchNewLine) cflags = REG_NEWLINE | REG_EXTENDED; reti = regcomp(®ex, pattern.c_str(), cflags); if(reti) { cout << ("Could not compile regex - "+pattern) << endl; } else { //patterns[pattern] = regex; } } regmatch_t pm; reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); vec.push_back(match); } else { break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } regfree(®ex); return vec; }
tstring Ansi2Tchar(const std::string& ansiString) // like atl 'A2T' { const char br[] = "\r\n"; TCHAR tbr[2 + 1]; ::OemToChar(br, tbr); tstring ttext(ansiString.begin(), ansiString.end()); ::OemToChar(ansiString.c_str(), &(ttext[0])); std::size_t pos = 0; while ((pos = ttext.find(tbr, pos)) != std::string::npos) { ttext.replace(pos, 2, TEXT("\r\n")); } return ttext; }
string RegexUtil::replace(const string& text, const string& pattern, const string& with) { string ttext(text); string rettxt; regex_t regex; int reti; if(patterns.find(pattern)!=patterns.end()) { regex = patterns[pattern]; } else { /* Compile regular expression */ reti = regcomp(®ex, pattern.c_str(), REG_EXTENDED); if(reti) { cout << "Could not compile regex\n" << endl; } else { patterns[pattern] = regex; } } regmatch_t pm; reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); rettxt += ttext.substr(0, pm.rm_so) + with; } else { rettxt += ttext; break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } if(ttext!="")rettxt += ttext; return rettxt; }
vector<string> RegexUtil::search(const string& text, const string& pattern) { vector<string> vec; string ttext(text); regex_t regex; int reti; if(patterns.find(pattern)!=patterns.end()) { regex = patterns[pattern]; } else { /* Compile regular expression */ reti = regcomp(®ex, pattern.c_str(), REG_EXTENDED); if(reti) { cout << "Could not compile regex\n" << endl; } else { patterns[pattern] = regex; } } regmatch_t pm; reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); while (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ if(!reti) { string match; match = ttext.substr(pm.rm_so, pm.rm_eo-pm.rm_so); vec.push_back(match); } else { break; } ttext = ttext.substr(pm.rm_eo); pm.rm_eo = -1; pm.rm_so = -1; reti = regexec (®ex, ttext.c_str(), 1, &pm, 0); } return vec; }
void RegexUtil::find(const string& text, const string& pattern, int &spos, int &epos, bool matchNewLine/* = false*/) { string ttext(text); regex_t regex; int reti; /*if(patterns.find(pattern)!=patterns.end()) { regex = patterns[pattern]; } else*/ { /* Compile regular expression */ int cflags = REG_EXTENDED; if(matchNewLine) cflags = REG_NEWLINE | REG_EXTENDED; reti = regcomp(®ex, pattern.c_str(), cflags); if(reti) { cout << ("Could not compile regex - "+pattern) << endl; } else { //patterns[pattern] = regex; } } spos = -1; epos = -1; regmatch_t pm; reti = regexec(®ex, ttext.c_str(), 1, &pm, 0); regfree(®ex); if (reti == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec() finds the next match */ spos = pm.rm_so; epos = pm.rm_eo; } }
// ============================================================================ // pxpByteCode // ============================================================================ pxpByteCode::pxpByteCode( pkgDecompiler* decompiler ) : Decompiler(decompiler) , CToken(NULL) , CTokenTree(NULL) , CTokenGroup(NULL) , CTokenGroupTree(NULL) , CTokenItem(NULL) , CTokenItemTree(NULL) , CTokenGroupCnd(NULL) , CTokenGroupCndTree(NULL) , unXmlParser() { // temp tree nodes unXmlParseTree bytecode ( wxT("bytecode") ); unXmlParseTree bgroup ( wxT("group") ); unXmlParseTree gname ( wxT("name") ); unXmlParseTree gmemo ( wxT("memo") ); unXmlParseTree gtoken ( wxT("token") ); unXmlParseTree tcode ( wxT("code") ); unXmlParseTree tname ( wxT("name") ); unXmlParseTree tdesc ( wxT("desc") ); unXmlParseTree tdata ( wxT("data") ); unXmlParseTree titem ( wxT("item") ); unXmlParseTree itype ( wxT("type") ); unXmlParseTree iname ( wxT("name") ); unXmlParseTree ttext ( wxT("text") ); unXmlParseTree gcond ( wxT("gcond") ); unXmlParseTree cif ( wxT("if") ); unXmlParseTree ceq ( wxT("eq") ); unXmlParseTree cthen ( wxT("then") ); unXmlParseTree cleft ( wxT("left") ); unXmlParseTree cright ( wxT("right") ); unXmlParseTree ctstream ( wxT("tstream") ); unXmlParseTree cnum ( wxT("num") ); unXmlParseTree nfunc ( wxT("nativefunctions") ); unXmlParseTree ffirst ( wxT("first") ); unXmlParseTree fextended ( wxT("extended") ); // token group - pre bgroup.AddCommand( new_xpObjCreate<pkgTokenGroup>(txpParseTree) ); bgroup.AddCommand( new_xpFunc1( this, &pxpByteCode::SetTokenGroup, txpTokenGroupObject ) ); bgroup.AddCommand( new_xpFunc1( this, &pxpByteCode::SetTokenGroupTree, txpParseTree ) ); gname.AddCommand( new_xpFunc1( txpTokenGroup, &pkgTokenGroup::SetName, txpNodeName(txpParseTree) ) ); gname.AddPostCommand( new_xpFunc1( Decompiler, &pkgDecompiler::AddTokenGroup, txpTokenGroup ) ); // token group - post bgroup.AddPostCommand( new_xpObjClear(txpParseTree) ); bgroup.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearTokenGroup ) ); bgroup.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearTokenGroupTree ) ); // gcond = pre gcond.AddCommand( new_xpObjCreate<pkgTokenCondition>(txpParseTree) ); gcond.AddCommand( new_xpFunc1( this, &pxpByteCode::SetTokenGroupCnd, txpTokenGroupCndObject ) ); gcond.AddCommand( new_xpFunc1( this, &pxpByteCode::SetTokenGroupCndTree, txpParseTree ) ); // gcond = post gcond.AddPostCommand( new_xpObjClear(txpParseTree) ); gcond.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearTokenGroupCnd ) ); gcond.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearTokenGroupCndTree ) ); // token - pre gtoken.AddCommand( new_xpObjCreate<dtToken>(txpParseTree) ); gtoken.AddCommand( new_xpFunc1( this, &pxpByteCode::SetToken, txpTokenObject ) ); gtoken.AddCommand( new_xpFunc1( this, &pxpByteCode::SetTokenTree, txpParseTree ) ); tcode.AddCommand( new_xpFunc1( txpTokenTreeObject, &dtToken::SetTokenData, txpNodeData(txpParseTree) ) ); tname.AddCommand( new_xpFunc1( txpTokenTreeObject, &dtToken::SetTokenName, txpNodeName(txpParseTree) ) ); tdesc.AddCommand( new_xpFunc1( txpTokenTreeObject, &dtToken::SetDesc, txpNodeName(txpParseTree) ) ); tdesc.AddCommand( new_xpFunc1( txpTokenGroup, &pkgTokenGroup::AddToken, txpToken ) ); tdesc.AddCommand( new_xpFunc2( Decompiler, &pkgDecompiler::AddToken, txpToken, txpTokenGroupCnd ) ); // token - post gtoken.AddPostCommand( new_xpFunc0( txpToken, &dtToken::DumpInfo ) ); gtoken.AddPostCommand( new_xpObjClear(txpParseTree) ); gtoken.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearToken ) ); gtoken.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearTokenTree ) ); // titem - pre titem.AddCommand( new_xpFunc1( this, &pxpByteCode::SetTokenItemTree, txpParseTree ) ); itype.AddCommand( new_xpObjFactory( txpTokenItemTree, &GDataTypeFactory, &pkgDataTypeFactory::Create, txpNodeName(txpParseTree) ) ); itype.AddCommand( new_xpFunc1( this, &pxpByteCode::SetTokenItem, txpTokenItemTreeObject ) ); iname.AddCommand( new_xpFunc1( txpTokenItem, &pkgDataType::SetDesc, txpNodeName(txpParseTree) ) ); // titem - post titem.AddPostCommand( new_xpFunc1( txpToken, &dtToken::AddItem, txpTokenItem ) ); titem.AddPostCommand( new_xpObjClear(txpParseTree) ); titem.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearTokenItem ) ); titem.AddPostCommand( new_xpFunc0( this, &pxpByteCode::ClearTokenItemTree ) ); ffirst.AddCommand( new_xpFunc1( Decompiler, &pkgDecompiler::SetFunctionIdFirst, txpNodeData(txpParseTree) ) ); fextended.AddCommand( new_xpFunc1( Decompiler, &pkgDecompiler::SetFunctionIdExtended, txpNodeData(txpParseTree) ) ); // construct tree starting from leaves // ie: node on right side *cannot* appear anywhere below on left side // token gtoken.AddChild( tcode, pl::one ); gtoken.AddChild( tname, pl::one ); gtoken.AddChild( tdesc, pl::one ); titem.AddChild( itype, pl::one ); titem.AddChild( iname, pl::one ); tdata.AddChild( titem, pl::minone ); tdata.AddChild( ttext, pl::maxone ); gtoken.AddChild( tdata, pl::maxone ); // if cleft.AddChild( ctstream, pl::maxone ); cleft.AddChild( cnum, pl::maxone ); cright.AddChild( ctstream, pl::maxone ); cright.AddChild( cnum, pl::maxone ); ceq.AddChild( cleft, pl::one ); ceq.AddChild( cright, pl::one ); cif.AddChild( ceq, pl::one ); // then cthen.AddChild( gtoken, pl::any ); // group bgroup.AddChild( gname, pl::one ); bgroup.AddChild( gmemo, pl::any ); gcond.AddChild( cif, pl::one ); gcond.AddChild( cthen, pl::one ); bgroup.AddChild( gcond, pl::maxone ); bgroup.AddChild( gtoken, pl::any ); // native functions nfunc.AddChild( fextended, pl::one ); nfunc.AddChild( ffirst, pl::one ); // bytecode bytecode.AddChild( bgroup, pl::minone ); bytecode.AddChild( nfunc, pl::maxone ); ParseTree = new unXmlParseTree( bytecode ); }