OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
besregtest.cc
Go to the documentation of this file.
1 // besregtest.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 
32 #include <iostream>
33 #include <string>
34 #include <map>
35 
36 using std::cout ;
37 using std::endl ;
38 using std::string ;
39 using std::multimap ;
40 using std::pair ;
41 
42 #include "BESRegex.h"
43 #include "BESError.h"
44 
45 multimap<string,string> expressions ;
46 
47 bool break_includes( const string &s, string &err ) ;
48 bool break_types( const string &s, string &err ) ;
49 
50 void
51 usage( const string &prog )
52 {
53  cout << "Usage: " << prog << " include|exclude|type <regular_expression> <string_to_match>" << endl ;
54  cout << " samples:" << endl ;
55  cout << " besregtest include \"123456;\" 01234567 matches 6 of 8 characters" << endl ;
56  cout << " besregtest include \"^123456$;\" 01234567 does not match" << endl ;
57  cout << " besregtest include \"^123456$;\" 123456 matches all 6 of 6 characters" << endl ;
58  cout << " besregtest include \".*\\.nc$;\" fnoc1.nc matches" << endl ;
59  cout << " besregtest include \".*\\.nc$;\" fnoc1.ncd does not matche" << endl ;
60  cout << " besregtest type \"nc:.*\\.nc$;nc:.*\\.nc\\.gz$;ff:.*\\.dat$;ff:.*\\.dat\\.gz$;\" fnoc1.nc matches type nc" << endl ;
61 }
62 
63 int
64 main( int argc, char **argv )
65 {
66  if( argc != 4 )
67  {
68  usage( argv[0] ) ;
69  return 1 ;
70  }
71 
72  string what = argv[1] ;
73  if( what != "include" && what != "exclude" && what != "type" )
74  {
75  cout << "please specify either an Include or TypeMatch expression "
76  << "by using include or type respectively as first parameter" << endl ;
77  usage( argv[0] ) ;
78  return 1 ;
79  }
80 
81  string err ;
82  bool status = true ;
83  if( what == "include" || what == "exclude" )
84  status = break_includes( argv[2], err ) ;
85  else
86  status = break_types( argv[2], err ) ;
87 
88  if( !status )
89  {
90  cout << err << endl ;
91  usage( argv[0] ) ;
92  return 1 ;
93  }
94 
95  string inQuestion( argv[3] ) ;
96 
97  multimap<string,string>::const_iterator i = expressions.begin() ;
98  multimap<string,string>::const_iterator ie = expressions.end() ;
99 
100  for( ; i != ie; i++ )
101  {
102  string reg = (*i).second ;
103  try
104  {
105  BESRegex reg_expr( reg.c_str() ) ;
106  int result = reg_expr.match( inQuestion.c_str(), inQuestion.length() ) ;
107  if( result != -1)
108  {
109  if( result == inQuestion.length() )
110  {
111  cout << "expression \"" << reg << "\" matches exactly" ;
112  }
113  else
114  {
115  cout << "expression \"" << reg << "\" matches " << result
116  << " characters out of " << inQuestion.length()
117  << " characters" ;
118  }
119  if( what == "type" )
120  cout << ", type = " << (*i).first ;
121  cout << endl ;
122  }
123  else
124  {
125  cout << "expression \"" << reg << "\" does not match" ;
126  if( what == "type" )
127  cout << " for type " << (*i).first ;
128  cout << endl ;
129  }
130  }
131  catch( BESError &e )
132  {
133  string serr = (string)"malformed regular expression \""
134  + reg + "\": " + e.get_message() ;
135  cout << serr << endl ;
136  }
137  }
138 
139  return 0 ;
140 }
141 
142 bool
143 break_includes( const string &listStr, string &err )
144 {
145  string::size_type str_begin = 0 ;
146  string::size_type str_end = listStr.length() ;
147  string::size_type semi = 0 ;
148  bool done = false ;
149  while( done == false )
150  {
151  semi = listStr.find( ";", str_begin ) ;
152  if( semi == string::npos )
153  {
154  err = (string)"regular expression malformed, no semicolon" ;
155  return false ;
156  }
157  else
158  {
159  string a_member = listStr.substr( str_begin, semi-str_begin ) ;
160  str_begin = semi+1 ;
161  if( semi == str_end-1 )
162  {
163  done = true ;
164  }
165  if( a_member != "" )
166  {
167  expressions.insert( pair<string,string>( "", a_member ) );
168  }
169  }
170  }
171 
172  return true ;
173 }
174 
175 bool
176 break_types( const string &listStr, string &err )
177 {
178  string::size_type str_begin = 0 ;
179  string::size_type str_end = listStr.length() ;
180  string::size_type semi = 0 ;
181  bool done = false ;
182  while( done == false )
183  {
184  semi = listStr.find( ";", str_begin ) ;
185  if( semi == string::npos )
186  {
187  err = (string)"type match malformed, no semicolon, "
188  + "looking for type:regexp;[type:regexp;]" ;
189  return false ;
190  }
191  else
192  {
193  string a_pair = listStr.substr( str_begin, semi-str_begin ) ;
194  str_begin = semi+1 ;
195  if( semi == str_end-1 )
196  {
197  done = true ;
198  }
199 
200  string::size_type col = a_pair.find( ":" ) ;
201  if( col == string::npos )
202  {
203  err = (string)"Catalog type match malformed, no colon, "
204  + "looking for type:regexp;[type:regexp;]" ;
205  return false ;
206  }
207  else
208  {
209  string a_type = a_pair.substr( 0, col ) ;
210  string a_reg = a_pair.substr( col+1, a_pair.length()-col ) ;
211  expressions.insert( pair<string,string>( a_type, a_reg ) ) ;
212  }
213  }
214  }
215 
216  return true ;
217 }
218