1 : /******************************************************************************
2 : * $Id: ogrcsvdriver.cpp 23830 2012-01-30 23:07:44Z 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 23830 2012-01-30 23:07:44Z rouault $");
34 :
35 : /************************************************************************/
36 : /* ~OGRCSVDriver() */
37 : /************************************************************************/
38 :
39 369 : OGRCSVDriver::~OGRCSVDriver()
40 :
41 : {
42 369 : }
43 :
44 : /************************************************************************/
45 : /* GetName() */
46 : /************************************************************************/
47 :
48 23889 : const char *OGRCSVDriver::GetName()
49 :
50 : {
51 23889 : return "CSV";
52 : }
53 :
54 : /************************************************************************/
55 : /* Open() */
56 : /************************************************************************/
57 :
58 1198 : OGRDataSource *OGRCSVDriver::Open( const char * pszFilename, int bUpdate )
59 :
60 : {
61 1198 : OGRCSVDataSource *poDS = new OGRCSVDataSource();
62 :
63 1198 : if( !poDS->Open( pszFilename, bUpdate, FALSE ) )
64 : {
65 1040 : delete poDS;
66 1040 : poDS = NULL;
67 : }
68 :
69 1198 : return poDS;
70 : }
71 :
72 : /************************************************************************/
73 : /* CreateDataSource() */
74 : /************************************************************************/
75 :
76 8 : 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 8 : if (strcmp(pszName, "/dev/stdout") == 0)
86 0 : pszName = "/vsistdout/";
87 :
88 8 : 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 8 : CPLString osDirName;
102 :
103 8 : if( EQUAL(CPLGetExtension(pszName),"csv") )
104 : {
105 2 : osDirName = CPLGetPath(pszName);
106 2 : if( osDirName == "" )
107 0 : osDirName = ".";
108 :
109 : /* HACK: CPLGetPath("/vsimem/foo.csv") = "/vsimem", but this is not */
110 : /* recognized afterwards as a valid directory name */
111 2 : if( osDirName == "/vsimem" )
112 2 : osDirName = "/vsimem/";
113 : }
114 : else
115 : {
116 6 : if( strncmp(pszName, "/vsizip/", 8) == 0)
117 : {
118 : /* do nothing */
119 : }
120 6 : else if( !EQUAL(pszName, "/vsistdout/") &&
121 : VSIMkdir( pszName, 0755 ) != 0 )
122 : {
123 : CPLError( CE_Failure, CPLE_AppDefined,
124 : "Failed to create directory %s:\n%s",
125 0 : pszName, VSIStrerror( errno ) );
126 0 : return NULL;
127 : }
128 6 : osDirName = pszName;
129 : }
130 :
131 : /* -------------------------------------------------------------------- */
132 : /* Force it to open as a datasource. */
133 : /* -------------------------------------------------------------------- */
134 8 : OGRCSVDataSource *poDS = new OGRCSVDataSource();
135 :
136 16 : if( !poDS->Open( osDirName, TRUE, TRUE ) )
137 : {
138 0 : delete poDS;
139 0 : return NULL;
140 : }
141 :
142 8 : if( osDirName != pszName )
143 2 : poDS->SetDefaultCSVName( CPLGetFilename(pszName) );
144 :
145 8 : return poDS;
146 : }
147 :
148 : /************************************************************************/
149 : /* TestCapability() */
150 : /************************************************************************/
151 :
152 6 : int OGRCSVDriver::TestCapability( const char * pszCap )
153 :
154 : {
155 6 : if( EQUAL(pszCap,ODrCCreateDataSource) )
156 4 : return TRUE;
157 2 : else if( EQUAL(pszCap,ODrCDeleteDataSource) )
158 2 : return TRUE;
159 : else
160 0 : return FALSE;
161 : }
162 :
163 : /************************************************************************/
164 : /* DeleteDataSource() */
165 : /************************************************************************/
166 :
167 10 : OGRErr OGRCSVDriver::DeleteDataSource( const char *pszFilename )
168 :
169 : {
170 10 : if( CPLUnlinkTree( pszFilename ) == 0 )
171 6 : return OGRERR_NONE;
172 : else
173 4 : return OGRERR_FAILURE;
174 : }
175 :
176 : /************************************************************************/
177 : /* RegisterOGRCSV() */
178 : /************************************************************************/
179 :
180 389 : void RegisterOGRCSV()
181 :
182 : {
183 389 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRCSVDriver );
184 389 : }
185 :
|