libdap Updated for version 3.20.11
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
40#include "ce_expr.tab.hh"
41
42#pragma GCC diagnostic ignored "-Wsign-compare"
43
44namespace libdap {
45
52template<class T1, class T2>
53bool Cmp(int op, T1 v1, T2 v2)
54{
55 DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
56
57 switch (op) {
58 case SCAN_EQUAL:
59 return v1 == v2;
60 case SCAN_NOT_EQUAL:
61 return v1 != v2;
62 case SCAN_GREATER:
63 return v1 > v2;
64 case SCAN_GREATER_EQL:
65 return v1 >= v2;
66 case SCAN_LESS:
67 return v1 < v2;
68 case SCAN_LESS_EQL:
69 return v1 <= v2;
70 case SCAN_REGEXP:
71 throw Error(malformed_expr, "Regular expressions are supported for strings only.");
72 default:
73 throw Error(malformed_expr, "Unrecognized operator.");
74 }
75}
76
77#if 0
78template<class T>
79static inline unsigned long long dap_floor_zero(T i)
80{
81 return (unsigned long long) ((i < 0) ? 0 : i);
82}
83#endif
84
85#if 0
86// Hack
87template<class T>
88static inline T dap_floor_zero(T i)
89{
90 return i;
91}
92
101template<class UT1, class T2>
102bool USCmp(int op, UT1 v1, T2 v2)
103{
104 DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
105
106 switch (op) {
107 case SCAN_EQUAL:
108 return v1 == dap_floor_zero<T2>(v2);
109 case SCAN_NOT_EQUAL:
110 return v1 != dap_floor_zero<T2>(v2);
111 case SCAN_GREATER:
112 return v1 > dap_floor_zero<T2>(v2);
113 case SCAN_GREATER_EQL:
114 return v1 >= dap_floor_zero<T2>(v2);
115 case SCAN_LESS:
116 return v1 < dap_floor_zero<T2>(v2);
117 case SCAN_LESS_EQL:
118 return v1 <= dap_floor_zero<T2>(v2);
119 case SCAN_REGEXP:
120 throw Error(malformed_expr, "Regular expressions are supported for strings only.");
121 default:
122 throw Error(malformed_expr, "Unrecognized operator.");
123 }
124}
125
138template<class T1, class UT2>
139bool SUCmp(int op, T1 v1, UT2 v2)
140{
141 DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
142
143 switch (op) {
144 case SCAN_EQUAL:
145 return dap_floor_zero<T1>(v1) == v2;
146 case SCAN_NOT_EQUAL:
147 return dap_floor_zero<T1>(v1) != v2;
148 case SCAN_GREATER:
149 return dap_floor_zero<T1>(v1) > v2;
150 case SCAN_GREATER_EQL:
151 return dap_floor_zero<T1>(v1) >= v2;
152 case SCAN_LESS:
153 return dap_floor_zero<T1>(v1) < v2;
154 case SCAN_LESS_EQL:
155 return dap_floor_zero<T1>(v1) <= v2;
156 case SCAN_REGEXP:
157 throw Error(malformed_expr, "Regular expressions are supported for strings only.");
158 default:
159 throw Error(malformed_expr, "Unrecognized operator.");
160 }
161}
162#endif
163
168template<class T1, class T2>
169bool StrCmp(int op, T1 v1, T2 v2)
170{
171 switch (op) {
172 case SCAN_EQUAL:
173 return v1 == v2;
174 case SCAN_NOT_EQUAL:
175 return v1 != v2;
176 case SCAN_GREATER:
177 return v1 > v2;
178 case SCAN_GREATER_EQL:
179 return v1 >= v2;
180 case SCAN_LESS:
181 return v1 < v2;
182 case SCAN_LESS_EQL:
183 return v1 <= v2;
184 case SCAN_REGEXP: {
185 Regex r(v2);
186 return r.match(v1.c_str(), v1.length()) > 0;
187 }
188 default:
189 throw Error(malformed_expr, "Unrecognized operator.");
190 }
191}
192
193} // namespace libdap
194
195#endif // _operators_h
A class for error processing.
Definition: Error.h:94
Regular expression matching.
Definition: GNURegex.h:57
int match(const char *s, int len, int pos=0) const
Does the pattern match.
Definition: GNURegex.cc:141
top level DAP object to house generic methods
Definition: AlarmHandler.h:36
bool StrCmp(int op, T1 v1, T2 v2)
Definition: Operators.h:169
bool Cmp(int op, T1 v1, T2 v2)
Definition: Operators.h:53