1 : /******************************************************************************
2 : * $Id: ogrgeoconceptdriver.cpp
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implements OGRGeoconceptDriver class.
6 : * Author: Didier Richard, didier.richard@ign.fr
7 : * Language: C++
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2007, Geoconcept and IGN
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "ogrgeoconceptdatasource.h"
32 : #include "ogrgeoconceptdriver.h"
33 : #include "cpl_conv.h"
34 : #include "cpl_string.h"
35 :
36 : CPL_CVSID("$Id: ogrgeoconceptdriver.cpp 00000 2007-11-03 10:42:48Z drichard $");
37 :
38 : /************************************************************************/
39 : /* ~OGRGeoconceptDriver() */
40 : /************************************************************************/
41 :
42 96 : OGRGeoconceptDriver::~OGRGeoconceptDriver()
43 :
44 : {
45 96 : }
46 :
47 : /************************************************************************/
48 : /* GetName() */
49 : /************************************************************************/
50 :
51 1876 : const char *OGRGeoconceptDriver::GetName()
52 :
53 : {
54 1876 : return "Geoconcept";
55 : }
56 :
57 : /************************************************************************/
58 : /* Open() */
59 : /************************************************************************/
60 :
61 26 : OGRDataSource *OGRGeoconceptDriver::Open( const char* pszFilename,
62 : int bUpdate )
63 :
64 : {
65 : OGRGeoconceptDataSource *poDS;
66 :
67 26 : poDS = new OGRGeoconceptDataSource();
68 :
69 26 : if( !poDS->Open( pszFilename, TRUE, bUpdate ) )
70 : {
71 22 : delete poDS;
72 22 : return NULL;
73 : }
74 4 : return poDS;
75 : }
76 :
77 : /************************************************************************/
78 : /* CreateDataSource() */
79 : /* */
80 : /* Options (-dsco) : */
81 : /* EXTENSION=GXT|TXT (default GXT) */
82 : /************************************************************************/
83 :
84 1 : OGRDataSource *OGRGeoconceptDriver::CreateDataSource( const char* pszName,
85 : char** papszOptions )
86 :
87 : {
88 : VSIStatBuf stat;
89 1 : int bSingleNewFile = FALSE;
90 :
91 1 : if( pszName==NULL || strlen(pszName)==0 )
92 : {
93 : CPLError( CE_Failure, CPLE_AppDefined,
94 0 : "Invalid datasource name (null or empty)");
95 0 : return NULL;
96 : }
97 :
98 : /* -------------------------------------------------------------------- */
99 : /* Is the target a valid existing directory? */
100 : /* -------------------------------------------------------------------- */
101 1 : if( CPLStat( pszName, &stat ) == 0 )
102 : {
103 0 : if( !VSI_ISDIR(stat.st_mode) )
104 : {
105 : CPLError( CE_Failure, CPLE_AppDefined,
106 : "%s is not a valid existing directory.",
107 0 : pszName );
108 0 : return NULL;
109 : }
110 : }
111 :
112 : /* -------------------------------------------------------------------- */
113 : /* Does it end with the extension .gxt indicating the user likely */
114 : /* wants to create a single file set? */
115 : /* -------------------------------------------------------------------- */
116 1 : else if(
117 : EQUAL(CPLGetExtension(pszName),"gxt") ||
118 : EQUAL(CPLGetExtension(pszName),"txt")
119 : )
120 : {
121 1 : bSingleNewFile = TRUE;
122 : }
123 :
124 : /* -------------------------------------------------------------------- */
125 : /* Otherwise try to create a new directory. */
126 : /* -------------------------------------------------------------------- */
127 : else
128 : {
129 : VSIStatBuf sStat;
130 :
131 0 : if( VSIStat( pszName, &sStat ) == 0 )
132 : {
133 : CPLError( CE_Failure, CPLE_OpenFailed,
134 : "Attempt to create datasource named %s, "
135 : "but that is an existing directory.",
136 0 : pszName );
137 0 : return NULL;
138 : }
139 : }
140 :
141 : /* -------------------------------------------------------------------- */
142 : /* Return a new OGRDataSource() */
143 : /* -------------------------------------------------------------------- */
144 1 : OGRGeoconceptDataSource *poDS = NULL;
145 :
146 1 : poDS = new OGRGeoconceptDataSource();
147 1 : if( !poDS->Create( pszName, papszOptions ) )
148 : {
149 0 : delete poDS;
150 0 : return NULL;
151 : }
152 1 : return poDS;
153 : }
154 :
155 : /************************************************************************/
156 : /* DeleteDataSource() */
157 : /************************************************************************/
158 :
159 0 : OGRErr OGRGeoconceptDriver::DeleteDataSource( const char *pszDataSource )
160 :
161 : {
162 : int iExt;
163 : VSIStatBuf sStatBuf;
164 : static const char *apszExtensions[] =
165 : { "gxt", "txt", "gct", "gcm", "gcr", NULL };
166 :
167 0 : if( VSIStat( pszDataSource, &sStatBuf ) != 0 )
168 : {
169 : CPLError( CE_Failure, CPLE_AppDefined,
170 : "%s does not appear to be a file or directory.",
171 0 : pszDataSource );
172 :
173 0 : return OGRERR_FAILURE;
174 : }
175 :
176 0 : if( VSI_ISREG(sStatBuf.st_mode)
177 : && (
178 : EQUAL(CPLGetExtension(pszDataSource),"gxt") ||
179 : EQUAL(CPLGetExtension(pszDataSource),"txt")
180 : ) )
181 : {
182 0 : for( iExt=0; apszExtensions[iExt] != NULL; iExt++ )
183 : {
184 : const char *pszFile = CPLResetExtension(pszDataSource,
185 0 : apszExtensions[iExt] );
186 0 : if( VSIStat( pszFile, &sStatBuf ) == 0 )
187 0 : VSIUnlink( pszFile );
188 : }
189 : }
190 0 : else if( VSI_ISDIR(sStatBuf.st_mode) )
191 : {
192 0 : char **papszDirEntries = CPLReadDir( pszDataSource );
193 : int iFile;
194 :
195 0 : for( iFile = 0;
196 0 : papszDirEntries != NULL && papszDirEntries[iFile] != NULL;
197 : iFile++ )
198 : {
199 0 : if( CSLFindString( (char **) apszExtensions,
200 0 : CPLGetExtension(papszDirEntries[iFile])) != -1)
201 : {
202 : VSIUnlink( CPLFormFilename( pszDataSource,
203 0 : papszDirEntries[iFile],
204 0 : NULL ) );
205 : }
206 : }
207 :
208 0 : CSLDestroy( papszDirEntries );
209 :
210 0 : VSIRmdir( pszDataSource );
211 : }
212 :
213 0 : return OGRERR_NONE;
214 : }
215 :
216 :
217 : /************************************************************************/
218 : /* TestCapability() */
219 : /************************************************************************/
220 :
221 0 : int OGRGeoconceptDriver::TestCapability( const char * pszCap )
222 :
223 : {
224 0 : if( EQUAL(pszCap,ODrCCreateDataSource) )
225 0 : return TRUE;
226 0 : else if( EQUAL(pszCap,ODrCDeleteDataSource) )
227 0 : return TRUE;
228 : else
229 0 : return FALSE;
230 : }
231 :
232 : /************************************************************************/
233 : /* RegisterOGRGeoconcept() */
234 : /************************************************************************/
235 :
236 64 : void RegisterOGRGeoconcept()
237 :
238 : {
239 64 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRGeoconceptDriver );
240 64 : }
|