1 : /******************************************************************************
2 : * $Id: cache.cpp 21880 2011-02-27 14:28:26Z 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 : GDALWMSCache::GDALWMSCache() {
34 2 : m_cache_path = "./gdalwmscache";
35 2 : m_postfix = "";
36 2 : m_cache_depth = 2;
37 2 : }
38 :
39 2 : GDALWMSCache::~GDALWMSCache() {
40 2 : }
41 :
42 2 : CPLErr GDALWMSCache::Initialize(CPLXMLNode *config) {
43 2 : const char *cache_path = CPLGetXMLValue(config, "Path", "./gdalwmscache");
44 2 : m_cache_path = cache_path;
45 :
46 2 : const char *cache_depth = CPLGetXMLValue(config, "Depth", "2");
47 2 : m_cache_depth = atoi(cache_depth);
48 :
49 2 : const char *cache_extension = CPLGetXMLValue(config, "Extension", "");
50 4 : m_postfix = cache_extension;
51 :
52 2 : return CE_None;
53 : }
54 :
55 0 : CPLErr GDALWMSCache::Write(const char *key, const CPLString &file_name) {
56 0 : CPLString cache_file(KeyToCacheFile(key));
57 : // printf("GDALWMSCache::Write(%s, %s) -> %s\n", key, file_name.c_str());
58 0 : if (CPLCopyFile(cache_file.c_str(), file_name.c_str()) != CE_None) {
59 0 : MakeDirs(cache_file.c_str());
60 0 : CPLCopyFile(cache_file.c_str(), file_name.c_str());
61 : }
62 :
63 0 : return CE_None;
64 : }
65 :
66 0 : CPLErr GDALWMSCache::Read(const char *key, CPLString *file_name) {
67 0 : CPLErr ret = CE_Failure;
68 0 : CPLString cache_file(KeyToCacheFile(key));
69 0 : VSILFILE* fp = VSIFOpenL(cache_file.c_str(), "rb");
70 0 : if (fp != NULL)
71 : {
72 0 : VSIFCloseL(fp);
73 0 : *file_name = cache_file;
74 0 : ret = CE_None;
75 : }
76 : // printf("GDALWMSCache::Read(...) -> %s\n", cache_file.c_str());
77 :
78 0 : return ret;
79 : }
80 :
81 0 : CPLString GDALWMSCache::KeyToCacheFile(const char *key) {
82 0 : CPLString hash(MD5String(key));
83 0 : CPLString cache_file(m_cache_path);
84 :
85 0 : if (cache_file.size() && (cache_file[cache_file.size() - 1] != '/')) cache_file.append(1, '/');
86 0 : for (int i = 0; i < m_cache_depth; ++i) {
87 0 : cache_file.append(1, hash[i]);
88 0 : cache_file.append(1, '/');
89 : }
90 0 : cache_file.append(hash);
91 0 : cache_file.append(m_postfix);
92 0 : return cache_file;
93 : }
|