1 : /******************************************************************************
2 : * $Id: stuff.cpp 22177 2011-04-16 19:45:11Z 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 0 : CPLString MD5String(const char *s) {
34 : unsigned char hash[16];
35 : char hhash[33];
36 0 : const char *tohex = "0123456789abcdef";
37 : struct cvs_MD5Context context;
38 0 : cvs_MD5Init(&context);
39 0 : cvs_MD5Update(&context, reinterpret_cast<unsigned char const *>(s), strlen(s));
40 0 : cvs_MD5Final(hash, &context);
41 0 : for (int i = 0; i < 16; ++i) {
42 0 : hhash[i * 2] = tohex[(hash[i] >> 4) & 0xf];
43 0 : hhash[i * 2 + 1] = tohex[hash[i] & 0xf];
44 : }
45 0 : hhash[32] = '\0';
46 0 : return CPLString(hhash);
47 : }
48 :
49 10 : CPLString ProjToWKT(const CPLString &proj) {
50 10 : char* wkt = NULL;
51 10 : OGRSpatialReference sr;
52 10 : CPLString srs;
53 :
54 : /* We could of course recognize OSGEO:41001 to SetFromUserInput(), but this hackish SRS */
55 : /* is almost only used in the context of WMS */
56 10 : if (strcmp(proj.c_str(),"OSGEO:41001") == 0)
57 : {
58 0 : if (sr.SetFromUserInput("EPSG:3857") != OGRERR_NONE) return srs;
59 : }
60 10 : else if (EQUAL(proj.c_str(),"EPSG:NONE"))
61 : {
62 : return srs;
63 : }
64 : else
65 : {
66 10 : if (sr.SetFromUserInput(proj.c_str()) != OGRERR_NONE) return srs;
67 : }
68 10 : sr.exportToWkt(&wkt);
69 10 : srs = wkt;
70 10 : OGRFree(wkt);
71 0 : return srs;
72 : }
73 :
74 14 : void URLAppend(CPLString *url, const char *s) {
75 14 : if ((s == NULL) || (s[0] == '\0')) return;
76 12 : if (s[0] == '&') {
77 10 : if (url->find('?') == std::string::npos) url->append(1, '?');
78 10 : if (((*url)[url->size() - 1] == '?') || ((*url)[url->size() - 1] == '&')) url->append(s + 1);
79 10 : else url->append(s);
80 2 : } else url->append(s);
81 : }
82 :
83 9 : void URLAppendF(CPLString *url, const char *s, ...) {
84 9 : CPLString tmp;
85 : va_list args;
86 :
87 9 : va_start(args, s);
88 9 : tmp.vPrintf(s, args);
89 9 : va_end(args);
90 :
91 9 : URLAppend(url, tmp.c_str());
92 9 : }
93 :
94 2 : void URLAppend(CPLString *url, const CPLString &s) {
95 2 : URLAppend(url, s.c_str());
96 2 : }
97 :
98 3 : CPLString BufferToVSIFile(GByte *buffer, size_t size) {
99 3 : CPLString file_name;
100 :
101 3 : file_name.Printf("/vsimem/wms/%p/wmsresult.dat", buffer);
102 3 : VSILFILE *f = VSIFileFromMemBuffer(file_name.c_str(), buffer, size, false);
103 3 : if (f == NULL) return CPLString();
104 3 : VSIFCloseL(f);
105 3 : return file_name;
106 : }
107 :
108 0 : CPLErr MakeDirs(const char *path) {
109 0 : char *p = CPLStrdup(CPLGetDirname(path));
110 0 : if (strlen(p) >= 2) {
111 0 : MakeDirs(p);
112 : }
113 0 : VSIMkdir(p, 0744);
114 0 : CPLFree(p);
115 0 : return CE_None;
116 : }
117 :
118 144 : int VersionStringToInt(const char *version) {
119 144 : if (version == NULL) return -1;
120 144 : const char *p = version;
121 144 : int v = 0;
122 720 : for (int i = 3; i >= 0; --i) {
123 576 : v += (1 << (i * 8)) * atoi(p);
124 576 : for (; (*p != '\0') && (*p != '.'); ++p);
125 576 : if (*p != '\0') ++p;
126 : }
127 144 : return v;
128 : }
129 :
130 16 : int StrToBool(const char *p) {
131 16 : if (p == NULL) return -1;
132 16 : if (EQUAL(p, "1") || EQUAL(p, "true") || EQUAL(p, "yes") || EQUAL(p, "enable") || EQUAL(p, "enabled") || EQUAL(p, "on")) return 1;
133 9 : if (EQUAL(p, "0") || EQUAL(p, "false") || EQUAL(p, "no") || EQUAL(p, "disable") || EQUAL(p, "disabled") || EQUAL(p, "off")) return 0;
134 0 : return -1;
135 : }
136 :
137 2 : int URLSearchAndReplace (CPLString *base, const char *search, const char *fmt, ...) {
138 2 : CPLString tmp;
139 : va_list args;
140 :
141 2 : size_t start = base->find(search);
142 2 : if (start == std::string::npos) {
143 0 : return -1;
144 : }
145 :
146 2 : va_start(args, fmt);
147 2 : tmp.vPrintf(fmt, args);
148 2 : va_end(args);
149 :
150 2 : base->replace(start, strlen(search), tmp);
151 2 : return start;
152 : }
|