Loading codec/c++/Makefile +2 −0 Original line number Diff line number Diff line Loading @@ -12,6 +12,8 @@ T3DK_CDGEN_HEADER = codec.h T3DK_CODETS = sip_codets.cpp T3DK_LIBS = -lboost_regex CPPFLAGS = -I. -I/opt/boost-mingw/include CC = gcc Loading codec/c++/sip_codets.cpp +48 −22 Original line number Diff line number Diff line #include "gen_classes.h" #include <regex.h> #include <boost/regex.hpp> namespace t3devlib { namespace gen { class Regex { public: Regex(const char* regex, int flags = 0) { typedef std::string::const_iterator iterator; flags |= REG_EXTENDED; if (regcomp (&mRegex, regex, flags)) Regex(const char* regex, int flags = 0) : mSource (regex), mRegex (regex, boost::regex_constants::extended) { std::cerr << "bad regex: " << regex << endl; abort(); } //FIXME: remove flags ? } bool Match (Buffer& buffer) { mStart = iterator (buffer.GetValue().c_str() + (buffer.GetPosition() / 8)); mStop = iterator (buffer.GetValue().c_str() + (buffer.GetEndMarker() / 8)); int result = boost::regex_search (mStart, mStop, mResults, mRegex); mStart = buffer.GetValue().c_str() + (buffer.GetPosition() / 8); //TODO: check that the regex is not match past the end marker of the buffer return !regexec (&mRegex, mStart, 16, mMatches, 0); boost::match_results<iterator>::iterator it; std::cout << endl << "matching /"<< mSource <<"/" << endl; int id=0; for (it=mResults.begin() ; it!=mResults.end() ; ++it) { std::cout << "first " << id << " *" << &*it->first << "*" << endl; std::cout << "second " << id << " *" << &*it->second << "*" << endl; id++; } return result; } void AssertMatch (Buffer& buffer, Variable* v) throw (DecodeError) { Loading @@ -29,34 +41,42 @@ public: } int GetMatchedLength(int id = 0) { return (mMatches[id].rm_eo - mMatches[id].rm_so)*8; return mResults[id].length()*8; } std::string GetMatchedString(int id = 0) { return std::string (mStart + mMatches[id].rm_so, mMatches[id].rm_eo - mMatches[id].rm_so); return std::string (mResults[id].first, mResults[id].second); } const char* GetMatchedPosition(int id = 0) { return mStart + mMatches[id].rm_so; return &*mResults[id].first; } const char GetLastMatchedChar(int id = 0) { return *(mStart + mMatches[id].rm_eo - 1); return *(&*mResults[id].second - 1); // FIXME: how about null match ? } void Error (Variable* v, Buffer& buffer) throw (DecodeError) { const char* start = buffer.GetValue().c_str() + (buffer.GetPosition() / 8); std::string message ("cannot match regex in '"); message.append (start, 8); message += "...'"; std::string message ("cannot match regex /"); message += mSource; message += "/ in '"; if ((mStop - mStart) > 40) { message.append (&*mStart, 40); message += "..."; } else { message.append (mStart, mStop); } message += "'"; throw DecodeError (v, message); } private: regex_t mRegex; regmatch_t mMatches[16]; const char* mStart; const char* mSource; boost::regex mRegex; iterator mStart, mStop; boost::match_results<iterator> mResults; }; void remove_whitespace (Buffer & buffer) { Loading Loading @@ -564,7 +584,7 @@ SipHeaderMap SipHeaderMap::msInstance; void MessageHeader::PreDecodeField (Buffer& buffer) throw (DecodeError) { static Regex reg_header_name ("([A-Za-z\\-]+)" SIPREG_HCOLON, REG_ICASE); static Regex reg_header_name ("^([A-Za-z\\-]+)" SIPREG_HCOLON, REG_ICASE); if (reg_header_name.Match (buffer)) { Loading @@ -574,6 +594,12 @@ void MessageHeader::PreDecodeField (Buffer& buffer) throw (DecodeError) } } void MessageHeader::PostDecodeField (int id, Buffer& buffer) throw (DecodeError) { //TODO: match CRLF throw DecodeError (this, __PRETTY_FUNCTION__); } const char* FieldName::msFields[] = { "ACCEPT_E", Loading Loading
codec/c++/Makefile +2 −0 Original line number Diff line number Diff line Loading @@ -12,6 +12,8 @@ T3DK_CDGEN_HEADER = codec.h T3DK_CODETS = sip_codets.cpp T3DK_LIBS = -lboost_regex CPPFLAGS = -I. -I/opt/boost-mingw/include CC = gcc Loading
codec/c++/sip_codets.cpp +48 −22 Original line number Diff line number Diff line #include "gen_classes.h" #include <regex.h> #include <boost/regex.hpp> namespace t3devlib { namespace gen { class Regex { public: Regex(const char* regex, int flags = 0) { typedef std::string::const_iterator iterator; flags |= REG_EXTENDED; if (regcomp (&mRegex, regex, flags)) Regex(const char* regex, int flags = 0) : mSource (regex), mRegex (regex, boost::regex_constants::extended) { std::cerr << "bad regex: " << regex << endl; abort(); } //FIXME: remove flags ? } bool Match (Buffer& buffer) { mStart = iterator (buffer.GetValue().c_str() + (buffer.GetPosition() / 8)); mStop = iterator (buffer.GetValue().c_str() + (buffer.GetEndMarker() / 8)); int result = boost::regex_search (mStart, mStop, mResults, mRegex); mStart = buffer.GetValue().c_str() + (buffer.GetPosition() / 8); //TODO: check that the regex is not match past the end marker of the buffer return !regexec (&mRegex, mStart, 16, mMatches, 0); boost::match_results<iterator>::iterator it; std::cout << endl << "matching /"<< mSource <<"/" << endl; int id=0; for (it=mResults.begin() ; it!=mResults.end() ; ++it) { std::cout << "first " << id << " *" << &*it->first << "*" << endl; std::cout << "second " << id << " *" << &*it->second << "*" << endl; id++; } return result; } void AssertMatch (Buffer& buffer, Variable* v) throw (DecodeError) { Loading @@ -29,34 +41,42 @@ public: } int GetMatchedLength(int id = 0) { return (mMatches[id].rm_eo - mMatches[id].rm_so)*8; return mResults[id].length()*8; } std::string GetMatchedString(int id = 0) { return std::string (mStart + mMatches[id].rm_so, mMatches[id].rm_eo - mMatches[id].rm_so); return std::string (mResults[id].first, mResults[id].second); } const char* GetMatchedPosition(int id = 0) { return mStart + mMatches[id].rm_so; return &*mResults[id].first; } const char GetLastMatchedChar(int id = 0) { return *(mStart + mMatches[id].rm_eo - 1); return *(&*mResults[id].second - 1); // FIXME: how about null match ? } void Error (Variable* v, Buffer& buffer) throw (DecodeError) { const char* start = buffer.GetValue().c_str() + (buffer.GetPosition() / 8); std::string message ("cannot match regex in '"); message.append (start, 8); message += "...'"; std::string message ("cannot match regex /"); message += mSource; message += "/ in '"; if ((mStop - mStart) > 40) { message.append (&*mStart, 40); message += "..."; } else { message.append (mStart, mStop); } message += "'"; throw DecodeError (v, message); } private: regex_t mRegex; regmatch_t mMatches[16]; const char* mStart; const char* mSource; boost::regex mRegex; iterator mStart, mStop; boost::match_results<iterator> mResults; }; void remove_whitespace (Buffer & buffer) { Loading Loading @@ -564,7 +584,7 @@ SipHeaderMap SipHeaderMap::msInstance; void MessageHeader::PreDecodeField (Buffer& buffer) throw (DecodeError) { static Regex reg_header_name ("([A-Za-z\\-]+)" SIPREG_HCOLON, REG_ICASE); static Regex reg_header_name ("^([A-Za-z\\-]+)" SIPREG_HCOLON, REG_ICASE); if (reg_header_name.Match (buffer)) { Loading @@ -574,6 +594,12 @@ void MessageHeader::PreDecodeField (Buffer& buffer) throw (DecodeError) } } void MessageHeader::PostDecodeField (int id, Buffer& buffer) throw (DecodeError) { //TODO: match CRLF throw DecodeError (this, __PRETTY_FUNCTION__); } const char* FieldName::msFields[] = { "ACCEPT_E", Loading