LCOV - code coverage report
Current view: directory - frmts/wms - stuff.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 77 74 96.1 %
Date: 2012-12-26 Functions: 10 10 100.0 %

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

Generated by: LCOV version 1.7