cpl_vsi_virtual.h

00001 /******************************************************************************
00002  * $Id: cpl_vsi_virtual.h 34165 2016-05-03 13:14:27Z rouault $
00003  *
00004  * Project:  VSI Virtual File System
00005  * Purpose:  Declarations for classes related to the virtual filesystem.
00006  *           These would only be normally required by applications implementing
00007  *           their own virtual file system classes which should be rare.
00008  *           The class interface may be fragile through versions.
00009  * Author:   Frank Warmerdam, warmerdam@pobox.com
00010  *
00011  ******************************************************************************
00012  * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
00013  * Copyright (c) 2010-2014, Even Rouault <even dot rouault at mines-paris dot org>
00014  *
00015  * Permission is hereby granted, free of charge, to any person obtaining a
00016  * copy of this software and associated documentation files (the "Software"),
00017  * to deal in the Software without restriction, including without limitation
00018  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00019  * and/or sell copies of the Software, and to permit persons to whom the
00020  * Software is furnished to do so, subject to the following conditions:
00021  *
00022  * The above copyright notice and this permission notice shall be included
00023  * in all copies or substantial portions of the Software.
00024  *
00025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00026  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00027  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00028  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00029  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00031  * DEALINGS IN THE SOFTWARE.
00032  ****************************************************************************/
00033 
00034 #ifndef CPL_VSI_VIRTUAL_H_INCLUDED
00035 #define CPL_VSI_VIRTUAL_H_INCLUDED
00036 
00037 #include "cpl_vsi.h"
00038 #include "cpl_vsi_error.h"
00039 #include "cpl_string.h"
00040 #include "cpl_multiproc.h"
00041 
00042 #include <map>
00043 #include <vector>
00044 #include <string>
00045 
00046 /************************************************************************/
00047 /*                           VSIVirtualHandle                           */
00048 /************************************************************************/
00049 
00050 class CPL_DLL VSIVirtualHandle {
00051   public:
00052     virtual int       Seek( vsi_l_offset nOffset, int nWhence ) = 0;
00053     virtual vsi_l_offset Tell() = 0;
00054     virtual size_t    Read( void *pBuffer, size_t nSize, size_t nMemb ) = 0;
00055     virtual int       ReadMultiRange( int nRanges, void ** ppData, const vsi_l_offset* panOffsets, const size_t* panSizes );
00056     virtual size_t    Write( const void *pBuffer, size_t nSize,size_t nMemb)=0;
00057     virtual int       Eof() = 0;
00058     virtual int       Flush() {return 0;}
00059     virtual int       Close() = 0;
00060     virtual int       Truncate( CPL_UNUSED vsi_l_offset nNewSize ) { return -1; }
00061     virtual void     *GetNativeFileDescriptor() { return NULL; }
00062     virtual           ~VSIVirtualHandle() { }
00063 };
00064 
00065 /************************************************************************/
00066 /*                         VSIFilesystemHandler                         */
00067 /************************************************************************/
00068 
00069 class CPL_DLL VSIFilesystemHandler {
00070 
00071 public:
00072 
00073     virtual ~VSIFilesystemHandler() {}
00074 
00075     VSIVirtualHandle *Open( const char *pszFilename,
00076                             const char *pszAccess );
00077 
00078     virtual VSIVirtualHandle *Open( const char *pszFilename,
00079                                     const char *pszAccess,
00080                                     bool bSetError ) = 0;
00081     virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags) = 0;
00082     virtual int Unlink( const char *pszFilename )
00083                       { (void) pszFilename; errno=ENOENT; return -1; }
00084     virtual int Mkdir( const char *pszDirname, long nMode )
00085                       {(void)pszDirname; (void)nMode; errno=ENOENT; return -1;}
00086     virtual int Rmdir( const char *pszDirname )
00087                       { (void) pszDirname; errno=ENOENT; return -1; }
00088     virtual char **ReadDir( const char *pszDirname )
00089                       { (void) pszDirname; return NULL; }
00090     virtual char **ReadDirEx( const char *pszDirname, int /* nMaxFiles */ )
00091                       { return ReadDir(pszDirname); }
00092     virtual int Rename( const char *oldpath, const char *newpath )
00093                       { (void) oldpath; (void)newpath; errno=ENOENT; return -1; }
00094     virtual int IsCaseSensitive( const char* pszFilename )
00095                       { (void) pszFilename; return TRUE; }
00096     virtual GIntBig GetDiskFreeSpace( const char* /* pszDirname */ ) { return -1; }
00097 };
00098 
00099 /************************************************************************/
00100 /*                            VSIFileManager                            */
00101 /************************************************************************/
00102 
00103 class CPL_DLL VSIFileManager
00104 {
00105 private:
00106     VSIFilesystemHandler *poDefaultHandler;
00107     std::map<std::string, VSIFilesystemHandler *> oHandlers;
00108 
00109     VSIFileManager();
00110 
00111     static VSIFileManager *Get();
00112 
00113 public:
00114     ~VSIFileManager();
00115 
00116     static VSIFilesystemHandler *GetHandler( const char * );
00117     static void InstallHandler( const std::string& osPrefix,
00118                                 VSIFilesystemHandler * );
00119     /* RemoveHandler is never defined. */
00120     /* static void RemoveHandler( const std::string& osPrefix ); */
00121 };
00122 
00123 
00124 /************************************************************************/
00125 /* ==================================================================== */
00126 /*                       VSIArchiveFilesystemHandler                   */
00127 /* ==================================================================== */
00128 /************************************************************************/
00129 
00130 class VSIArchiveEntryFileOffset
00131 {
00132     public:
00133         virtual ~VSIArchiveEntryFileOffset();
00134 };
00135 
00136 typedef struct
00137 {
00138     char         *fileName;
00139     vsi_l_offset  uncompressed_size;
00140     VSIArchiveEntryFileOffset*      file_pos;
00141     int           bIsDir;
00142     GIntBig       nModifiedTime;
00143 } VSIArchiveEntry;
00144 
00145 class VSIArchiveContent
00146 {
00147 public:
00148     time_t       mTime;
00149     vsi_l_offset nFileSize;
00150     int nEntries;
00151     VSIArchiveEntry* entries;
00152 
00153     VSIArchiveContent() : mTime(0), nFileSize(0), nEntries(0), entries(NULL) {}
00154     ~VSIArchiveContent();
00155 };
00156 
00157 class VSIArchiveReader
00158 {
00159     public:
00160         virtual ~VSIArchiveReader();
00161 
00162         virtual int GotoFirstFile() = 0;
00163         virtual int GotoNextFile() = 0;
00164         virtual VSIArchiveEntryFileOffset* GetFileOffset() = 0;
00165         virtual GUIntBig GetFileSize() = 0;
00166         virtual CPLString GetFileName() = 0;
00167         virtual GIntBig GetModifiedTime() = 0;
00168         virtual int GotoFileOffset(VSIArchiveEntryFileOffset* pOffset) = 0;
00169 };
00170 
00171 class VSIArchiveFilesystemHandler : public VSIFilesystemHandler
00172 {
00173 protected:
00174     CPLMutex* hMutex;
00175     /* We use a cache that contains the list of files contained in a VSIArchive file as */
00176     /* unarchive.c is quite inefficient in listing them. This speeds up access to VSIArchive files */
00177     /* containing ~1000 files like a CADRG product */
00178     std::map<CPLString,VSIArchiveContent*>   oFileList;
00179 
00180     virtual const char* GetPrefix() = 0;
00181     virtual std::vector<CPLString> GetExtensions() = 0;
00182     virtual VSIArchiveReader* CreateReader(const char* pszArchiveFileName) = 0;
00183 
00184 public:
00185     VSIArchiveFilesystemHandler();
00186     virtual ~VSIArchiveFilesystemHandler();
00187 
00188     virtual int      Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags );
00189     virtual int      Unlink( const char *pszFilename );
00190     virtual int      Rename( const char *oldpath, const char *newpath );
00191     virtual int      Mkdir( const char *pszDirname, long nMode );
00192     virtual int      Rmdir( const char *pszDirname );
00193     virtual char   **ReadDirEx( const char *pszDirname, int nMaxFiles );
00194 
00195     virtual const VSIArchiveContent* GetContentOfArchive(const char* archiveFilename, VSIArchiveReader* poReader = NULL);
00196     virtual char* SplitFilename(const char *pszFilename, CPLString &osFileInArchive, int bCheckMainFileExists);
00197     virtual VSIArchiveReader* OpenArchiveFile(const char* archiveFilename, const char* fileInArchiveName);
00198     virtual int FindFileInArchive(const char* archiveFilename, const char* fileInArchiveName, const VSIArchiveEntry** archiveEntry);
00199 };
00200 
00201 VSIVirtualHandle CPL_DLL *VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle);
00202 VSIVirtualHandle* VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle,
00203                                                 const GByte* pabyBeginningContent,
00204                                                 vsi_l_offset nCheatFileSize);
00205 VSIVirtualHandle CPL_DLL *VSICreateCachedFile( VSIVirtualHandle* poBaseHandle, size_t nChunkSize = 32768, size_t nCacheSize = 0 );
00206 VSIVirtualHandle CPL_DLL *VSICreateGZipWritable( VSIVirtualHandle* poBaseHandle, int bRegularZLibIn, int bAutoCloseBaseHandle );
00207 
00208 #endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */

Generated for GDAL by doxygen 1.6.1.