1 : /******************************************************************************
2 : * $Id: ogrodsdriver.cpp 23831 2012-01-30 23:12:23Z rouault $
3 : *
4 : * Project: ODS Translator
5 : * Purpose: Implements OGRODSDriver.
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2012, Even Rouault <even dot rouault at mines dash paris dot org>
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_ods.h"
31 : #include "cpl_conv.h"
32 :
33 : CPL_CVSID("$Id: ogrodsdriver.cpp 23831 2012-01-30 23:12:23Z rouault $");
34 :
35 : extern "C" void RegisterOGRODS();
36 :
37 : // g++ -DHAVE_EXPAT -g -Wall -fPIC ogr/ogrsf_frmts/ods/*.cpp -shared -o ogr_ODS.so -Iport -Igcore -Iogr -Iogr/ogrsf_frmts -Iogr/ogrsf_frmts/mem -Iogr/ogrsf_frmts/ods -L. -lgdal
38 :
39 : /************************************************************************/
40 : /* ~OGRODSDriver() */
41 : /************************************************************************/
42 :
43 214 : OGRODSDriver::~OGRODSDriver()
44 :
45 : {
46 214 : }
47 :
48 : /************************************************************************/
49 : /* GetName() */
50 : /************************************************************************/
51 :
52 13872 : const char *OGRODSDriver::GetName()
53 :
54 : {
55 13872 : return "ODS";
56 : }
57 :
58 : /************************************************************************/
59 : /* Open() */
60 : /************************************************************************/
61 :
62 62 : OGRDataSource *OGRODSDriver::Open( const char * pszFilename, int bUpdate )
63 :
64 : {
65 62 : CPLString osContentFilename;
66 62 : const char* pszContentFilename = pszFilename;
67 :
68 62 : VSILFILE* fpContent = NULL;
69 62 : VSILFILE* fpSettings = NULL;
70 :
71 62 : if (EQUAL(CPLGetExtension(pszFilename), "ODS"))
72 : {
73 12 : VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
74 12 : if (fp == NULL)
75 0 : return NULL;
76 :
77 12 : int bOK = FALSE;
78 : char szBuffer[1024];
79 12 : if (VSIFReadL(szBuffer, sizeof(szBuffer), 1, fp) == 1 &&
80 : memcmp(szBuffer, "PK", 2) == 0)
81 : {
82 12 : bOK = TRUE;
83 : }
84 :
85 12 : VSIFCloseL(fp);
86 :
87 12 : if (!bOK)
88 0 : return NULL;
89 :
90 12 : osContentFilename.Printf("/vsizip/%s/content.xml", pszFilename);
91 12 : pszContentFilename = osContentFilename.c_str();
92 : }
93 50 : else if (bUpdate) /* We cannot update the xml file, only the .ods */
94 : {
95 6 : return NULL;
96 : }
97 :
98 56 : if (EQUALN(pszContentFilename, "ODS:", 4) ||
99 : EQUAL(CPLGetFilename(pszContentFilename), "content.xml"))
100 : {
101 13 : if (EQUALN(pszContentFilename, "ODS:", 4))
102 1 : pszContentFilename += 4;
103 :
104 13 : fpContent = VSIFOpenL(pszContentFilename, "rb");
105 13 : if (fpContent == NULL)
106 0 : return NULL;
107 :
108 : char szBuffer[1024];
109 13 : int nRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fpContent);
110 13 : szBuffer[nRead] = 0;
111 :
112 13 : if (strstr(szBuffer, "<office:document-content") == NULL)
113 : {
114 0 : VSIFCloseL(fpContent);
115 0 : return NULL;
116 : }
117 :
118 : /* We could also check that there's a <office:spreadsheet>, but it might be further */
119 : /* in the XML due to styles, etc... */
120 : }
121 : else
122 : {
123 43 : return NULL;
124 : }
125 :
126 26 : if (EQUAL(CPLGetExtension(pszFilename), "ODS"))
127 : {
128 12 : fpSettings = VSIFOpenL(CPLSPrintf("/vsizip/%s/settings.xml", pszFilename), "rb");
129 : }
130 :
131 13 : OGRODSDataSource *poDS = new OGRODSDataSource();
132 :
133 26 : if( !poDS->Open( pszFilename, fpContent, fpSettings, bUpdate ) )
134 : {
135 0 : delete poDS;
136 0 : poDS = NULL;
137 : }
138 :
139 13 : return poDS;
140 : }
141 :
142 : /************************************************************************/
143 : /* CreateDataSource() */
144 : /************************************************************************/
145 :
146 1 : OGRDataSource *OGRODSDriver::CreateDataSource( const char * pszName,
147 : char **papszOptions )
148 :
149 : {
150 1 : if (!EQUAL(CPLGetExtension(pszName), "ODS"))
151 : {
152 0 : CPLError( CE_Failure, CPLE_AppDefined, "File extension should be ODS" );
153 0 : return NULL;
154 : }
155 :
156 : /* -------------------------------------------------------------------- */
157 : /* First, ensure there isn't any such file yet. */
158 : /* -------------------------------------------------------------------- */
159 : VSIStatBufL sStatBuf;
160 :
161 1 : if( VSIStatL( pszName, &sStatBuf ) == 0 )
162 : {
163 : CPLError( CE_Failure, CPLE_AppDefined,
164 : "It seems a file system object called '%s' already exists.",
165 0 : pszName );
166 :
167 0 : return NULL;
168 : }
169 :
170 : /* -------------------------------------------------------------------- */
171 : /* Try to create datasource. */
172 : /* -------------------------------------------------------------------- */
173 : OGRODSDataSource *poDS;
174 :
175 1 : poDS = new OGRODSDataSource();
176 :
177 1 : if( !poDS->Create( pszName, papszOptions ) )
178 : {
179 0 : delete poDS;
180 0 : return NULL;
181 : }
182 : else
183 1 : return poDS;
184 : }
185 :
186 : /************************************************************************/
187 : /* DeleteDataSource() */
188 : /************************************************************************/
189 :
190 0 : OGRErr OGRODSDriver::DeleteDataSource( const char *pszName )
191 : {
192 0 : if (VSIUnlink( pszName ) == 0)
193 0 : return OGRERR_NONE;
194 : else
195 0 : return OGRERR_FAILURE;
196 : }
197 :
198 : /************************************************************************/
199 : /* TestCapability() */
200 : /************************************************************************/
201 :
202 3 : int OGRODSDriver::TestCapability( const char * pszCap )
203 :
204 : {
205 3 : if( EQUAL(pszCap,ODrCCreateDataSource) )
206 1 : return TRUE;
207 2 : else if( EQUAL(pszCap,ODrCDeleteDataSource) )
208 0 : return TRUE;
209 : else
210 2 : return FALSE;
211 : }
212 :
213 : /************************************************************************/
214 : /* RegisterOGRODS() */
215 : /************************************************************************/
216 :
217 226 : void RegisterOGRODS()
218 :
219 : {
220 226 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRODSDriver );
221 226 : }
222 :
|