1 : /******************************************************************************
2 : * $Id: cpl_vsi_virtual.h 20028 2010-07-11 18:20:55Z rouault $
3 : *
4 : * Project: VSI Virtual File System
5 : * Purpose: Declarations for classes related to the virtual filesystem.
6 : * These would only be normally required by applications implmenting
7 : * their own virtual file system classes which should be rare.
8 : * The class interface may be fragile through versions.
9 : * Author: Frank Warmerdam, warmerdam@pobox.com
10 : *
11 : ******************************************************************************
12 : * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
13 : *
14 : * Permission is hereby granted, free of charge, to any person obtaining a
15 : * copy of this software and associated documentation files (the "Software"),
16 : * to deal in the Software without restriction, including without limitation
17 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 : * and/or sell copies of the Software, and to permit persons to whom the
19 : * Software is furnished to do so, subject to the following conditions:
20 : *
21 : * The above copyright notice and this permission notice shall be included
22 : * in all copies or substantial portions of the Software.
23 : *
24 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 : * DEALINGS IN THE SOFTWARE.
31 : ****************************************************************************/
32 :
33 : #ifndef CPL_VSI_VIRTUAL_H_INCLUDED
34 : #define CPL_VSI_VIRTUAL_H_INCLUDED
35 :
36 : #include "cpl_vsi.h"
37 : #include "cpl_string.h"
38 :
39 : #if defined(WIN32CE)
40 : # include "cpl_wince.h"
41 : # include <wce_errno.h>
42 : # pragma warning(disable:4786) /* Remove annoying warnings in eVC++ and VC++ 6.0 */
43 : #endif
44 :
45 : #include <map>
46 : #include <vector>
47 : #include <string>
48 :
49 : /************************************************************************/
50 : /* VSIVirtualHandle */
51 : /************************************************************************/
52 :
53 19170 : class CPL_DLL VSIVirtualHandle {
54 : public:
55 : virtual int Seek( vsi_l_offset nOffset, int nWhence ) = 0;
56 : virtual vsi_l_offset Tell() = 0;
57 : virtual size_t Read( void *pBuffer, size_t nSize, size_t nMemb ) = 0;
58 : virtual size_t Write( const void *pBuffer, size_t nSize,size_t nMemb)=0;
59 : virtual int Eof() = 0;
60 8 : virtual int Flush() {return 0;}
61 : virtual int Close() = 0;
62 19169 : virtual ~VSIVirtualHandle() { }
63 : };
64 :
65 : /************************************************************************/
66 : /* VSIFilesystemHandler */
67 : /************************************************************************/
68 :
69 4023 : class CPL_DLL VSIFilesystemHandler {
70 :
71 : public:
72 :
73 3888 : virtual ~VSIFilesystemHandler() {}
74 :
75 : virtual VSIVirtualHandle *Open( const char *pszFilename,
76 : const char *pszAccess) = 0;
77 : virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf) = 0;
78 0 : virtual int Unlink( const char *pszFilename )
79 0 : { errno=ENOENT; return -1; }
80 0 : virtual int Mkdir( const char *pszDirname, long nMode )
81 0 : { errno=ENOENT; return -1; }
82 0 : virtual int Rmdir( const char *pszDirname )
83 0 : { errno=ENOENT; return -1; }
84 0 : virtual char **ReadDir( const char *pszDirname )
85 0 : { return NULL; }
86 0 : virtual int Rename( const char *oldpath, const char *newpath )
87 0 : { errno=ENOENT; return -1; }
88 : };
89 :
90 : /************************************************************************/
91 : /* VSIFileManager */
92 : /************************************************************************/
93 :
94 : class CPL_DLL VSIFileManager
95 : {
96 : private:
97 : VSIFilesystemHandler *poDefaultHandler;
98 : std::map<std::string, VSIFilesystemHandler *> oHandlers;
99 :
100 : VSIFileManager();
101 :
102 : static VSIFileManager *Get();
103 :
104 : public:
105 : ~VSIFileManager();
106 :
107 : static VSIFilesystemHandler *GetHandler( const char * );
108 : static void InstallHandler( const std::string& osPrefix,
109 : VSIFilesystemHandler * );
110 : static void RemoveHandler( const std::string& osPrefix );
111 : };
112 :
113 :
114 : /************************************************************************/
115 : /* ==================================================================== */
116 : /* VSIArchiveFilesystemHandler */
117 : /* ==================================================================== */
118 : /************************************************************************/
119 :
120 : class VSIArchiveEntryFileOffset
121 32 : {
122 : public:
123 : virtual ~VSIArchiveEntryFileOffset();
124 : };
125 :
126 : typedef struct
127 : {
128 : char *fileName;
129 : vsi_l_offset uncompressed_size;
130 : VSIArchiveEntryFileOffset* file_pos;
131 : int bIsDir;
132 : GIntBig nModifiedTime;
133 : } VSIArchiveEntry;
134 :
135 : typedef struct
136 : {
137 : int nEntries;
138 : VSIArchiveEntry* entries;
139 : } VSIArchiveContent;
140 :
141 : class VSIArchiveReader
142 80 : {
143 : public:
144 : virtual ~VSIArchiveReader();
145 :
146 : virtual int GotoFirstFile() = 0;
147 : virtual int GotoNextFile() = 0;
148 : virtual VSIArchiveEntryFileOffset* GetFileOffset() = 0;
149 : virtual GUIntBig GetFileSize() = 0;
150 : virtual CPLString GetFileName() = 0;
151 : virtual GIntBig GetModifiedTime() = 0;
152 : virtual int GotoFileOffset(VSIArchiveEntryFileOffset* pOffset) = 0;
153 : };
154 :
155 : class VSIArchiveFilesystemHandler : public VSIFilesystemHandler
156 : {
157 : private:
158 : void* hMutex;
159 : /* We use a cache that contains the list of files containes in a VSIArchive file as */
160 : /* unarchive.c is quite inefficient in listing them. This speeds up access to VSIArchive files */
161 : /* containing ~1000 files like a CADRG product */
162 : std::map<CPLString,VSIArchiveContent*> oFileList;
163 :
164 : virtual const char* GetPrefix() = 0;
165 : virtual std::vector<CPLString> GetExtensions() = 0;
166 : virtual VSIArchiveReader* CreateReader(const char* pszArchiveFileName) = 0;
167 :
168 : public:
169 : VSIArchiveFilesystemHandler();
170 : virtual ~VSIArchiveFilesystemHandler();
171 :
172 : virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf );
173 : virtual int Unlink( const char *pszFilename );
174 : virtual int Rename( const char *oldpath, const char *newpath );
175 : virtual int Mkdir( const char *pszDirname, long nMode );
176 : virtual int Rmdir( const char *pszDirname );
177 : virtual char **ReadDir( const char *pszDirname );
178 :
179 : virtual const VSIArchiveContent* GetContentOfArchive(const char* archiveFilename, VSIArchiveReader* poReader = NULL);
180 : virtual char* SplitFilename(const char *pszFilename, CPLString &osFileInArchive);
181 : virtual VSIArchiveReader* OpenArchiveFile(const char* archiveFilename, const char* fileInArchiveName);
182 : virtual int FindFileInArchive(const char* archiveFilename, const char* fileInArchiveName, const VSIArchiveEntry** archiveEntry);
183 : };
184 :
185 : VSIVirtualHandle* VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle);
186 :
187 : #endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */
|