1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "spreadsheet-reader.h"
21 #include "gnumeric-reader.h"
22 #include "ods-reader.h"
24 #include <libpspp/str.h>
27 #include <gl/xalloc.h>
28 #include <gl/c-xvasprintf.h>
32 spreadsheet_open (const char *filename)
34 struct spreadsheet *ss = NULL;
36 ss = ods_probe (filename, true);
42 spreadsheet_close (UNUSED struct spreadsheet *spreadsheet)
48 spreadsheet_make_reader (struct spreadsheet *s, const struct spreadsheet_read_options *opts)
50 return ods_make_reader (s, opts);
54 spreadsheet_get_sheet_name (struct spreadsheet *s, int n)
56 return ods_get_sheet_name (s, n);
60 spreadsheet_get_sheet_range (struct spreadsheet *s, int n)
62 return ods_get_sheet_range (s, n);
69 reverse (char *s, int len)
72 for (i = 0; i < len / 2; ++i)
74 char tmp = s[len - i - 1];
81 /* Convert a string, which is an integer encoded in base26
82 IE, A=0, B=1, ... Z=25 to the integer it represents.
83 ... except that in this scheme, digits with an exponent
84 greater than 1 are implicitly incremented by 1, so
85 AA = 0 + 1*26, AB = 1 + 1*26,
86 ABC = 2 + 2*26 + 1*26^2 ....
89 ps26_to_int (const char *str)
94 int len = strlen (str);
96 for (i = len - 1 ; i >= 0; --i)
98 int mantissa = (str[i] - 'A');
100 assert (mantissa >= 0);
101 assert (mantissa < RADIX);
106 result += mantissa * multiplier;
119 long long int base = RADIX;
124 while (i > lower + base - 1)
135 ret = xmalloc (exp + 1);
140 ret[exp++] = (i % RADIX) + 'A';
152 create_cell_ref (int col0, int row0, int coli, int rowi)
158 if ( col0 < 0) return NULL;
159 if ( rowi < 0) return NULL;
160 if ( coli < 0) return NULL;
161 if ( row0 < 0) return NULL;
163 cs0 = int_to_ps26 (col0);
164 csi = int_to_ps26 (coli);
165 s = c_xasprintf ("%s%d:%s%d",
175 /* Convert a cell reference in the form "A1:B2", to
176 integers. A1 means column zero, row zero.
177 B1 means column 1 row 0. AA1 means column 26, row 0.
180 convert_cell_ref (const char *ref,
181 int *col0, int *row0,
182 int *coli, int *rowi)
190 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
196 str_uppercase (startcol);
197 *col0 = ps26_to_int (startcol);
198 str_uppercase (stopcol);
199 *coli = ps26_to_int (stopcol);
200 *row0 = startrow - 1;
201 *rowi = stoprow - 1 ;