Scopira 20080306

regex.h

00001 
00002 /*
00003  *  Copyright (c) 2004    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED__SCOPIRA_TOOL_REGEX_H__
00015 #define __INCLUDED__SCOPIRA_TOOL_REGEX_H__
00016 #include <scopira/tool/platform.h>
00017 #ifndef PLATFORM_win32
00018 
00019 #include <stddef.h>
00020 
00021 #if (GCC_VERSION_MAJOR<3) || (GCC_VERSION_MAJOR==3 && GCC_VERSION_MINOR<3)
00022 // this is a hack. gcc 3 turns on some regex() prototype stuff that
00023 // gcc3 likes, but g++<3.3 doesnt. predefine this trickery here
00024 #define __restrict_arr
00025 #endif
00026 
00027 #include <regex.h>
00028 
00029 #include <string>
00030 
00031 #include <scopira/tool/array.h>
00032 
00033 namespace scopira
00034 {
00035   namespace tool
00036   {
00037     class regmatch;
00038     class regex;
00039   }
00040 }
00041 
00047 class scopira::tool::regmatch
00048 {
00049   public:
00051     explicit regmatch(size_t sz);
00052 
00054     bool is_valid(size_t idx) const { return dm_ary[idx].rm_so != -1; }
00055 
00057     size_t begin(size_t idx) { return dm_ary[idx].rm_so; }
00059     size_t end(size_t idx) { return dm_ary[idx].rm_eo; }
00060 
00062     size_t size(void) const { return dm_ary.size(); }
00063 
00065     size_t size_match(void) const;
00066 
00068     std::string substr(const std::string &base, size_t idx)
00069       { assert(is_valid(idx)); return base.substr(begin(idx), end(idx)-begin(idx)); }
00070 
00071     friend class regex;
00072 
00073   private:
00075     scopira::tool::basic_array<regmatch_t> dm_ary;
00076 };
00077 
00083 class scopira::tool::regex
00084 {
00085   public:
00087     regex(void);
00089     regex(const std::string &pattern);
00091     ~regex();
00092 
00094     bool failed(void) const { return !dm_valid; }
00095 
00097     bool compile(const std::string &pattern);
00098 
00100     bool match(const std::string &s) const;
00101 
00103     bool match(const std::string &s, regmatch &mat) const;
00104 
00105   private:
00106     bool dm_valid;
00107     regex_t dm_re;
00108 
00109   private:
00110     // not implemented - not legal
00111     regex(const regex &);
00112     // not implemented - not legal
00113     void operator = (const regex&);
00114 };
00115 
00116 #endif
00117 #endif
00118