Main Page Class Hierarchy Compound List File List Compound Members File Members Related Pages

# gdalopen.cpp

    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.10  2002/04/16 13:11:48  warmerda
    00028  * updated
    00028  *
    00029  * Revision 1.12  2001/12/12 17:21:21  warmerda
    00030  * Use CPLStat instead of VSIStat().
    00031  *
    00032  * Revision 1.11  2001/08/20 13:40:28  warmerda
    00033  * modified message on failure to open if not a file
    00034  *
    00035  * Revision 1.10  2001/07/18 04:04:30  warmerda
    00036  * added CPL_CVSID
    00037  *
    00038  * Revision 1.9  2001/01/03 05:32:20  warmerda
    00039  * avoid depending on VSIStatBuf file size for 2GB issues
    00040  *
    00041  * Revision 1.8  2000/04/21 21:55:53  warmerda
    00042  * set filename as description of GDALDatasets.
    00043  *
    00044  * Revision 1.7  2000/01/10 15:43:06  warmerda
    00045  * Fixed debug statement.
    00046  *
    00047  * Revision 1.6  2000/01/10 15:31:02  warmerda
    00048  * Added debug statement in GDALOpen.
    00049  *
    00050  * Revision 1.5  1999/11/11 21:59:07  warmerda
    00051  * added GetDriver() for datasets
    00052  *
    00053  * Revision 1.4  1999/10/01 14:44:02  warmerda
    00054  * added documentation
    00055  *
    00056  * Revision 1.3  1999/04/21 04:00:34  warmerda
    00057  * Initialize fp to NULL.
    00058  *
    00059  * Revision 1.2  1998/12/31 18:52:45  warmerda
    00060  * Use CPL memory functions (they are safe), and fixed up header reading.
    00061  *
    00062  * Revision 1.1  1998/12/03 18:31:45  warmerda
    00063  * New
    00064  *
    00065  */
    00066
    00067 #include "gdal_priv.h"
    00068 #include "cpl_conv.h"
    00069
    00070 CPL_CVSID("$Id: gdalopen_cpp-source.html,v 1.10 2002/04/16 13:11:48 warmerda Exp $");
    00071
    00072 /************************************************************************/
    00073 /* ==================================================================== */
    00074 /*                             GDALOpenInfo                             */
    00075 /* ==================================================================== */
    00076 /************************************************************************/
    00077
    00078 /************************************************************************/
    00079 /*                            GDALOpenInfo()                            */
    00080 /************************************************************************/
    00081
    00082 GDALOpenInfo::GDALOpenInfo( const char * pszFilenameIn, GDALAccess eAccessIn )
    00083
    00084 {
    00085     pszFilename = CPLStrdup( pszFilenameIn );
    00086
    00087     nHeaderBytes = 0;
    00088     pabyHeader = NULL;
    00089     bStatOK = FALSE;
    00090     eAccess = eAccessIn;
    00091     fp = NULL;
    00092
    00093 /* -------------------------------------------------------------------- */
    00094 /*      Collect information about the file.                             */
    00095 /* -------------------------------------------------------------------- */
    00096     if( CPLStat( pszFilename, &sStat ) == 0 )
    00097     {
    00098         bStatOK = TRUE;
    00099
    00100         if( VSI_ISREG( sStat.st_mode ) )
    00101         {
    00102             pabyHeader = (GByte *) CPLCalloc(1025,1);
    00103
    00104             fp = VSIFOpen( pszFilename, "rb" );
    00105
    00106             if( fp != NULL )
    00107             {
    00108                 nHeaderBytes = VSIFRead( pabyHeader, 1, 1024, fp );
    00109
    00110                 VSIRewind( fp );
    00111             }
    00112         }
    00113     }
    00114 }
    00115
    00116 /************************************************************************/
    00117 /*                           ~GDALOpenInfo()                            */
    00118 /************************************************************************/
    00119
    00120 GDALOpenInfo::~GDALOpenInfo()
    00121
    00122 {
    00123     VSIFree( pabyHeader );
    00124     CPLFree( pszFilename );
    00125
    00126     if( fp != NULL )
    00127         VSIFClose( fp );
    00128 }
    00129
    00130 /************************************************************************/
    00131 /*                              GDALOpen()                              */
    00132 /************************************************************************/
    00133
    00149 GDALDatasetH GDALOpen( const char * pszFilename, GDALAccess eAccess )
    00150
    00151 {
    00152     int         iDriver;
    00153     GDALDriverManager *poDM = GetGDALDriverManager();
    00154     GDALOpenInfo oOpenInfo( pszFilename, eAccess );
    00155
    00156     CPLErrorReset();
    00157
    00158     for( iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++ )
    00159     {
    00160         GDALDriver      *poDriver = poDM->GetDriver( iDriver );
    00161         GDALDataset     *poDS;
    00162
    00163         poDS = poDriver->pfnOpen( &oOpenInfo );
    00164         if( poDS != NULL )
    00165         {
    00166             poDS->SetDescription( pszFilename );
    00167
    00168             if( poDS->poDriver == NULL )
    00169                 poDS->poDriver = poDriver;
    00170
    00171             CPLDebug( "GDAL", "GDALOpen(%s) succeeds as %s.\n",
    00172                       pszFilename, poDriver->pszLongName );
    00173
    00174             return (GDALDatasetH) poDS;
    00175         }
    00176
    00177         if( CPLGetLastErrorNo() != 0 )
    00178             return NULL;
    00179     }
    00180
    00181     if( oOpenInfo.bStatOK )
    00182         CPLError( CE_Failure, CPLE_OpenFailed,
    00183                   "`%s' not recognised as a supported file format.\n",
    00184                   pszFilename );
    00185     else
    00186         CPLError( CE_Failure, CPLE_OpenFailed,
    00187                   "`%s' does not exist in the file system,\n"
    00188                   "and is not recognised as a supported dataset name.\n",
    00189                   pszFilename );
    00190
    00191     return NULL;
    00192 }
    00193