Dont try to build in gnumeric/odf reader if the libraries are not installed
[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
33 void 
34 spreadsheet_destroy (struct spreadsheet *s)
35 {
36   switch (s->type)
37     {
38 #ifdef ODF_READ_SUPPORT
39     case SPREADSHEET_ODS:
40       ods_destroy (s);
41       break;
42 #endif
43 #ifdef GNM_READ_SUPPORT
44     case SPREADSHEET_GNUMERIC:
45       gnumeric_destroy (s);
46       break;
47 #endif
48     default:
49       NOT_REACHED ();
50       break;
51     }
52 }
53
54
55 struct casereader * 
56 spreadsheet_make_reader (struct spreadsheet *s, const struct spreadsheet_read_options *opts)
57 {
58 #ifdef ODS_READ_SUPPORT
59   if ( s->type == SPREADSHEET_ODS)
60     return ods_make_reader (s, opts);
61 #endif
62 #ifdef GNM_READ_SUPPORT
63   if ( s->type == SPREADSHEET_GNUMERIC)
64     return gnumeric_make_reader (s, opts);
65 #endif
66
67   return NULL;
68 }
69
70 const char * 
71 spreadsheet_get_sheet_name (struct spreadsheet *s, int n)
72 {
73 #ifdef ODF_READ_SUPPORT
74   if ( s->type == SPREADSHEET_ODS)
75     return ods_get_sheet_name (s, n);
76 #endif
77 #ifdef GNM_READ_SUPPORT
78   if ( s->type == SPREADSHEET_GNUMERIC)
79     return gnumeric_get_sheet_name (s, n);
80 #endif
81
82   return NULL;
83 }
84
85 char * 
86 spreadsheet_get_sheet_range (struct spreadsheet *s, int n)
87 {
88 #ifdef ODF_READ_SUPPORT
89   if ( s->type == SPREADSHEET_ODS)
90     return ods_get_sheet_range (s, n);
91 #endif
92
93 #ifdef GNM_READ_SUPPORT
94   if ( s->type == SPREADSHEET_GNUMERIC)
95     return gnumeric_get_sheet_range (s, n);
96 #endif
97
98   return NULL;
99 }
100
101
102 #define RADIX 26
103
104 static void
105 reverse (char *s, int len)
106 {
107   int i;
108   for (i = 0; i < len / 2; ++i)
109     {
110       char tmp = s[len - i - 1];
111       s[len - i -1] = s[i];
112       s[i] = tmp;
113     }
114 }
115
116
117 /* Convert a string, which is an integer encoded in base26
118    IE, A=0, B=1, ... Z=25 to the integer it represents.
119    ... except that in this scheme, digits with an exponent
120    greater than 1 are implicitly incremented by 1, so
121    AA  = 0 + 1*26, AB = 1 + 1*26,
122    ABC = 2 + 2*26 + 1*26^2 ....
123 */
124 int
125 ps26_to_int (const char *str)
126 {
127   int i;
128   int multiplier = 1;
129   int result = 0;
130   int len = strlen (str);
131
132   for (i = len - 1 ; i >= 0; --i)
133     {
134       int mantissa = (str[i] - 'A');
135
136       assert (mantissa >= 0);
137       assert (mantissa < RADIX);
138
139       if (i != len - 1)
140         mantissa++;
141
142       result += mantissa * multiplier;
143       multiplier *= RADIX;
144     }
145
146   return result;
147 }
148
149 char *
150 int_to_ps26 (int i)
151 {
152   char *ret = NULL;
153
154   int lower = 0;
155   long long int base = RADIX;
156   int exp = 1;
157
158   assert (i >= 0);
159
160   while (i > lower + base - 1)
161     {
162       lower += base;
163       base *= RADIX;      
164       assert (base > 0);
165       exp++;
166     }
167
168   i -= lower;
169   i += base;
170
171   ret = xmalloc (exp + 1);
172
173   exp = 0;
174   do
175     {
176       ret[exp++] = (i % RADIX) + 'A';
177       i /= RADIX;
178     }
179   while (i > 1);
180
181   ret[exp]='\0';
182
183   reverse (ret, exp);
184   return ret;
185 }
186
187
188 char *
189 create_cell_ref (int col0, int row0)
190 {
191   char *cs0 ;
192   char *s ;
193
194   if ( col0 < 0) return NULL;
195   if ( row0 < 0) return NULL;
196
197   cs0 =  int_to_ps26 (col0);
198   s =  c_xasprintf ("%s%d", cs0, row0 + 1);
199
200   free (cs0);
201
202   return s;
203 }
204
205 char *
206 create_cell_range (int col0, int row0, int coli, int rowi)
207 {
208   char *s0 = create_cell_ref (col0, row0);
209   char *si = create_cell_ref (coli, rowi);
210
211   char *s =  c_xasprintf ("%s:%s", s0, si);
212
213   free (s0);
214   free (si);
215
216   return s;
217 }
218
219
220 /* Convert a cell reference in the form "A1:B2", to
221    integers.  A1 means column zero, row zero.
222    B1 means column 1 row 0. AA1 means column 26, row 0.
223 */
224 bool
225 convert_cell_ref (const char *ref,
226                   int *col0, int *row0,
227                   int *coli, int *rowi)
228 {
229   char startcol[5];
230   char stopcol [5];
231
232   int startrow;
233   int stoprow;
234
235   int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
236               startcol, &startrow,
237               stopcol, &stoprow);
238   if ( n != 4)
239     return false;
240
241   str_uppercase (startcol);
242   *col0 = ps26_to_int (startcol);
243   str_uppercase (stopcol);
244   *coli = ps26_to_int (stopcol);
245   *row0 = startrow - 1;
246   *rowi = stoprow - 1 ;
247
248   return true;
249 }
250