The vbs tools - vbs_ls, vbs_rm, vbs_fs - for listing, removing and mounting vbs and Mark6 format scattered VLBI recordings on FlexBuff and Mark6 systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
3.3 KiB

  1. // Implementation of the RegularExpression class
  2. #include <regular_expression.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <cstring>
  7. using std::cerr;
  8. using std::endl;
  9. using std::flush;
  10. using std::string;
  11. DEFINE_EZEXCEPT(Regular_Expression_Exception)
  12. // support classes
  13. pcintregmatch_t::pcintregmatch_t() {
  14. this->::regmatch_t::rm_so = -1;
  15. this->::regmatch_t::rm_eo = -1;
  16. }
  17. pcintregmatch_t::pcintregmatch_t( const ::regmatch_t& rm ) {
  18. this->::regmatch_t::rm_so = rm.rm_so;
  19. this->::regmatch_t::rm_eo = rm.rm_eo;
  20. }
  21. pcintregmatch_t::operator bool() const {
  22. return (rm_so!=-1 && rm_eo!=-1);
  23. }
  24. matchresult::matchresult()
  25. {}
  26. matchresult::matchresult( size_t nSubExpr, std::string const& os ):
  27. __matches( nSubExpr ), __org_string( os )
  28. {}
  29. matchresult::matchresult( const matchresult::matchvec_t& mv, const std::string& os ):
  30. __matches( mv ), __org_string( os )
  31. {}
  32. matchresult::operator bool() const {
  33. return (__matches.size()!=0);
  34. }
  35. matchresult::matchvec_t::value_type matchresult::operator[]( unsigned int m ) const {
  36. if( m>=__matches.size() ) {
  37. THROW_EZEXCEPT(Regular_Expression_Exception,
  38. "requesting group " << m << ", but only " << __matches.size() << " groups matched");
  39. }
  40. return __matches[m];
  41. }
  42. std::string matchresult::operator[]( const matchvec_t::value_type& m ) const {
  43. if( !m ) {
  44. THROW_EZEXCEPT(Regular_Expression_Exception,
  45. "requesting string from invalid group");
  46. }
  47. return __org_string.substr(m.rm_so, (m.rm_eo-m.rm_so));
  48. }
  49. std::string matchresult::group( unsigned int m ) const {
  50. return (*this)[(*this)[m]];
  51. }
  52. bool operator==( const char* tocheck, const Regular_Expression& against ) {
  53. return against.matches( tocheck );
  54. }
  55. bool operator!=( const char* tocheck, const Regular_Expression& against ) {
  56. return !(against.matches( tocheck ));
  57. }
  58. bool operator==( const string& s, const Regular_Expression& against ) {
  59. return against.matches( s.c_str() );
  60. }
  61. bool operator!=( const string& s, const Regular_Expression& against ) {
  62. return !(against.matches(s.c_str()));
  63. }
  64. Regular_Expression::Regular_Expression( const string& pattern_text, int flags ) :
  65. myOriginalPattern( pattern_text )
  66. {
  67. // Compile the pattern....
  68. int r;
  69. char errbuf[ 512 ];
  70. if( (r=::regcomp(&myCompiledExpression,
  71. myOriginalPattern.c_str(),
  72. flags))!=0 ) {
  73. ::regerror(r, &myCompiledExpression, errbuf, sizeof(errbuf));
  74. THROW_EZEXCEPT(Regular_Expression_Exception,
  75. "Failed to compile RegEx(" << myOriginalPattern << "): " << errbuf);
  76. }
  77. }
  78. matchresult Regular_Expression::matches( const string& s ) const {
  79. // After compiling the regexp, the re_nsub member informs how many
  80. // sub expressions/match groups there were. Together with the zeroth
  81. // group (the whole) match, we know how many there are in total
  82. const size_t nSubExpr( 1 + myCompiledExpression.re_nsub );
  83. matchresult mr( nSubExpr, s );
  84. if( ::regexec(&myCompiledExpression, s.c_str(), nSubExpr, &mr.__matches[0], 0)==0 )
  85. return mr;
  86. return matchresult();
  87. }
  88. string Regular_Expression::pattern( void ) const {
  89. return myOriginalPattern;
  90. }
  91. Regular_Expression::~Regular_Expression() {
  92. ::regfree( &myCompiledExpression );
  93. }