00001 /****************************************************************************** 00002 * Copyright (c) 1998, Frank Warmerdam 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00020 * DEALINGS IN THE SOFTWARE. 00021 ****************************************************************************** 00022 * 00023 * gdalopen.c 00024 * 00025 * GDALOpen() function, and supporting functions. 00026 * 00027 * 00028 * $Log: gdalopen_cpp-source.html,v $ 00028 * Revision 1.8 2001/07/05 13:24:08 warmerda 00028 * *** empty log message *** 00028 * 00029 * Revision 1.9 2001/01/03 05:32:20 warmerda 00030 * avoid depending on VSIStatBuf file size for 2GB issues 00031 * 00032 * Revision 1.8 2000/04/21 21:55:53 warmerda 00033 * set filename as description of GDALDatasets. 00034 * 00035 * Revision 1.7 2000/01/10 15:43:06 warmerda 00036 * Fixed debug statement. 00037 * 00038 * Revision 1.6 2000/01/10 15:31:02 warmerda 00039 * Added debug statement in GDALOpen. 00040 * 00041 * Revision 1.5 1999/11/11 21:59:07 warmerda 00042 * added GetDriver() for datasets 00043 * 00044 * Revision 1.4 1999/10/01 14:44:02 warmerda 00045 * added documentation 00046 * 00047 * Revision 1.3 1999/04/21 04:00:34 warmerda 00048 * Initialize fp to NULL. 00049 * 00050 * Revision 1.2 1998/12/31 18:52:45 warmerda 00051 * Use CPL memory functions (they are safe), and fixed up header reading. 00052 * 00053 * Revision 1.1 1998/12/03 18:31:45 warmerda 00054 * New 00055 * 00056 */ 00057 00058 #include "gdal_priv.h" 00059 #include "cpl_conv.h" 00060 00061 /************************************************************************/ 00062 /* ==================================================================== */ 00063 /* GDALOpenInfo */ 00064 /* ==================================================================== */ 00065 /************************************************************************/ 00066 00067 /************************************************************************/ 00068 /* GDALOpenInfo() */ 00069 /************************************************************************/ 00070 00071 GDALOpenInfo::GDALOpenInfo( const char * pszFilenameIn, GDALAccess eAccessIn ) 00072 00073 { 00074 pszFilename = CPLStrdup( pszFilenameIn ); 00075 00076 nHeaderBytes = 0; 00077 pabyHeader = NULL; 00078 bStatOK = FALSE; 00079 eAccess = eAccessIn; 00080 fp = NULL; 00081 00082 /* -------------------------------------------------------------------- */ 00083 /* Collect information about the file. */ 00084 /* -------------------------------------------------------------------- */ 00085 if( VSIStat( pszFilename, &sStat ) == 0 ) 00086 { 00087 bStatOK = TRUE; 00088 00089 if( VSI_ISREG( sStat.st_mode ) ) 00090 { 00091 pabyHeader = (GByte *) CPLCalloc(1025,1); 00092 00093 fp = VSIFOpen( pszFilename, "rb" ); 00094 00095 if( fp != NULL ) 00096 { 00097 nHeaderBytes = VSIFRead( pabyHeader, 1, 1024, fp ); 00098 00099 VSIRewind( fp ); 00100 } 00101 } 00102 } 00103 } 00104 00105 /************************************************************************/ 00106 /* ~GDALOpenInfo() */ 00107 /************************************************************************/ 00108 00109 GDALOpenInfo::~GDALOpenInfo() 00110 00111 { 00112 VSIFree( pabyHeader ); 00113 CPLFree( pszFilename ); 00114 00115 if( fp != NULL ) 00116 VSIFClose( fp ); 00117 } 00118 00119 /************************************************************************/ 00120 /* GDALOpen() */ 00121 /************************************************************************/ 00122 00138 GDALDatasetH GDALOpen( const char * pszFilename, GDALAccess eAccess ) 00139 00140 { 00141 int iDriver; 00142 GDALDriverManager *poDM = GetGDALDriverManager(); 00143 GDALOpenInfo oOpenInfo( pszFilename, eAccess ); 00144 00145 CPLErrorReset(); 00146 00147 for( iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++ ) 00148 { 00149 GDALDriver *poDriver = poDM->GetDriver( iDriver ); 00150 GDALDataset *poDS; 00151 00152 poDS = poDriver->pfnOpen( &oOpenInfo ); 00153 if( poDS != NULL ) 00154 { 00155 poDS->SetDescription( pszFilename ); 00156 00157 if( poDS->poDriver == NULL ) 00158 poDS->poDriver = poDriver; 00159 00160 CPLDebug( "GDAL", "GDALOpen(%s) succeeds as %s.\n", 00161 pszFilename, poDriver->pszLongName ); 00162 00163 return (GDALDatasetH) poDS; 00164 } 00165 00166 if( CPLGetLastErrorNo() != 0 ) 00167 return NULL; 00168 } 00169 00170 CPLError( CE_Failure, CPLE_OpenFailed, 00171 "`%s' not recognised as a supported file format.\n", 00172 pszFilename ); 00173 00174 return NULL; 00175 } 00176