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