//
//
//
//    regex.h
//
//    Header file for regular expression class.
//
//    Modified for class experiment (2003-09-18, 2003-10-04).
//
//    =========================================================================
//
//    Copyright (C) 2001-2003 by Matt Roberts, KK5JY.
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//    =========================================================================
//
//
//


#ifndef __REGEX_H
#define __REGEX_H

#include <sys/types.h>
#include <regex.h>
#include <string>
#include <unistd.h>


namespace my {

	//
	//
	//   class regex
	//
	//

	class regex {
		public:
			enum flags {
				extended = REG_EXTENDED,
				icase    = REG_ICASE,
				nosub    = REG_NOSUB,
				newline  = REG_NEWLINE
			};
		private:
			std::string re_str;
			regex_t re_exp;
			int cflags;

		private: // private utility functions
			void compile (void);

		public:  // public [cd]tors
			regex (const std::string &ex, int fl = nosub);
			regex (const regex &r);
			regex (int fl = nosub);
			~regex (void);

		public:  // public manipulators
			void copy (const regex &);
			void set (const std::string &, int flgs = nosub);

			int getflags (void) const { return cflags; };
			const std::string literal (void) const { return std::string(re_str); };

		public:  // public accessors
			bool compare (const std::string &s) const;
			bool compare (const my::regex &s)   const;

		public:  // public operators
			// boolean comparison operators (bulk of class)
			bool operator == (const regex &r)       const { return compare(r); };
			bool operator == (const std::string &s) const { return compare(s); };

			bool operator != (const regex &r)       const { return ! compare(r); };
			bool operator != (const std::string &s) const { return ! compare(s); };

			const regex & operator = (const regex &r)       { copy(r); return (*this); };
			const regex & operator = (const std::string &s) { set(s);  return (*this); };

	};

}

inline bool operator == (const std::string &s, const my::regex &r) {
	return r.compare(s);
}

inline bool operator != (const std::string &s, const my::regex &r) {
	return ( ! r.compare(s));
}

#endif  // __REGEX_H
