OPeNDAP Hyrax Back End Server (BES) Updated for version 3.8.3
|
00001 // BESRegex.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // and James Gallagher <jgallagher@gso.uri.edu> 00009 // 00010 // This library is free software; you can redistribute it and/or 00011 // modify it under the terms of the GNU Lesser General Public 00012 // License as published by the Free Software Foundation; either 00013 // version 2.1 of the License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 // 00024 // You can contact University Corporation for Atmospheric Research at 00025 // 3080 Center Green Drive, Boulder, CO 80301 00026 00027 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00028 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00029 // 00030 // Authors: 00031 // pwest Patrick West <pwest@ucar.edu> 00032 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00033 // jimg James Gallagher <jgallagher@gso.uri.edu> 00034 00035 #include <config.h> 00036 00037 #ifndef WIN32 00038 #include <alloca.h> 00039 #endif 00040 00041 #include <sys/types.h> 00042 #include <regex.h> 00043 00044 #include <cstdlib> 00045 #include <new> 00046 #include <string> 00047 #include <stdexcept> 00048 00049 #include "BESRegex.h" 00050 #include "BESInternalError.h" 00051 #include "BESScrub.h" 00052 00053 using namespace std; 00054 00055 void 00056 BESRegex::init(const char *t) 00057 { 00058 d_preg = static_cast<void*>(new regex_t); 00059 int result = regcomp(static_cast<regex_t*>(d_preg), t, REG_EXTENDED); 00060 00061 if (result != 0) { 00062 size_t msg_len = regerror(result, static_cast<regex_t*>(d_preg), 00063 static_cast<char*>(NULL), 00064 static_cast<size_t>(0)); 00065 char *msg = new char[msg_len+1]; 00066 regerror(result, static_cast<regex_t*>(d_preg), msg, msg_len); 00067 string err = string( "BESRegex error: " ) + string( msg ) ; 00068 BESInternalError e( err, __FILE__, __LINE__ ) ; 00069 delete[] msg; 00070 throw e; 00071 } 00072 } 00073 00074 BESRegex::~BESRegex() 00075 { 00076 regfree(static_cast<regex_t*>(d_preg)); 00077 delete static_cast<regex_t*>(d_preg); d_preg = 0; 00078 00079 } 00080 00084 BESRegex::BESRegex(const char* t) 00085 { 00086 init(t); 00087 } 00088 00091 BESRegex::BESRegex(const char* t, int) 00092 { 00093 init(t); 00094 } 00095 00102 int 00103 BESRegex::match(const char* s, int len, int pos) 00104 { 00105 regmatch_t pmatch[len]; 00106 string ss = s; 00107 00108 int result = regexec(static_cast<regex_t*>(d_preg), 00109 ss.substr(pos, len-pos).c_str(), len, pmatch, 0); 00110 if (result == REG_NOMATCH) 00111 return -1; 00112 00113 return pmatch[0].rm_eo - pmatch[0].rm_so; 00114 } 00115 00126 int 00127 BESRegex::search(const char* s, int len, int& matchlen, int pos) 00128 { 00129 // sanitize allocation 00130 if (!BESScrub::size_ok(sizeof(regmatch_t), len+1)) 00131 return -1; 00132 00133 // alloc space for len matches, which is theoretical max. 00134 regmatch_t *pmatch = new regmatch_t[len+1]; 00135 string ss = s; 00136 00137 int result = regexec(static_cast<regex_t*>(d_preg), 00138 ss.substr(pos, len-pos).c_str(), len, pmatch, 0); 00139 if (result == REG_NOMATCH) { 00140 delete[] pmatch; pmatch = 0; 00141 return -1; 00142 } 00143 00144 // Match found, find the first one (pmatch lists the longest first) 00145 int m = 0; 00146 for (int i = 1; i < len; ++i) 00147 if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so) 00148 m = i; 00149 00150 matchlen = pmatch[m].rm_eo - pmatch[m].rm_so; 00151 int matchpos = pmatch[m].rm_so; 00152 00153 delete[] pmatch; pmatch = 0; 00154 return matchpos; 00155 } 00156