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.5 2000/11/06 04:49:01 warmerda 00028 * *** empty log message *** 00028 * 00029 * Revision 1.8 2000/04/21 21:55:53 warmerda 00030 * set filename as description of GDALDatasets. 00031 * 00032 * Revision 1.7 2000/01/10 15:43:06 warmerda 00033 * Fixed debug statement. 00034 * 00035 * Revision 1.6 2000/01/10 15:31:02 warmerda 00036 * Added debug statement in GDALOpen. 00037 * 00038 * Revision 1.5 1999/11/11 21:59:07 warmerda 00039 * added GetDriver() for datasets 00040 * 00041 * Revision 1.4 1999/10/01 14:44:02 warmerda 00042 * added documentation 00043 * 00044 * Revision 1.3 1999/04/21 04:00:34 warmerda 00045 * Initialize fp to NULL. 00046 * 00047 * Revision 1.2 1998/12/31 18:52:45 warmerda 00048 * Use CPL memory functions (they are safe), and fixed up header reading. 00049 * 00050 * Revision 1.1 1998/12/03 18:31:45 warmerda 00051 * New 00052 * 00053 */ 00054 00055 #include "gdal_priv.h" 00056 #include "cpl_conv.h" 00057 00058 /************************************************************************/ 00059 /* ==================================================================== */ 00060 /* GDALOpenInfo */ 00061 /* ==================================================================== */ 00062 /************************************************************************/ 00063 00064 /************************************************************************/ 00065 /* GDALOpenInfo() */ 00066 /************************************************************************/ 00067 00068 GDALOpenInfo::GDALOpenInfo( const char * pszFilenameIn, GDALAccess eAccessIn ) 00069 00070 { 00071 pszFilename = CPLStrdup( pszFilenameIn ); 00072 00073 nHeaderBytes = 0; 00074 pabyHeader = NULL; 00075 bStatOK = FALSE; 00076 eAccess = eAccessIn; 00077 fp = NULL; 00078 00079 /* -------------------------------------------------------------------- */ 00080 /* Collect information about the file. */ 00081 /* -------------------------------------------------------------------- */ 00082 if( VSIStat( pszFilename, &sStat ) == 0 ) 00083 { 00084 bStatOK = TRUE; 00085 00086 if( VSI_ISREG( sStat.st_mode ) ) 00087 { 00088 nHeaderBytes = MIN(1024,sStat.st_size); 00089 pabyHeader = (GByte *) CPLCalloc(nHeaderBytes+1,1); 00090 00091 fp = VSIFOpen( pszFilename, "rb" ); 00092 00093 if( fp != NULL ) 00094 { 00095 nHeaderBytes = VSIFRead( pabyHeader, 1, nHeaderBytes, fp ); 00096 00097 VSIRewind( fp ); 00098 } 00099 } 00100 } 00101 00102 } 00103 00104 /************************************************************************/ 00105 /* ~GDALOpenInfo() */ 00106 /************************************************************************/ 00107 00108 GDALOpenInfo::~GDALOpenInfo() 00109 00110 { 00111 VSIFree( pabyHeader ); 00112 CPLFree( pszFilename ); 00113 00114 if( fp != NULL ) 00115 VSIFClose( fp ); 00116 } 00117 00118 /************************************************************************/ 00119 /* GDALOpen() */ 00120 /************************************************************************/ 00121 00137 GDALDatasetH GDALOpen( const char * pszFilename, GDALAccess eAccess ) 00138 00139 { 00140 int iDriver; 00141 GDALDriverManager *poDM = GetGDALDriverManager(); 00142 GDALOpenInfo oOpenInfo( pszFilename, eAccess ); 00143 00144 CPLErrorReset(); 00145 00146 for( iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++ ) 00147 { 00148 GDALDriver *poDriver = poDM->GetDriver( iDriver ); 00149 GDALDataset *poDS; 00150 00151 poDS = poDriver->pfnOpen( &oOpenInfo ); 00152 if( poDS != NULL ) 00153 { 00154 poDS->SetDescription( pszFilename ); 00155 00156 if( poDS->poDriver == NULL ) 00157 poDS->poDriver = poDriver; 00158 00159 CPLDebug( "GDAL", "GDALOpen(%s) succeeds as %s.\n", 00160 pszFilename, poDriver->pszLongName ); 00161 00162 return (GDALDatasetH) poDS; 00163 } 00164 00165 if( CPLGetLastErrorNo() != 0 ) 00166 return NULL; 00167 } 00168 00169 CPLError( CE_Failure, CPLE_OpenFailed, 00170 "`%s' not recognised as a supported file format.\n", 00171 pszFilename ); 00172 00173 return NULL; 00174 } 00175