Work on getting rid of trns_chain_finalize().
[pspp] / src / data / spreadsheet-reader.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "spreadsheet-reader.h"
20
21 #include <libpspp/assertion.h>
22 #include "gnumeric-reader.h"
23 #include "ods-reader.h"
24
25 #include <libpspp/str.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <gl/xalloc.h>
29 #include <gl/c-xvasprintf.h>
30 #include <stdlib.h>
31
32 #ifdef ODF_READ_SUPPORT
33 const bool ODF_READING_SUPPORTED = true;
34 #else
35 const bool ODF_READING_SUPPORTED = false;
36 #endif
37
38 #ifdef GNM_READ_SUPPORT
39 const bool GNM_READING_SUPPORTED = true;
40 #else
41 const bool GNM_READING_SUPPORTED = false;
42 #endif
43
44 void 
45 spreadsheet_destroy (struct spreadsheet *s)
46 {
47   switch (s->type)
48     {
49     case SPREADSHEET_ODS:
50       assert (ODF_READING_SUPPORTED);
51       ods_destroy (s);
52       break;
53
54     case SPREADSHEET_GNUMERIC:
55       assert (GNM_READING_SUPPORTED);
56       gnumeric_destroy (s);
57       break;
58     default:
59       NOT_REACHED ();
60       break;
61     }
62 }
63
64
65 struct casereader * 
66 spreadsheet_make_reader (struct spreadsheet *s,
67                          const struct spreadsheet_read_options *opts)
68 {
69   if (ODF_READING_SUPPORTED)
70     if ( s->type == SPREADSHEET_ODS)
71       return ods_make_reader (s, opts);
72
73   if (GNM_READING_SUPPORTED)
74     if ( s->type == SPREADSHEET_GNUMERIC)
75       return gnumeric_make_reader (s, opts);
76
77   return NULL;
78 }
79
80 const char * 
81 spreadsheet_get_sheet_name (struct spreadsheet *s, int n)
82 {
83   if (ODF_READING_SUPPORTED)
84     if ( s->type == SPREADSHEET_ODS)
85       return ods_get_sheet_name (s, n);
86
87   if (GNM_READING_SUPPORTED)
88     if ( s->type == SPREADSHEET_GNUMERIC)
89       return gnumeric_get_sheet_name (s, n);
90
91   return NULL;
92 }
93
94
95 char * 
96 spreadsheet_get_sheet_range (struct spreadsheet *s, int n)
97 {
98   if (ODF_READING_SUPPORTED)
99     if ( s->type == SPREADSHEET_ODS)
100       return ods_get_sheet_range (s, n);
101
102   if (GNM_READING_SUPPORTED)
103     if ( s->type == SPREADSHEET_GNUMERIC)
104       return gnumeric_get_sheet_range (s, n);
105
106   return NULL;
107 }
108
109
110 #define RADIX 26
111
112 static void
113 reverse (char *s, int len)
114 {
115   int i;
116   for (i = 0; i < len / 2; ++i)
117     {
118       char tmp = s[len - i - 1];
119       s[len - i -1] = s[i];
120       s[i] = tmp;
121     }
122 }
123
124
125 /* Convert a string, which is an integer encoded in base26
126    IE, A=0, B=1, ... Z=25 to the integer it represents.
127    ... except that in this scheme, digits with an exponent
128    greater than 1 are implicitly incremented by 1, so
129    AA  = 0 + 1*26, AB = 1 + 1*26,
130    ABC = 2 + 2*26 + 1*26^2 ....
131 */
132 int
133 ps26_to_int (const char *str)
134 {
135   int i;
136   int multiplier = 1;
137   int result = 0;
138   int len = strlen (str);
139
140   for (i = len - 1 ; i >= 0; --i)
141     {
142       int mantissa = (str[i] - 'A');
143
144       assert (mantissa >= 0);
145       assert (mantissa < RADIX);
146
147       if (i != len - 1)
148         mantissa++;
149
150       result += mantissa * multiplier;
151       multiplier *= RADIX;
152     }
153
154   return result;
155 }
156
157 char *
158 int_to_ps26 (int i)
159 {
160   char *ret = NULL;
161
162   int lower = 0;
163   long long int base = RADIX;
164   int exp = 1;
165
166   assert (i >= 0);
167
168   while (i > lower + base - 1)
169     {
170       lower += base;
171       base *= RADIX;      
172       assert (base > 0);
173       exp++;
174     }
175
176   i -= lower;
177   i += base;
178
179   ret = xmalloc (exp + 1);
180
181   exp = 0;
182   do
183     {
184       ret[exp++] = (i % RADIX) + 'A';
185       i /= RADIX;
186     }
187   while (i > 1);
188
189   ret[exp]='\0';
190
191   reverse (ret, exp);
192   return ret;
193 }
194
195
196 char *
197 create_cell_ref (int col0, int row0)
198 {
199   char *cs0 ;
200   char *s ;
201
202   if ( col0 < 0) return NULL;
203   if ( row0 < 0) return NULL;
204
205   cs0 =  int_to_ps26 (col0);
206   s =  c_xasprintf ("%s%d", cs0, row0 + 1);
207
208   free (cs0);
209
210   return s;
211 }
212
213 char *
214 create_cell_range (int col0, int row0, int coli, int rowi)
215 {
216   char *s0 = create_cell_ref (col0, row0);
217   char *si = create_cell_ref (coli, rowi);
218
219   char *s =  c_xasprintf ("%s:%s", s0, si);
220
221   free (s0);
222   free (si);
223
224   return s;
225 }
226
227
228 /* Convert a cell reference in the form "A1:B2", to
229    integers.  A1 means column zero, row zero.
230    B1 means column 1 row 0. AA1 means column 26, row 0.
231 */
232 bool
233 convert_cell_ref (const char *ref,
234                   int *col0, int *row0,
235                   int *coli, int *rowi)
236 {
237   char startcol[5];
238   char stopcol [5];
239
240   int startrow;
241   int stoprow;
242
243   int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
244               startcol, &startrow,
245               stopcol, &stoprow);
246   if ( n != 4)
247     return false;
248
249   str_uppercase (startcol);
250   *col0 = ps26_to_int (startcol);
251   str_uppercase (stopcol);
252   *coli = ps26_to_int (stopcol);
253   *row0 = startrow - 1;
254   *rowi = stoprow - 1 ;
255
256   return true;
257 }
258