00001 00002 #ifndef STRINGFORMAT_H_ 00003 #define STRINGFORMAT_H_ 00004 00005 #include "boost/xpressive/xpressive.hpp" 00006 00007 namespace ariba { 00008 namespace utility { 00009 namespace string_format { 00010 00011 using boost::xpressive::sregex; 00012 00013 class regex_nav { 00014 private: 00015 typedef boost::xpressive::smatch _match; 00016 typedef _match::nested_results_type nested_results; 00017 typedef nested_results::const_iterator nested_iterator; 00018 const _match& match; 00019 00020 public: 00021 regex_nav(const _match& match) : 00022 match(match) { 00023 } 00024 00025 regex_nav() : 00026 match(*((const _match*) NULL)) { 00027 } 00028 00029 bool matched() const { 00030 return &match != NULL; 00031 } 00032 00033 regex_nav operator[] (const sregex& type) const { 00034 const nested_results& nr = match.nested_results(); 00035 for (nested_iterator i = nr.begin(); i != nr.end(); i++) { 00036 if (i->regex_id() == type.regex_id()) return regex_nav(*i); 00037 } 00038 return regex_nav(); 00039 } 00040 00041 regex_nav operator[](int index) const { 00042 const nested_results& nr = match.nested_results(); 00043 for (nested_iterator i = nr.begin(); i != nr.end() && index >= 0; i++) { 00044 if (index == 0) return regex_nav(*i); 00045 index--; 00046 } 00047 return regex_nav(); 00048 } 00049 00050 int size() const { 00051 return match.nested_results().size(); 00052 } 00053 00054 std::string str() const { 00055 if (!matched()) return std::string("<no match>"); 00056 return match[0].str(); 00057 } 00058 }; 00059 00060 // regex: string 00061 extern const sregex rstring; 00062 00063 // regex: base64 encoding 00064 extern const sregex rbase64; 00065 00066 // regex: raw alphabet 00067 extern const sregex rchars; 00068 00069 // regex: integer 00070 extern const sregex rint; 00071 00072 // regex: binary label 00073 extern const sregex rlabel; 00074 00075 // regex: dot separated identifier 00076 extern const sregex rid; 00077 00078 // regex: "leaf" data 00079 extern const sregex rdata; 00080 00081 // regex: fields 00082 extern const sregex rfield_label; 00083 extern const sregex rfield; 00084 extern const sregex rfields; 00085 00086 // regex: objects 00087 extern const sregex robject_data; 00088 extern const sregex robject_id; 00089 extern const sregex robject; 00090 extern const sregex robjects; 00091 00092 }}} 00093 00094 #endif /* STRINGFORMAT_H_ */