1 : /******************************************************************************
2 : * $Id: ogrxlsdatasource.cpp 25307 2012-12-15 09:04:40Z rouault $
3 : *
4 : * Project: XLS Translator
5 : * Purpose: Implements OGRXLSDataSource class
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, 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 <freexl.h>
31 :
32 : #ifdef _WIN32
33 : # include <windows.h>
34 : #endif
35 :
36 : #include "ogr_xls.h"
37 : #include "cpl_conv.h"
38 : #include "cpl_string.h"
39 :
40 : CPL_CVSID("$Id: ogrxlsdatasource.cpp 25307 2012-12-15 09:04:40Z rouault $");
41 :
42 : /************************************************************************/
43 : /* OGRXLSDataSource() */
44 : /************************************************************************/
45 :
46 4 : OGRXLSDataSource::OGRXLSDataSource()
47 :
48 : {
49 4 : papoLayers = NULL;
50 4 : nLayers = 0;
51 :
52 4 : pszName = NULL;
53 :
54 4 : xlshandle = NULL;
55 4 : }
56 :
57 : /************************************************************************/
58 : /* ~OGRXLSDataSource() */
59 : /************************************************************************/
60 :
61 4 : OGRXLSDataSource::~OGRXLSDataSource()
62 :
63 : {
64 8 : for( int i = 0; i < nLayers; i++ )
65 4 : delete papoLayers[i];
66 4 : CPLFree( papoLayers );
67 :
68 4 : CPLFree( pszName );
69 :
70 4 : if (xlshandle)
71 3 : freexl_close(xlshandle);
72 4 : }
73 :
74 : /************************************************************************/
75 : /* TestCapability() */
76 : /************************************************************************/
77 :
78 2 : int OGRXLSDataSource::TestCapability( const char * pszCap )
79 :
80 : {
81 2 : return FALSE;
82 : }
83 :
84 : /************************************************************************/
85 : /* GetLayer() */
86 : /************************************************************************/
87 :
88 14 : OGRLayer *OGRXLSDataSource::GetLayer( int iLayer )
89 :
90 : {
91 14 : if( iLayer < 0 || iLayer >= nLayers )
92 2 : return NULL;
93 : else
94 12 : return papoLayers[iLayer];
95 : }
96 :
97 : /************************************************************************/
98 : /* Open() */
99 : /************************************************************************/
100 :
101 4 : int OGRXLSDataSource::Open( const char * pszFilename, int bUpdateIn)
102 :
103 : {
104 4 : if (bUpdateIn)
105 : {
106 0 : return FALSE;
107 : }
108 :
109 : #ifdef _WIN32
110 : if( CSLTestBoolean( CPLGetConfigOption( "GDAL_FILENAME_IS_UTF8", "YES" ) ) )
111 : pszName = CPLRecode( pszFilename, CPL_ENC_UTF8, CPLString().Printf( "CP%d", GetACP() ) );
112 : else
113 : pszName = CPLStrdup( pszFilename );
114 : #else
115 4 : pszName = CPLStrdup( pszFilename );
116 : #endif
117 :
118 : // --------------------------------------------------------------------
119 : // Does this appear to be a .xls file?
120 : // --------------------------------------------------------------------
121 :
122 : /* Open only for getting info. To get cell values, we have to use freexl_open */
123 4 : if (freexl_open_info (pszName, &xlshandle) != FREEXL_OK)
124 0 : return FALSE;
125 :
126 4 : unsigned int nSheets = 0;
127 4 : if (freexl_get_info (xlshandle, FREEXL_BIFF_SHEET_COUNT, &nSheets) != FREEXL_OK)
128 0 : return FALSE;
129 :
130 16 : for(int i=0; i<(int)nSheets; i++)
131 : {
132 12 : freexl_select_active_worksheet(xlshandle, i);
133 :
134 12 : const char* pszSheetname = NULL;
135 12 : if (freexl_get_worksheet_name(xlshandle, i, &pszSheetname) != FREEXL_OK)
136 0 : return FALSE;
137 :
138 12 : unsigned int nRows = 0;
139 12 : unsigned short nCols = 0;
140 12 : if (freexl_worksheet_dimensions(xlshandle, &nRows, &nCols) != FREEXL_OK)
141 0 : return FALSE;
142 :
143 : /* Skip empty sheets */
144 12 : if (nRows == 0)
145 8 : continue;
146 :
147 4 : papoLayers = (OGRLayer**) CPLRealloc(papoLayers, (nLayers + 1) * sizeof(OGRLayer*));
148 4 : papoLayers[nLayers ++] = new OGRXLSLayer(this, pszSheetname, i, (int)nRows, nCols);
149 : }
150 :
151 4 : freexl_close(xlshandle);
152 4 : xlshandle = NULL;
153 :
154 4 : return TRUE;
155 : }
156 :
157 : /************************************************************************/
158 : /* GetXLSHandle() */
159 : /************************************************************************/
160 :
161 36 : const void* OGRXLSDataSource::GetXLSHandle()
162 : {
163 36 : if (xlshandle)
164 33 : return xlshandle;
165 :
166 3 : if (freexl_open (pszName, &xlshandle) != FREEXL_OK)
167 0 : return NULL;
168 :
169 3 : return xlshandle;
170 : }
|