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