libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
Operators.h
Go to the documentation of this file.
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> bool Cmp(int op, T1 v1, T2 v2) {
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#if 0
76template<class T>
77static inline unsigned long long dap_floor_zero(T i)
78{
79 return (unsigned long long) ((i < 0) ? 0 : i);
80}
81#endif
82
83#if 0
84// Hack
85template<class T>
86static inline T dap_floor_zero(T i)
87{
88 return i;
89}
90
99template<class UT1, class T2>
100bool USCmp(int op, UT1 v1, T2 v2)
101{
102 DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
103
104 switch (op) {
105 case SCAN_EQUAL:
106 return v1 == dap_floor_zero<T2>(v2);
107 case SCAN_NOT_EQUAL:
108 return v1 != dap_floor_zero<T2>(v2);
109 case SCAN_GREATER:
110 return v1 > dap_floor_zero<T2>(v2);
111 case SCAN_GREATER_EQL:
112 return v1 >= dap_floor_zero<T2>(v2);
113 case SCAN_LESS:
114 return v1 < dap_floor_zero<T2>(v2);
115 case SCAN_LESS_EQL:
116 return v1 <= dap_floor_zero<T2>(v2);
117 case SCAN_REGEXP:
118 throw Error(malformed_expr, "Regular expressions are supported for strings only.");
119 default:
120 throw Error(malformed_expr, "Unrecognized operator.");
121 }
122}
123
136template<class T1, class UT2>
137bool SUCmp(int op, T1 v1, UT2 v2)
138{
139 DBGN(cerr << __PRETTY_FUNCTION__ << v1 << " " << op << " " << v2 << endl);
140
141 switch (op) {
142 case SCAN_EQUAL:
143 return dap_floor_zero<T1>(v1) == v2;
144 case SCAN_NOT_EQUAL:
145 return dap_floor_zero<T1>(v1) != v2;
146 case SCAN_GREATER:
147 return dap_floor_zero<T1>(v1) > v2;
148 case SCAN_GREATER_EQL:
149 return dap_floor_zero<T1>(v1) >= v2;
150 case SCAN_LESS:
151 return dap_floor_zero<T1>(v1) < v2;
152 case SCAN_LESS_EQL:
153 return dap_floor_zero<T1>(v1) <= v2;
154 case SCAN_REGEXP:
155 throw Error(malformed_expr, "Regular expressions are supported for strings only.");
156 default:
157 throw Error(malformed_expr, "Unrecognized operator.");
158 }
159}
160#endif
161
166template <class T1, class T2> bool StrCmp(int op, T1 v1, T2 v2) {
167 switch (op) {
168 case SCAN_EQUAL:
169 return v1 == v2;
170 case SCAN_NOT_EQUAL:
171 return v1 != v2;
172 case SCAN_GREATER:
173 return v1 > v2;
174 case SCAN_GREATER_EQL:
175 return v1 >= v2;
176 case SCAN_LESS:
177 return v1 < v2;
178 case SCAN_LESS_EQL:
179 return v1 <= v2;
180 case SCAN_REGEXP: {
181 Regex r(v2);
182 return r.match(v1.c_str(), v1.length()) > 0;
183 }
184 default:
185 throw Error(malformed_expr, "Unrecognized operator.");
186 }
187}
188
189} // namespace libdap
190
191#endif // _operators_h
#define malformed_expr
(400)
Definition Error.h:66
A class for error processing.
Definition Error.h:92
Regular expression matching.
Definition GNURegex.h:54
int match(const char *s, int len, int pos=0) const
Does the pattern match.
Definition GNURegex.cc:136
#define DBGN(x)
Definition debug.h:59
top level DAP object to house generic methods
Definition AISConnect.cc:30
bool StrCmp(int op, T1 v1, T2 v2)
Definition Operators.h:166
bool Cmp(int op, T1 v1, T2 v2)
Definition Operators.h:52