LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/csv - ogrcsvdriver.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 46 34 73.9 %
Date: 2011-12-18 Functions: 9 7 77.8 %

       1                 : /******************************************************************************
       2                 :  * $Id: ogrcsvdriver.cpp 23016 2011-08-31 21:24:12Z rouault $
       3                 :  *
       4                 :  * Project:  CSV Translator
       5                 :  * Purpose:  Implements OGRCSVDriver.
       6                 :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2004, Frank Warmerdam <warmerdam@pobox.com>
      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                 : #include "ogr_csv.h"
      31                 : #include "cpl_conv.h"
      32                 : 
      33                 : CPL_CVSID("$Id: ogrcsvdriver.cpp 23016 2011-08-31 21:24:12Z rouault $");
      34                 : 
      35                 : /************************************************************************/
      36                 : /*                           ~OGRCSVDriver()                            */
      37                 : /************************************************************************/
      38                 : 
      39             169 : OGRCSVDriver::~OGRCSVDriver()
      40                 : 
      41                 : {
      42             169 : }
      43                 : 
      44                 : /************************************************************************/
      45                 : /*                              GetName()                               */
      46                 : /************************************************************************/
      47                 : 
      48           10206 : const char *OGRCSVDriver::GetName()
      49                 : 
      50                 : {
      51           10206 :     return "CSV";
      52                 : }
      53                 : 
      54                 : /************************************************************************/
      55                 : /*                                Open()                                */
      56                 : /************************************************************************/
      57                 : 
      58             535 : OGRDataSource *OGRCSVDriver::Open( const char * pszFilename, int bUpdate )
      59                 : 
      60                 : {
      61             535 :     OGRCSVDataSource   *poDS = new OGRCSVDataSource();
      62                 : 
      63             535 :     if( !poDS->Open( pszFilename, bUpdate, FALSE ) )
      64                 :     {
      65             459 :         delete poDS;
      66             459 :         poDS = NULL;
      67                 :     }
      68                 : 
      69             535 :     return poDS;
      70                 : }
      71                 : 
      72                 : /************************************************************************/
      73                 : /*                          CreateDataSource()                          */
      74                 : /************************************************************************/
      75                 : 
      76               2 : OGRDataSource *OGRCSVDriver::CreateDataSource( const char * pszName,
      77                 :                                                char **papszOptions )
      78                 : 
      79                 : {
      80                 : /* -------------------------------------------------------------------- */
      81                 : /*      First, ensure there isn't any such file yet.                    */
      82                 : /* -------------------------------------------------------------------- */
      83                 :     VSIStatBufL sStatBuf;
      84                 : 
      85               2 :     if (strcmp(pszName, "/dev/stdout") == 0)
      86               0 :         pszName = "/vsistdout/";
      87                 : 
      88               2 :     if( VSIStatL( pszName, &sStatBuf ) == 0 )
      89                 :     {
      90                 :         CPLError( CE_Failure, CPLE_AppDefined, 
      91                 :                   "It seems a file system object called '%s' already exists.",
      92               0 :                   pszName );
      93                 : 
      94               0 :         return NULL;
      95                 :     }
      96                 : 
      97                 : /* -------------------------------------------------------------------- */
      98                 : /*      If the target is not a simple .csv then create it as a          */
      99                 : /*      directory.                                                      */
     100                 : /* -------------------------------------------------------------------- */
     101               2 :     CPLString osDirName;
     102                 : 
     103               2 :     if( EQUAL(CPLGetExtension(pszName),"csv") )
     104                 :     {
     105               0 :         osDirName = CPLGetPath(pszName);
     106               0 :         if( osDirName == "" )
     107               0 :             osDirName = ".";
     108                 :     }
     109                 :     else
     110                 :     {
     111               2 :         if( strncmp(pszName, "/vsizip/", 8) == 0)
     112                 :         {
     113                 :             /* do nothing */
     114                 :         }
     115               2 :         else if( !EQUAL(pszName, "/vsistdout/") &&
     116                 :             VSIMkdir( pszName, 0755 ) != 0 )
     117                 :         {
     118                 :             CPLError( CE_Failure, CPLE_AppDefined, 
     119                 :                       "Failed to create directory %s:\n%s", 
     120               0 :                       pszName, VSIStrerror( errno ) );
     121               0 :             return NULL;
     122                 :         }
     123               2 :         osDirName = pszName;
     124                 :     }
     125                 : 
     126                 : /* -------------------------------------------------------------------- */
     127                 : /*      Force it to open as a datasource.                               */
     128                 : /* -------------------------------------------------------------------- */
     129               2 :     OGRCSVDataSource   *poDS = new OGRCSVDataSource();
     130                 : 
     131               4 :     if( !poDS->Open( osDirName, TRUE, TRUE ) )
     132                 :     {
     133               0 :         delete poDS;
     134               0 :         return NULL;
     135                 :     }
     136                 : 
     137               2 :     if( osDirName != pszName )
     138               0 :         poDS->SetDefaultCSVName( CPLGetFilename(pszName) );
     139                 : 
     140               2 :     return poDS;
     141                 : }
     142                 : 
     143                 : /************************************************************************/
     144                 : /*                           TestCapability()                           */
     145                 : /************************************************************************/
     146                 : 
     147               2 : int OGRCSVDriver::TestCapability( const char * pszCap )
     148                 : 
     149                 : {
     150               2 :     if( EQUAL(pszCap,ODrCCreateDataSource) )
     151               1 :         return TRUE;
     152               1 :     else if( EQUAL(pszCap,ODrCDeleteDataSource) )
     153               1 :         return TRUE;
     154                 :     else
     155               0 :         return FALSE;
     156                 : }
     157                 : 
     158                 : /************************************************************************/
     159                 : /*                          DeleteDataSource()                          */
     160                 : /************************************************************************/
     161                 : 
     162               5 : OGRErr OGRCSVDriver::DeleteDataSource( const char *pszFilename )
     163                 : 
     164                 : {
     165               5 :     if( CPLUnlinkTree( pszFilename ) == 0 )
     166               3 :         return OGRERR_NONE;
     167                 :     else
     168               2 :         return OGRERR_FAILURE;
     169                 : }
     170                 : 
     171                 : /************************************************************************/
     172                 : /*                           RegisterOGRCSV()                           */
     173                 : /************************************************************************/
     174                 : 
     175             178 : void RegisterOGRCSV()
     176                 : 
     177                 : {
     178             178 :     OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRCSVDriver );
     179             178 : }
     180                 : 

Generated by: LCOV version 1.7