libdap  Updated for version 3.20.3
libdap4 is an implementation of OPeNDAP's DAP protocol.
Operators.h
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2002,2003 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 // (c) COPYRIGHT URI/MIT 1999
26 // Please read the full copyright statement in the file COPYRIGHT_URI.
27 //
28 // Authors:
29 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
30 
31 // Templates for relational operations.
32 //
33 // jhrg 3/24/99
34 
35 #ifndef _operators_h
36 #define _operators_h
37 
38 #include "GNURegex.h" // GNU Regex class used for string =~ op.
39 #include "ce_expr.tab.hh"
40 
41 namespace libdap {
42 
50 template<class T1, class T2>
51 bool Cmp(int op, T1 v1, T2 v2)
52 {
53  DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
54 
55  switch (op) {
56  case SCAN_EQUAL:
57  return v1 == v2;
58  case SCAN_NOT_EQUAL:
59  return v1 != v2;
60  case SCAN_GREATER:
61  return v1 > v2;
62  case SCAN_GREATER_EQL:
63  return v1 >= v2;
64  case SCAN_LESS:
65  return v1 < v2;
66  case SCAN_LESS_EQL:
67  return v1 <= v2;
68  case SCAN_REGEXP:
69  throw Error(malformed_expr, "Regular expressions are supported for strings only.");
70  default:
71  throw Error(malformed_expr, "Unrecognized operator.");
72  }
73 }
74 
75 template<class T>
76 static inline unsigned long long dap_floor_zero(T i)
77 {
78  return (unsigned long long) ((i < 0) ? 0 : i);
79 }
80 
89 template<class UT1, class T2>
90 bool USCmp(int op, UT1 v1, T2 v2)
91 {
92  DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
93 
94  switch (op) {
95  case SCAN_EQUAL:
96  return v1 == dap_floor_zero<T2>(v2);
97  case SCAN_NOT_EQUAL:
98  return v1 != dap_floor_zero<T2>(v2);
99  case SCAN_GREATER:
100  return v1 > dap_floor_zero<T2>(v2);
101  case SCAN_GREATER_EQL:
102  return v1 >= dap_floor_zero<T2>(v2);
103  case SCAN_LESS:
104  return v1 < dap_floor_zero<T2>(v2);
105  case SCAN_LESS_EQL:
106  return v1 <= dap_floor_zero<T2>(v2);
107  case SCAN_REGEXP:
108  throw Error(malformed_expr, "Regular expressions are supported for strings only.");
109  default:
110  throw Error(malformed_expr, "Unrecognized operator.");
111  }
112 }
113 
126 template<class T1, class UT2>
127 bool SUCmp(int op, T1 v1, UT2 v2)
128 {
129  DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
130 
131  switch (op) {
132  case SCAN_EQUAL:
133  return dap_floor_zero<T1>(v1) == v2;
134  case SCAN_NOT_EQUAL:
135  return dap_floor_zero<T1>(v1) != v2;
136  case SCAN_GREATER:
137  return dap_floor_zero<T1>(v1) > v2;
138  case SCAN_GREATER_EQL:
139  return dap_floor_zero<T1>(v1) >= v2;
140  case SCAN_LESS:
141  return dap_floor_zero<T1>(v1) < v2;
142  case SCAN_LESS_EQL:
143  return dap_floor_zero<T1>(v1) <= v2;
144  case SCAN_REGEXP:
145  throw Error(malformed_expr, "Regular expressions are supported for strings only.");
146  default:
147  throw Error(malformed_expr, "Unrecognized operator.");
148  }
149 }
150 
156 template<class T1, class T2>
157 bool StrCmp(int op, T1 v1, T2 v2)
158 {
159  switch (op) {
160  case SCAN_EQUAL:
161  return v1 == v2;
162  case SCAN_NOT_EQUAL:
163  return v1 != v2;
164  case SCAN_GREATER:
165  return v1 > v2;
166  case SCAN_GREATER_EQL:
167  return v1 >= v2;
168  case SCAN_LESS:
169  return v1 < v2;
170  case SCAN_LESS_EQL:
171  return v1 <= v2;
172  case SCAN_REGEXP: {
173  Regex r(v2.c_str());
174  return r.match(v1.c_str(), v1.length()) > 0;
175  }
176  default:
177  throw Error(malformed_expr, "Unrecognized operator.");
178  }
179 }
180 
181 } // namespace libdap
182 
183 #endif // _operators_h
libdap::Regex::match
int match(const char *s, int len, int pos=0)
Does the pattern match.
Definition: GNURegex.cc:115
libdap::Error
A class for error processing.
Definition: Error.h:93
libdap::Cmp
bool Cmp(int op, T1 v1, T2 v2)
Definition: Operators.h:51
libdap
top level DAP object to house generic methods
Definition: AlarmHandler.h:36
libdap::Regex
Definition: GNURegex.h:36
libdap::USCmp
bool USCmp(int op, UT1 v1, T2 v2)
Definition: Operators.h:90
libdap::StrCmp
bool StrCmp(int op, T1 v1, T2 v2)
Definition: Operators.h:157
libdap::SUCmp
bool SUCmp(int op, T1 v1, UT2 v2)
Definition: Operators.h:127