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.
 
 
 

114 lines
3.1 KiB

  1. //
  2. // Wrapper class around POSIX regex, imported from pcint
  3. //
  4. #ifndef REGULAR_EXPRESSION_H
  5. #define REGULAR_EXPRESSION_H
  6. #include <ezexcept.h>
  7. #include <sys/types.h>
  8. #include <regex.h>
  9. #include <string>
  10. #include <vector>
  11. DECLARE_EZEXCEPT(Regular_Expression_Exception)
  12. // wrapper around regmatch_t which
  13. // allows to give it some extra functionality
  14. struct pcintregmatch_t:
  15. ::regmatch_t
  16. {
  17. // creates invalid one
  18. pcintregmatch_t();
  19. // init with a given regmatch
  20. pcintregmatch_t( const ::regmatch_t& rm );
  21. // this'un valid?
  22. // regex(3) sais that an invalid
  23. // regmatch has -1 for both offsets
  24. operator bool() const;
  25. };
  26. // matchresult holds a vector
  27. // of matches. Size <> 0 indicates
  28. // a successfull match
  29. // Size >1 implies possible matchgroups
  30. // Not all matchgroups are required to be valid
  31. struct matchresult {
  32. typedef std::vector<pcintregmatch_t> matchvec_t;
  33. // empty matchresult
  34. matchresult();
  35. // Pre-construct so that we can let the regex engine write into this
  36. // result
  37. matchresult( size_t nSubExpr, std::string const& os );
  38. // fully construct, a list of matches (possibly
  39. // empty) AND a pointer to the original string
  40. matchresult( const matchvec_t& mv, const std::string& os );
  41. // implement operator bool
  42. // which tells the truthiness
  43. // of the matchresult
  44. operator bool() const;
  45. // return the m'th regmatch.
  46. // Most likely use:
  47. // matchresult m = rx.match(<some string>);
  48. // if( m[1] )
  49. // cout << "matchgroup 1=" << m.matchgroup(1) << endl;
  50. matchvec_t::value_type operator[]( unsigned int m ) const;
  51. // nice.. operator overloading :)
  52. std::string operator[]( const matchvec_t::value_type& m ) const;
  53. // snip the indicated match out of
  54. // the original string.
  55. // string interface, caller doesn't
  56. // have to do mem.management, the
  57. std::string group( unsigned int m ) const;
  58. // our attributes
  59. matchvec_t __matches;
  60. std::string __org_string;
  61. };
  62. // A generic regex....
  63. class Regular_Expression
  64. {
  65. friend bool operator==( const char* tocheck, const Regular_Expression& against );
  66. friend bool operator!=( const char* tocheck, const Regular_Expression& against );
  67. friend bool operator==( const std::string& s, const Regular_Expression& against );
  68. friend bool operator!=( const std::string& s, const Regular_Expression& against );
  69. public:
  70. // Create from a pattern,
  71. // note that one match for the whole pattern will be returned in matches()[0]
  72. // so maxmatch should be at least <number of expectect matches> + 1
  73. Regular_Expression( const std::string& pattern, int flags=REG_EXTENDED );
  74. // match the string 's' against this RX
  75. matchresult matches( const std::string& s ) const;
  76. // Return the pattern
  77. std::string pattern( void ) const;
  78. ~Regular_Expression();
  79. private:
  80. // Our private parts
  81. std::string myOriginalPattern;
  82. regex_t myCompiledExpression;
  83. // Prohibit these
  84. Regular_Expression();
  85. Regular_Expression(Regular_Expression const&);
  86. Regular_Expression const& operator=(Regular_Expression const&);
  87. };
  88. #endif