1 : /******************************************************************************
2 : * $Id: stuff.cpp 18020 2009-11-14 14:33:20Z rouault $
3 : *
4 : * Project: WMS Client Driver
5 : * Purpose: Implementation of Dataset and RasterBand classes for WMS
6 : * and other similar services.
7 : * Author: Adam Nowacki, nowak@xpam.de
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2007, Adam Nowacki
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 : #include "stdinc.h"
32 :
33 2 : CPLString MD5String(const char *s) {
34 : unsigned char hash[16];
35 : char hhash[33];
36 2 : const char *tohex = "0123456789abcdef";
37 : struct cvs_MD5Context context;
38 2 : cvs_MD5Init(&context);
39 2 : cvs_MD5Update(&context, reinterpret_cast<unsigned char const *>(s), strlen(s));
40 2 : cvs_MD5Final(hash, &context);
41 34 : for (int i = 0; i < 16; ++i) {
42 32 : hhash[i * 2] = tohex[(hash[i] >> 4) & 0xf];
43 32 : hhash[i * 2 + 1] = tohex[hash[i] & 0xf];
44 : }
45 2 : hhash[32] = '\0';
46 2 : return CPLString(hhash);
47 : }
48 :
49 7 : CPLString ProjToWKT(const CPLString &proj) {
50 7 : char* wkt = NULL;
51 7 : OGRSpatialReference sr;
52 7 : CPLString srs;
53 :
54 7 : if (sr.SetFromUserInput(proj.c_str()) != OGRERR_NONE) return srs;
55 7 : sr.exportToWkt(&wkt);
56 7 : srs = wkt;
57 7 : OGRFree(wkt);
58 0 : return srs;
59 : }
60 :
61 9 : void URLAppend(CPLString *url, const char *s) {
62 9 : if ((s == NULL) || (s[0] == '\0')) return;
63 9 : if (s[0] == '&') {
64 9 : if (url->find('?') == std::string::npos) url->append(1, '?');
65 9 : if (((*url)[url->size() - 1] == '?') || ((*url)[url->size() - 1] == '&')) url->append(s + 1);
66 8 : else url->append(s);
67 0 : } else url->append(s);
68 : }
69 :
70 8 : void URLAppendF(CPLString *url, const char *s, ...) {
71 8 : CPLString tmp;
72 : va_list args;
73 :
74 8 : va_start(args, s);
75 8 : tmp.vPrintf(s, args);
76 8 : va_end(args);
77 :
78 8 : URLAppend(url, tmp.c_str());
79 8 : }
80 :
81 0 : void URLAppend(CPLString *url, const CPLString &s) {
82 0 : URLAppend(url, s.c_str());
83 0 : }
84 :
85 3 : CPLString BufferToVSIFile(GByte *buffer, size_t size) {
86 3 : CPLString file_name;
87 :
88 3 : file_name.Printf("/vsimem/wms/%p/wmsresult.dat", buffer);
89 3 : FILE *f = VSIFileFromMemBuffer(file_name.c_str(), buffer, size, false);
90 3 : if (f == NULL) return CPLString();
91 3 : VSIFCloseL(f);
92 3 : return file_name;
93 : }
94 :
95 0 : CPLErr MakeDirs(const char *path) {
96 0 : char *p = CPLStrdup(CPLGetDirname(path));
97 0 : if (strlen(p) >= 2) {
98 0 : MakeDirs(p);
99 : }
100 0 : VSIMkdir(p, 0744);
101 0 : CPLFree(p);
102 0 : return CE_None;
103 : }
104 :
105 4 : int VersionStringToInt(const char *version) {
106 4 : if (version == NULL) return -1;
107 4 : const char *p = version;
108 4 : int v = 0;
109 20 : for (int i = 3; i >= 0; --i) {
110 16 : v += (1 << (i * 8)) * atoi(p);
111 16 : for (; (*p != '\0') && (*p != '.'); ++p);
112 16 : if (*p != '\0') ++p;
113 : }
114 4 : return v;
115 : }
116 :
117 5 : int StrToBool(const char *p) {
118 5 : if (p == NULL) return -1;
119 5 : if (EQUAL(p, "1") || EQUAL(p, "true") || EQUAL(p, "yes") || EQUAL(p, "enable") || EQUAL(p, "enabled") || EQUAL(p, "on")) return 1;
120 0 : if (EQUAL(p, "0") || EQUAL(p, "false") || EQUAL(p, "no") || EQUAL(p, "disable") || EQUAL(p, "disabled") || EQUAL(p, "off")) return 0;
121 0 : return -1;
122 : }
123 :
124 24 : int URLSearchAndReplace (CPLString *base, const char *search, const char *fmt, ...) {
125 24 : CPLString tmp;
126 : va_list args;
127 :
128 24 : size_t start = base->find(search);
129 24 : if (start == std::string::npos) {
130 0 : return -1;
131 : }
132 :
133 24 : va_start(args, fmt);
134 24 : tmp.vPrintf(fmt, args);
135 24 : va_end(args);
136 :
137 24 : base->replace(start, strlen(search), tmp);
138 24 : return start;
139 : }
|