1 : /**********************************************************************
2 : * $Id: mitab_ogr_driver.cpp,v 1.11 2005/05/21 03:15:18 fwarmerdam Exp $
3 : *
4 : * Name: mitab_ogr_driver.cpp
5 : * Project: MapInfo Mid/Mif, Tab ogr support
6 : * Language: C++
7 : * Purpose: Implementation of the MIDDATAFile class used to handle
8 : * reading/writing of the MID/MIF files
9 : * Author: Stephane Villeneuve, stephane.v@videotron.ca
10 : *
11 : **********************************************************************
12 : * Copyright (c) 1999, 2000, Stephane Villeneuve
13 : *
14 : * Permission is hereby granted, free of charge, to any person obtaining a
15 : * copy of this software and associated documentation files (the "Software"),
16 : * to deal in the Software without restriction, including without limitation
17 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 : * and/or sell copies of the Software, and to permit persons to whom the
19 : * Software is furnished to do so, subject to the following conditions:
20 : *
21 : * The above copyright notice and this permission notice shall be included
22 : * in all copies or substantial portions of the Software.
23 : *
24 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 : * DEALINGS IN THE SOFTWARE.
31 : **********************************************************************
32 : *
33 : * $Log: mitab_ogr_driver.cpp,v $
34 : * Revision 1.11 2005/05/21 03:15:18 fwarmerdam
35 : * Removed unused stat buffer.
36 : *
37 : * Revision 1.10 2004/02/27 21:06:03 fwarmerdam
38 : * Better support for "single file" creation ... don't allow other layers to
39 : * be created. But *do* single file to satisfy the first layer creation request
40 : * made. Also, allow creating a datasource "on" an existing directory.
41 : *
42 : * Revision 1.9 2003/03/20 15:57:46 warmerda
43 : * Added delete datasource support
44 : *
45 : * Revision 1.8 2001/01/22 16:03:58 warmerda
46 : * expanded tabs
47 : *
48 : * Revision 1.7 2000/01/26 18:17:00 warmerda
49 : * reimplement OGR driver
50 : *
51 : * Revision 1.6 2000/01/15 22:30:44 daniel
52 : * Switch to MIT/X-Consortium OpenSource license
53 : *
54 : * Revision 1.5 1999/12/15 17:05:24 warmerda
55 : * Only create OGRTABDataSource if SmartOpen() result is non-NULL.
56 : *
57 : * Revision 1.4 1999/12/15 16:28:17 warmerda
58 : * fixed a few type problems
59 : *
60 : * Revision 1.3 1999/12/14 02:22:29 daniel
61 : * Merged TAB+MIF DataSource/Driver into ane using IMapInfoFile class
62 : *
63 : * Revision 1.2 1999/11/12 02:44:36 stephane
64 : * added comment, change Register name.
65 : *
66 : * Revision 1.1 1999/11/08 21:05:51 svillene
67 : * first revision
68 : *
69 : * Revision 1.1 1999/11/08 04:16:07 stephane
70 : * First Revision
71 : *
72 : **********************************************************************/
73 :
74 : #include "mitab_ogr_driver.h"
75 :
76 :
77 : /************************************************************************/
78 : /* ~OGRTABDriver() */
79 : /************************************************************************/
80 :
81 72 : OGRTABDriver::~OGRTABDriver()
82 :
83 : {
84 72 : }
85 :
86 : /************************************************************************/
87 : /* OGRTABDriver::GetName() */
88 : /************************************************************************/
89 :
90 3336 : const char *OGRTABDriver::GetName()
91 :
92 : {
93 3336 : return "MapInfo File";
94 : }
95 :
96 : /************************************************************************/
97 : /* OGRTABDriver::Open() */
98 : /************************************************************************/
99 :
100 : OGRDataSource *OGRTABDriver::Open( const char * pszFilename,
101 310 : int bUpdate )
102 :
103 : {
104 : OGRTABDataSource *poDS;
105 :
106 310 : if( bUpdate )
107 : {
108 122 : return NULL;
109 : }
110 :
111 188 : poDS = new OGRTABDataSource();
112 188 : if( poDS->Open( pszFilename, TRUE ) )
113 13 : return poDS;
114 : else
115 : {
116 175 : delete poDS;
117 175 : return NULL;
118 : }
119 : }
120 :
121 :
122 : /************************************************************************/
123 : /* CreateDataSource() */
124 : /************************************************************************/
125 :
126 : OGRDataSource *OGRTABDriver::CreateDataSource( const char * pszName,
127 9 : char ** papszOptions )
128 :
129 : {
130 : OGRTABDataSource *poDS;
131 :
132 : /* -------------------------------------------------------------------- */
133 : /* Try to create the data source. */
134 : /* -------------------------------------------------------------------- */
135 9 : poDS = new OGRTABDataSource();
136 9 : if( !poDS->Create( pszName, papszOptions ) )
137 : {
138 0 : delete poDS;
139 0 : return NULL;
140 : }
141 : else
142 9 : return poDS;
143 : }
144 :
145 : /************************************************************************/
146 : /* TestCapability() */
147 : /************************************************************************/
148 :
149 4 : int OGRTABDriver::TestCapability( const char * pszCap )
150 :
151 : {
152 4 : if( EQUAL(pszCap,ODrCCreateDataSource) )
153 4 : return TRUE;
154 0 : else if( EQUAL(pszCap,ODrCDeleteDataSource) )
155 0 : return TRUE;
156 : else
157 0 : return FALSE;
158 : }
159 :
160 : /************************************************************************/
161 : /* DeleteDataSource() */
162 : /************************************************************************/
163 :
164 9 : OGRErr OGRTABDriver::DeleteDataSource( const char *pszDataSource )
165 :
166 : {
167 : int iExt;
168 : VSIStatBuf sStatBuf;
169 : static const char *apszExtensions[] =
170 : { "mif", "mid", "tab", "map", "ind", "dat", "id", NULL };
171 :
172 9 : if( VSIStat( pszDataSource, &sStatBuf ) != 0 )
173 : {
174 : CPLError( CE_Failure, CPLE_AppDefined,
175 : "%s does not appear to be a file or directory.",
176 1 : pszDataSource );
177 :
178 1 : return OGRERR_FAILURE;
179 : }
180 :
181 8 : if( VSI_ISREG(sStatBuf.st_mode)
182 : && (EQUAL(CPLGetExtension(pszDataSource),"mif")
183 : || EQUAL(CPLGetExtension(pszDataSource),"mid")
184 : || EQUAL(CPLGetExtension(pszDataSource),"tab")) )
185 : {
186 48 : for( iExt=0; apszExtensions[iExt] != NULL; iExt++ )
187 : {
188 : const char *pszFile = CPLResetExtension(pszDataSource,
189 42 : apszExtensions[iExt] );
190 42 : if( VSIStat( pszFile, &sStatBuf ) == 0 )
191 14 : VSIUnlink( pszFile );
192 : }
193 : }
194 2 : else if( VSI_ISDIR(sStatBuf.st_mode) )
195 : {
196 2 : char **papszDirEntries = CPLReadDir( pszDataSource );
197 : int iFile;
198 :
199 20 : for( iFile = 0;
200 : papszDirEntries != NULL && papszDirEntries[iFile] != NULL;
201 : iFile++ )
202 : {
203 18 : if( CSLFindString( (char **) apszExtensions,
204 : CPLGetExtension(papszDirEntries[iFile])) != -1)
205 : {
206 : VSIUnlink( CPLFormFilename( pszDataSource,
207 : papszDirEntries[iFile],
208 8 : NULL ) );
209 : }
210 : }
211 :
212 2 : CSLDestroy( papszDirEntries );
213 :
214 2 : VSIRmdir( pszDataSource );
215 : }
216 :
217 8 : return OGRERR_NONE;
218 : }
219 :
220 : /************************************************************************/
221 : /* RegisterOGRTAB() */
222 : /************************************************************************/
223 :
224 : extern "C"
225 : {
226 :
227 80 : void RegisterOGRTAB()
228 :
229 : {
230 80 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRTABDriver );
231 80 : }
232 :
233 : }
|