bes  Updated for version 3.17.4
wcsUtil.h
1 /******************************************************************************
2  * $Id: wcsUtil.h 2011-07-19 16:24:00Z $
3  *
4  * Project: The Open Geospatial Consortium (OGC) Web Coverage Service (WCS)
5  * for Earth Observation: Open Source Reference Implementation
6  * Purpose: WCS Utility Function definition
7  * Author: Yuanzheng Shao, yshao3@gmu.edu
8  *
9  ******************************************************************************
10  * Copyright (c) 2011, Liping Di <ldi@gmu.edu>, Yuanzheng Shao <yshao3@gmu.edu>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  ****************************************************************************/
30 
31 #ifndef WCSUTIL_H_
32 #define WCSUTIL_H_
33 
34 #include <iostream>
35 #include <fstream>
36 #include <sstream>
37 #include <vector>
38 #include <stdlib.h>
39 #include <time.h>
40 
41 #include <cpl_string.h>
42 #include "wcs_error.h"
43 #include "BoundingBox.h"
44 
45 using namespace std;
46 
47 #ifndef EQUAL
48 #if defined(WIN32) || defined(WIN32CE)
49 # define EQUALN(a,b,n) (strnicmp(a,b,n)==0)
50 # define EQUAL(a,b) (stricmp(a,b)==0)
51 #else
52 # define EQUALN(a,b,n) (strncasecmp(a,b,n)==0)
53 # define EQUAL(a,b) (strcasecmp(a,b)==0)
54 #endif
55 #endif
56 
57 #ifndef TRUE
58 #define TRUE 1
59 #endif
60 #ifndef FALSE
61 #define FALSE 0
62 #endif
63 
64 #ifndef NULL
65 #define NULL 0
66 #endif
67 
68 #ifdef WINDOWS
69 #define DELIMITER "\\"
70 #else
71 #define DELIMITER "/"
72 #endif
73 
74 const int SHORT_NAME_LEN = 128;
75 const int MAX_NAME_LEN = 512;
76 const int MAX_LIST_LEN = 1024;
77 const int MAX_LINE_LEN = 65536;
78 
79 
80 // -----------------------------------------------------------------------
81 // CGI (Common Gateway Interface) related class and functions
82 // -----------------------------------------------------------------------
83 enum CGI_METHOD_FLAG
84 {
85  UN_KNOWN, HTTP_GET, HTTP_XML_POST
86 };
87 
88 /************************************************************************/
89 /* ==================================================================== */
90 /* WCSCGI */
91 /* ==================================================================== */
92 /************************************************************************/
93 
101 class WCSCGI
102 {
103 private:
104  string ms_CGIContent;
105  CGI_METHOD_FLAG me_CGIMethod;
106 
107 public:
108  WCSCGI()
109  {
110  me_CGIMethod = UN_KNOWN;
111  }
112 
113  CPLErr Run();
114 
115  string GetRqstContent()
116  {
117  return ms_CGIContent;
118  }
119 
120  CGI_METHOD_FLAG GetCGImethod()
121  {
122  return me_CGIMethod;
123  }
124 };
125 
126 /************************************************************************/
127 /* ==================================================================== */
128 /* StringList */
129 /* ==================================================================== */
130 /************************************************************************/
131 
133 {
134 private:
135  vector<string> strings;
136 
137 public:
138  StringList(const string& sstrings, const char delimiter);
139  StringList(const string& sstrings, const string& delimiters);
140  StringList(){ }
141 
142  int indexof(string qstr);
143 
144  void add(string newstr)
145  {
146  strings.push_back(newstr);
147  }
148 
149  void clear()
150  {
151  strings.clear();
152  }
153 
154  int size()
155  {
156  return strings.size();
157  }
158 
159  string& operator [](int index)
160  {
161  return strings[index];
162  }
163 
164  void append(StringList & s);
165  void append(const string sstrings, const char delimiter);
166  void append(const string sstrings, const string& delimiters);
167 
168  string toString()
169  {
170  string s = "";
171  for (int i = 0; i < size(); i++)
172  s += strings[i];
173  return s;
174  }
175 
176  string toString(const char delimiter)
177  {
178  string s = "";
179  for (int i = 0; i < size(); i++)
180  s += strings[i] + delimiter;
181  return s;
182  }
183 
184  string toString(const string& delimiters)
185  {
186  string s = "";
187  for (int i = 0; i < size(); i++)
188  s += strings[i] + delimiters;
189  return s;
190  }
191 };
192 
193 class S2C
194 {
195 
196 private:
197  char buf[MAX_LINE_LEN];
198 
199 public:
200  S2C(string s)
201  {
202  s.copy(buf, string::npos);
203  buf[s.size()] = 0;
204  }
205 
206  char * c_str()
207  {
208  return buf;
209  }
210 
211 };
212 
213 /************************************************************************/
214 /* ==================================================================== */
215 /* KVP */
216 /* ==================================================================== */
217 /************************************************************************/
218 
219 class KVP
220 {
221 public:
222  string name, value;
223 
224  KVP& operator =(const KVP &id)
225  {
226  name = id.name;
227  value = id.value;
228  return *this;
229  }
230 
231  KVP& operator =(const KVP *pid)
232  {
233  name = pid->name;
234  value = pid->value;
235  return *this;
236  }
237 
238  KVP(string n, string v) :
239  name(n), value(v)
240  {
241  }
242 
243  KVP(string namevaluepair);
244 };
245 
246 
247 /************************************************************************/
248 /* ==================================================================== */
249 /* KVPsReader */
250 /* ==================================================================== */
251 /************************************************************************/
252 
254 {
255 protected:
256  vector<KVP> m_kvps;
257 
258 public:
259  KVPsReader()
260  {
261  }
262 
263  ~KVPsReader()
264  {
265  }
266 
267  KVPsReader(const string& urlStr, const char &tok);
268 
269  KVP& operator [](const int index)
270  {
271  return m_kvps[index];
272  }
273 
274  int size()
275  {
276  return m_kvps.size();
277  }
278 
279  string getValue(const string &keyname);
280  string getValue(const string &keyname, const string &defaultvalue);
281  vector<string> getValues(const string &keyname);
282 };
283 
284 /************************************************************************/
285 /* ==================================================================== */
286 /* CFGReader */
287 /* ==================================================================== */
288 /************************************************************************/
290 {
291 protected:
292  vector<KVP> kvps;
293 
294 public:
295  CFGReader(const string &configfilename);
296 
297  KVP& operator [](const int index)
298  {
299  return kvps[index];
300  }
301 
302  int size()
303  {
304  return kvps.size();
305  }
306 
307  string getValue(const string &keyname);
308  string getValue(const string &keyname, const string &defaultvalue);
309 };
310 
311 
312 // -----------------------------------------------------------------------
313 // Extra Template Functions
314 // Exchange() --- used to exchange values
315 // convertToString() --- convert value to string
316 // convertFromString --- convert string to values
317 // -----------------------------------------------------------------------
318 template<class T>
319 static void Exchange(T & a, T & b)
320 {
321  T t;
322  t = a;
323  a = b;
324  b = t;
325 }
326 
327 template <class T>
328 static string convertToString(T &value)
329 {
330  stringstream ss;
331  ss<<value;
332  string rtnstr = ss.str();
333  return rtnstr;
334 }
335 
336 template <class T>
337 static void convertFromString(T &value, const string& s)
338 {
339  stringstream ss(s);
340  ss>>value;
341 }
342 
343 
344 // -----------------------------------------------------------------------
345 // Extra Utility Functions
346 // -----------------------------------------------------------------------
347 CPL_C_START
348 
349 // String Operation Related
350 int CPL_DLL CPL_STDCALL CsvburstCpp(const std::string& line, std::vector<std::string> &strSet, const char tok);
351 int CPL_DLL CPL_STDCALL CsvburstComplexCpp(const string& line, vector<string> &strSet, const char* tok);
352 int CPL_DLL CPL_STDCALL Find_Compare_SubStr(string line, string sub);
353 void CPL_DLL CPL_STDCALL Strslip(const char* str, const char* arrStr[], const char leftdlm, const char rightdlm);
354 string CPL_DLL CPL_STDCALL StrReplace(string& str, const string oldSubStr, const string newStr);
355 string CPL_DLL CPL_STDCALL SPrintArray(GDALDataType eDataType, const void *paDataArray, int nValues, const char *pszDelimiter);
356 string CPL_DLL CPL_STDCALL StrTrimHead(const string &str);
357 string CPL_DLL CPL_STDCALL StrTrimTail(const string &str);
358 string CPL_DLL CPL_STDCALL StrTrims(const std::string&, const char*);
359 string CPL_DLL CPL_STDCALL StrTrim(const string &str);
360 
361 // Date Time Operation Related
362 int CPL_DLL CPL_STDCALL CompareDateTime_GreaterThan(string time1, string time2);
363 int CPL_DLL CPL_STDCALL ConvertDateTimeToSeconds(string datetime);
364 string CPL_DLL CPL_STDCALL GetTimeString(int code);
365 
366 // Directory Operation Related
367 CPLErr CPL_DLL CPL_STDCALL GetFileNameList(char* dir, std::vector<string> &strList);
368 string CPL_DLL CPL_STDCALL MakeTempFile(string dir, string covID, string suffix);
369 string CPL_DLL CPL_STDCALL GetUUID();
370 
371 // Request Parser Operation
372 void CPL_DLL CPL_STDCALL GetSubSetTime(const string& subsetstr, vector<string> &subsetvalue);
373 string CPL_DLL CPL_STDCALL GetSubSetLatLon(const string& subsetstr, vector<double> &subsetvalue);
374 string CPL_DLL CPL_STDCALL GetSingleValue(const string& subsetstr);
375 
376 CPLErr CPL_DLL CPL_STDCALL GetTRMMBandList(string start, string end, std::vector<int> &bandList);
377 void CPL_DLL CPL_STDCALL GetCornerPoints(const GDAL_GCP* &pGCPList, const int &nGCPs, My2DPoint& lowLeft, My2DPoint& upRight);
378 
379 CPL_C_END
380 
381 #endif /*WCSUTIL_H_*/
Definition: wcsUtil.h:219
STL namespace.
Definition: wcsUtil.h:193