OOP _gst_re_search (OOP srcOOP, OOP patternOOP, int from, int to) { int res = 0; const char *src; struct pre_pattern_buffer *regex; struct pre_registers *regs; RegexCaching caching; OOP resultOOP; if (!regexClassOOP) init_re (); caching = lookupRegex (patternOOP, ®ex); if (caching != REGEX_CACHE_HIT && compileRegex (patternOOP, regex) != NULL) return NULL; /* now search */ src = &STRING_OOP_AT (OOP_TO_OBJ (srcOOP), 1); regs = (struct pre_registers *) calloc (1, sizeof (struct pre_registers)); res = pre_search (regex, src, to, from - 1, to - from + 1, regs); if (caching == REGEX_NOT_CACHED) pre_free_pattern (regex); resultOOP = make_re_results (srcOOP, regs); pre_free_registers(regs); free(regs); return resultOOP; }
string DecodedDataDocumentParser::replaceSocamAccelerometerTags (string text) { regex_t regex; const char* tag_regex_text = "<\\s*accelerometer\\s*/>"; compileRegex(®ex, tag_regex_text); regmatch_t* groups = new regmatch_t[1]; //string text(to_match); while (1) { const char * p = text.c_str(); int nomatch = regexec(®ex, p, 1, groups, 0); if (nomatch) { break; } regmatch_t match = groups[0]; if (groups[0].rm_so == -1) { break; } string tag = text.substr (match.rm_so,match.rm_eo-match.rm_so); cout << "Matched(" << match.rm_so << "," << match.rm_eo << "): " << tag << endl; SOCAMAPI::SocamAccelerometerApi api; api.refreshCoord(); int x = api.getCoord(SOCAMAPI::COORD_X); int y = api.getCoord(SOCAMAPI::COORD_Y); int z = api.getCoord(SOCAMAPI::COORD_Z); std::ostringstream oss; oss << "<div class='S_AC_DIV'> <table class='S_AC_TABLE'>" << "" << "<tr><td>" << x << "</td><td>" << y << "</td><td>" << z << "</td></tr></table></div>"; string newTagContent = oss.str(); text.replace(match.rm_so,match.rm_eo-match.rm_so,newTagContent); cout<<"Replaced:" <<text<<endl; } regfree (®ex); return text; }
/* Create a Regex object. We look for one that points to the same string in the cache (so that we can optimize a loop that repeatedly calls asRegex; if none is found, we create one ex-novo. Note that Regex and String objects have the same layout; only, Regexes are read-only so that we can support this kind of "interning" them. */ OOP _gst_re_make_cacheable (OOP patternOOP) { OOP regexOOP; const char *pattern; char *regex; struct pre_pattern_buffer *compiled; int patternLength; int i; if (!regexClassOOP) init_re (); if (IS_OOP_READONLY (patternOOP)) return patternOOP; /* Search in the cache */ patternLength = _gst_basic_size (patternOOP); pattern = &STRING_OOP_AT (OOP_TO_OBJ (patternOOP), 1); for (i = 0; i < REGEX_CACHE_SIZE; i++) { if (!cache[i].regex) break; regexOOP = cache[i].patternOOP; regex = &STRING_OOP_AT (OOP_TO_OBJ (regexOOP), 1); if (_gst_basic_size (regexOOP) == patternLength && memcmp (regex, pattern, patternLength) == 0) { markRegexAsMRU (i); return regexOOP; } } /* No way, must allocate a new Regex object */ regexOOP = _gst_object_alloc (regexClassOOP, patternLength); regex = &STRING_OOP_AT (OOP_TO_OBJ (regexOOP), 1); memcpy (regex, pattern, patternLength); /* Put it in the cache (we must compile it to check that it * is well-formed). */ lookupRegex (regexOOP, &compiled); if (compileRegex (patternOOP, compiled) != NULL) return _gst_nil_oop; else return regexOOP; }
int _gst_re_match (OOP srcOOP, OOP patternOOP, int from, int to) { int res = 0; const char *src; struct pre_pattern_buffer *regex; RegexCaching caching; if (!regexClassOOP) init_re (); caching = lookupRegex (patternOOP, ®ex); if (caching != REGEX_CACHE_HIT && compileRegex (patternOOP, regex) != NULL) return -100; /* now search */ src = &STRING_OOP_AT (OOP_TO_OBJ (srcOOP), 1); res = pre_match (regex, src, to, from - 1, NULL); if (caching == REGEX_NOT_CACHED) pre_free_pattern (regex); return res; }
string DecodedDataDocumentParser::replaceSocamPersonTags (string text) { regex_t regex; const char* tag_regex_text = "<\\s*person\\s*>([^,]*),([^<]*)<\\s*/person\\s*>"; compileRegex(®ex, tag_regex_text); size_t ngroups = regex.re_nsub + 1; regmatch_t *groups = (regmatch_t *) malloc(ngroups * sizeof(regmatch_t)); //string text(to_match); while (1) { const char * p = text.c_str(); int nomatch = regexec(®ex, p, ngroups, groups, 0); if (nomatch) { break; } regmatch_t match = groups[0]; if (groups[0].rm_so == -1) { break; } string name, surname; size_t nmatched; for (nmatched = 0; nmatched < ngroups; nmatched++) { if (groups[nmatched].rm_so == (size_t)(-1)) { break; } string matchStr = text.substr (groups[nmatched].rm_so,groups[nmatched].rm_eo-groups[nmatched].rm_so); cout << "Match: "<< matchStr << endl; if(nmatched == 1) { name = matchStr; } else if(nmatched == 2) { surname = matchStr; } } cout << "Parsed: name=" << name << " surname=" << surname << endl; string tag = text.substr (match.rm_so,match.rm_eo-match.rm_so); QContact contact = getContact(name, surname); //qWarning("\tContact ID: %d -- UUID: %s",contact.localId(),contact.details("Guid").at(0).value("Guid").toStdString().c_str()); //qWarning("\t\tDisplayLabel: %s",contact.details("DisplayLabel").at(0).value("Label").toStdString().c_str()); //qWarning("\t\tEmail %s",contact.details("EmailAddress").at(0).value("EmailAddress").toStdString().c_str()); //qWarning("\t\tPhoneNumber: %s",contact.details("PhoneNumber").at(0).value("PhoneNumber").toStdString().c_str()); /* QContactDetail(name="DisplayLabel", key=7, "Label"=QVariant(QString, "Self Simulator") ); QContactDetail(name="Type", key=10, "Type"=QVariant(QString, "Contact") ); QContactDetail(name="Name", key=13, "CustomLabel"=QVariant(QString, "Self Simulator") , "FirstName"=QVariant(QString, "Self") , "LastName"=QVariant(QString, "Simulator") ); QContactDetail(name="EmailAddress", key=16, "EmailAddress"=QVariant(QString, "*****@*****.**") ); QContactDetail(name="PhoneNumber", key=19, "PhoneNumber"=QVariant(QString, "+44123456789") ); QContactDetail(name="Address", key=22, "Country"=QVariant(QString, "UK") , "Locality"=QVariant(QString, "Leister") , "Street"=QVariant(QString, "56 Edmonton Square") ); QContactDetail(name="Guid", key=25, "Guid"=QVariant(QString, "0") ); QContactDetail(name="Timestamp", key=28, "CreationTimestamp"=QVariant(QDateTime, QDateTime("dom 13. mar 12:21:09 2011") ) , "ModificationTimestamp"=QVariant(QDateTime, QDateTime("dom 13. mar 12:21:09 2011") ) ); */ ostringstream oss ; oss << "<table>"; oss << "<tr>"; if(contact.details("DisplayLabel").count()>0) { oss << "<td>" << contact.details("DisplayLabel").at(0).value("Label").toStdString() << "</td>"; } oss << "</tr>"; oss << "<tr>"; if(contact.details("EmailAddress").count()>0) { oss << "<td>" << contact.details("EmailAddress").at(0).value("EmailAddress").toStdString() << "</td>"; } oss << "</tr>"; oss << "<tr>"; if(contact.details("PhoneNumber").count()>0) { oss << "<td>" << contact.details("PhoneNumber").at(0).value("PhoneNumber").toStdString() << "</td>"; } oss << "</tr>"; oss << "</table>"; string newTagContent = oss.str(); text.replace(match.rm_so,match.rm_eo-match.rm_so,newTagContent); cout<<"Replaced:" <<text<<endl; } regfree (®ex); return text; }