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.
 
 
 

117 lines
3.4 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. // fully construct, a list of matches (possibly
  36. // empty) AND a pointer to the original string
  37. matchresult( const matchvec_t& mv, const char* os );
  38. matchresult( const matchvec_t& mv, const std::string& os );
  39. // implement operator bool
  40. // which tells the truthiness
  41. // of the matchresult
  42. operator bool() const;
  43. // return the m'th regmatch.
  44. // Most likely use:
  45. // matchresult m = rx.match(<some string>);
  46. // if( m[1] )
  47. // cout << "matchgroup 1=" << m.matchgroup(1) << endl;
  48. matchvec_t::value_type operator[]( unsigned int m ) const;
  49. // nice.. operator overloading :)
  50. std::string operator[]( const matchvec_t::value_type& m ) const;
  51. // snip the indicated match out of
  52. // the original string.
  53. // string interface, caller doesn't
  54. // have to do mem.management, the
  55. std::string group( unsigned int m ) const;
  56. // our attributes
  57. matchvec_t __matches;
  58. std::string __org_string;
  59. };
  60. // A generic regex....
  61. class Regular_Expression
  62. {
  63. friend bool operator==( const char* tocheck, const Regular_Expression& against );
  64. friend bool operator!=( const char* tocheck, const Regular_Expression& against );
  65. friend bool operator==( const std::string& s, const Regular_Expression& against );
  66. friend bool operator!=( const std::string& s, const Regular_Expression& against );
  67. public:
  68. // Create from a pattern,
  69. // note that one match for the whole pattern will be returned in matches()[0]
  70. // so maxmatch should be at least <number of expectect matches> + 1
  71. Regular_Expression( const char* pattern, int flags=REG_EXTENDED );
  72. Regular_Expression( const std::string& pattern, int flags=REG_EXTENDED );
  73. // match the string 's' against this RX
  74. matchresult matches( const char* s ) const;
  75. matchresult matches( const std::string& s ) const;
  76. // Return the pattern
  77. std::string pattern( void ) const;
  78. // Delete all allocated stuff
  79. ~Regular_Expression();
  80. private:
  81. // Our private parts
  82. char* myOriginalPattern;
  83. regex_t myCompiledExpression;
  84. ::regmatch_t* mySubexprs;
  85. // Prohibit these
  86. Regular_Expression();
  87. Regular_Expression( const Regular_Expression& );
  88. Regular_Expression& operator=( const Regular_Expression& );
  89. };
  90. #endif