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