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