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