root/tags/gdal_1_3_2/gcore/gdalopeninfo.cpp

Revision 8241, 4.9 kB (checked in by fwarmerdam, 3 years ago)

Get header info with large file API if ENOENT. This is intended to
provide access to in-memory and other virtual filesystem objects.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /******************************************************************************
2  * $Id$
3  *
4  * Project:  GDAL Core
5  * Purpose:  Implementation of GDALOpenInfo class.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  *
8  **********************************************************************
9  * Copyright (c) 2002, Frank Warmerdam
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ******************************************************************************
29  *
30  * $Log$
31  * Revision 1.2  2005/09/11 16:33:34  fwarmerdam
32  * Get header info with large file API if ENOENT.  This is intended to
33  * provide access to in-memory and other virtual filesystem objects.
34  *
35  * Revision 1.1  2005/07/11 17:09:26  fwarmerdam
36  * New
37  *
38  */
39
40 #include "gdal_priv.h"
41 #include "cpl_conv.h"
42
43 CPL_CVSID("$Id$");
44
45 /************************************************************************/
46 /* ==================================================================== */
47 /*                             GDALOpenInfo                             */
48 /* ==================================================================== */
49 /************************************************************************/
50
51 /************************************************************************/
52 /*                            GDALOpenInfo()                            */
53 /************************************************************************/
54
55 GDALOpenInfo::GDALOpenInfo( const char * pszFilenameIn, GDALAccess eAccessIn )
56
57 {
58 /* -------------------------------------------------------------------- */
59 /*      Ensure that C: is treated as C:\ so we can stat it on           */
60 /*      Windows.  Similar to what is done in CPLStat().                 */
61 /* -------------------------------------------------------------------- */
62 #ifdef WIN32
63     if( strlen(pszFilenameIn) == 2 && pszFilenameIn[1] == ':' )
64     {
65         char    szAltPath[10];
66        
67         strcpy( szAltPath, pszFilenameIn );
68         strcat( szAltPath, "\\" );
69         pszFilename = CPLStrdup( szAltPath );
70     }
71     else
72 #endif
73         pszFilename = CPLStrdup( pszFilenameIn );
74
75 /* -------------------------------------------------------------------- */
76 /*      Initialize.                                                     */
77 /* -------------------------------------------------------------------- */
78
79     nHeaderBytes = 0;
80     pabyHeader = NULL;
81     bIsDirectory = FALSE;
82     bStatOK = FALSE;
83     eAccess = eAccessIn;
84     fp = NULL;
85    
86 /* -------------------------------------------------------------------- */
87 /*      Collect information about the file.                             */
88 /* -------------------------------------------------------------------- */
89     VSIStatBufL  sStat;
90
91     if( VSIStatL( pszFilename, &sStat ) == 0 )
92     {
93         bStatOK = TRUE;
94
95         if( VSI_ISREG( sStat.st_mode ) )
96         {
97             pabyHeader = (GByte *) CPLCalloc(1025,1);
98
99             fp = VSIFOpen( pszFilename, "rb" );
100
101             if( fp != NULL )
102             {
103                 nHeaderBytes = (int) VSIFRead( pabyHeader, 1, 1024, fp );
104
105                 VSIRewind( fp );
106             }
107             else if( errno == 27 /* "File to large" */
108                      || errno == ENOENT )
109             {
110                 fp = VSIFOpenL( pszFilename, "rb" );
111                 if( fp != NULL )
112                 {
113                     nHeaderBytes = (int) VSIFReadL( pabyHeader, 1, 1024, fp );
114                     VSIFCloseL( fp );
115                     fp = NULL;
116                 }
117             }
118         }
119         else if( VSI_ISDIR( sStat.st_mode ) )
120             bIsDirectory = TRUE;
121     }
122 }
123
124 /************************************************************************/
125 /*                           ~GDALOpenInfo()                            */
126 /************************************************************************/
127
128 GDALOpenInfo::~GDALOpenInfo()
129
130 {
131     VSIFree( pabyHeader );
132     CPLFree( pszFilename );
133
134     if( fp != NULL )
135         VSIFClose( fp );
136 }
137
Note: See TracBrowser for help on using the browser.