1 : /******************************************************************************
2 : * $Id: ogrxlsxdriver.cpp 23816 2012-01-28 17:37:33Z rouault $
3 : *
4 : * Project: XLSX Translator
5 : * Purpose: Implements OGRXLSXDriver.
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_xlsx.h"
31 : #include "cpl_conv.h"
32 :
33 : CPL_CVSID("$Id: ogrxlsxdriver.cpp 23816 2012-01-28 17:37:33Z rouault $");
34 :
35 : extern "C" void RegisterOGRXLSX();
36 :
37 : // g++ -DHAVE_EXPAT -g -Wall -fPIC ogr/ogrsf_frmts/xlsx/*.cpp -shared -o ogr_XLSX.so -Iport -Igcore -Iogr -Iogr/ogrsf_frmts -Iogr/ogrsf_frmts/mem -Iogr/ogrsf_frmts/xlsx -L. -lgdal
38 :
39 : /************************************************************************/
40 : /* ~OGRXLSXDriver() */
41 : /************************************************************************/
42 :
43 369 : OGRXLSXDriver::~OGRXLSXDriver()
44 :
45 : {
46 369 : }
47 :
48 : /************************************************************************/
49 : /* GetName() */
50 : /************************************************************************/
51 :
52 23454 : const char *OGRXLSXDriver::GetName()
53 :
54 : {
55 23454 : return "XLSX";
56 : }
57 :
58 : /************************************************************************/
59 : /* Open() */
60 : /************************************************************************/
61 :
62 : #define XLSX_MIMETYPE "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"
63 :
64 82 : OGRDataSource *OGRXLSXDriver::Open( const char * pszFilename, int bUpdate )
65 :
66 : {
67 82 : if (!EQUAL(CPLGetExtension(pszFilename), "XLSX"))
68 58 : return NULL;
69 :
70 24 : VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
71 24 : if (fp == NULL)
72 0 : return NULL;
73 :
74 24 : int bOK = FALSE;
75 : char szBuffer[2048];
76 24 : if (VSIFReadL(szBuffer, sizeof(szBuffer), 1, fp) == 1 &&
77 : memcmp(szBuffer, "PK", 2) == 0)
78 : {
79 24 : bOK = TRUE;
80 : }
81 :
82 24 : VSIFCloseL(fp);
83 :
84 24 : if (!bOK)
85 0 : return NULL;
86 :
87 24 : VSILFILE* fpContent = VSIFOpenL(CPLSPrintf("/vsizip/%s/[Content_Types].xml", pszFilename), "rb");
88 24 : if (fpContent == NULL)
89 0 : return NULL;
90 :
91 24 : int nRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fpContent);
92 24 : szBuffer[nRead] = 0;
93 :
94 24 : VSIFCloseL(fpContent);
95 :
96 24 : if (strstr(szBuffer, XLSX_MIMETYPE) == NULL)
97 0 : return NULL;
98 :
99 24 : VSILFILE* fpWorkbook = VSIFOpenL(CPLSPrintf("/vsizip/%s/xl/workbook.xml", pszFilename), "rb");
100 24 : if (fpWorkbook == NULL)
101 0 : return NULL;
102 :
103 24 : VSILFILE* fpSharedStrings = VSIFOpenL(CPLSPrintf("/vsizip/%s/xl/sharedStrings.xml", pszFilename), "rb");
104 24 : VSILFILE* fpStyles = VSIFOpenL(CPLSPrintf("/vsizip/%s/xl/styles.xml", pszFilename), "rb");
105 :
106 24 : OGRXLSXDataSource *poDS = new OGRXLSXDataSource();
107 :
108 24 : if( !poDS->Open( pszFilename, fpWorkbook, fpSharedStrings, fpStyles, bUpdate ) )
109 : {
110 0 : delete poDS;
111 0 : poDS = NULL;
112 : }
113 :
114 24 : return poDS;
115 : }
116 :
117 : /************************************************************************/
118 : /* CreateDataSource() */
119 : /************************************************************************/
120 :
121 2 : OGRDataSource *OGRXLSXDriver::CreateDataSource( const char * pszName,
122 : char **papszOptions )
123 :
124 : {
125 2 : if (!EQUAL(CPLGetExtension(pszName), "XLSX"))
126 : {
127 0 : CPLError( CE_Failure, CPLE_AppDefined, "File extension should be XLSX" );
128 0 : return NULL;
129 : }
130 :
131 : /* -------------------------------------------------------------------- */
132 : /* First, ensure there isn't any such file yet. */
133 : /* -------------------------------------------------------------------- */
134 : VSIStatBufL sStatBuf;
135 :
136 2 : if( VSIStatL( pszName, &sStatBuf ) == 0 )
137 : {
138 : CPLError( CE_Failure, CPLE_AppDefined,
139 : "It seems a file system object called '%s' already exists.",
140 0 : pszName );
141 :
142 0 : return NULL;
143 : }
144 :
145 : /* -------------------------------------------------------------------- */
146 : /* Try to create datasource. */
147 : /* -------------------------------------------------------------------- */
148 : OGRXLSXDataSource *poDS;
149 :
150 2 : poDS = new OGRXLSXDataSource();
151 :
152 2 : if( !poDS->Create( pszName, papszOptions ) )
153 : {
154 0 : delete poDS;
155 0 : return NULL;
156 : }
157 : else
158 2 : return poDS;
159 : }
160 :
161 : /************************************************************************/
162 : /* DeleteDataSource() */
163 : /************************************************************************/
164 :
165 0 : OGRErr OGRXLSXDriver::DeleteDataSource( const char *pszName )
166 : {
167 0 : if (VSIUnlink( pszName ) == 0)
168 0 : return OGRERR_NONE;
169 : else
170 0 : return OGRERR_FAILURE;
171 : }
172 :
173 : /************************************************************************/
174 : /* TestCapability() */
175 : /************************************************************************/
176 :
177 4 : int OGRXLSXDriver::TestCapability( const char * pszCap )
178 :
179 : {
180 4 : if( EQUAL(pszCap,ODrCCreateDataSource) )
181 2 : return TRUE;
182 2 : else if( EQUAL(pszCap,ODrCDeleteDataSource) )
183 0 : return TRUE;
184 : else
185 2 : return FALSE;
186 : }
187 :
188 : /************************************************************************/
189 : /* RegisterOGRXLSX() */
190 : /************************************************************************/
191 :
192 389 : void RegisterOGRXLSX()
193 :
194 : {
195 389 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRXLSXDriver );
196 389 : }
197 :
|