1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2010, 2011, 2013, 2020 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 <libpspp/assertion.h>
22 #include "gnumeric-reader.h"
23 #include "ods-reader.h"
25 #include <libpspp/str.h>
28 #include <gl/xalloc.h>
29 #include <gl/c-xvasprintf.h>
33 spreadsheet_ref (struct spreadsheet *s)
40 spreadsheet_unref (struct spreadsheet *s)
42 if (--s->ref_cnt == 0)
48 spreadsheet_make_reader (struct spreadsheet *s,
49 const struct spreadsheet_read_options *opts)
51 return s->make_reader (s, opts);
55 spreadsheet_get_sheet_name (struct spreadsheet *s, int n)
57 return s->get_sheet_name (s, n);
62 spreadsheet_get_sheet_range (struct spreadsheet *s, int n)
64 return s->get_sheet_range (s, n);
68 spreadsheet_get_sheet_n_sheets (struct spreadsheet *s)
70 return s->get_sheet_n_sheets (s);
74 spreadsheet_get_sheet_n_rows (struct spreadsheet *s, int n)
76 return s->get_sheet_n_rows (s, n);
80 spreadsheet_get_sheet_n_columns (struct spreadsheet *s, int n)
82 return s->get_sheet_n_columns (s, n);
86 spreadsheet_get_cell (struct spreadsheet *s, int n, int row, int column)
88 return s->get_sheet_cell (s, n, row, column);
95 reverse (char *s, int len)
98 for (i = 0; i < len / 2; ++i)
100 char tmp = s[len - i - 1];
101 s[len - i -1] = s[i];
107 /* Convert a string, which is an integer encoded in base26
108 IE, A=0, B=1, ... Z=25 to the integer it represents.
109 ... except that in this scheme, digits with an exponent
110 greater than 1 are implicitly incremented by 1, so
111 AA = 0 + 1*26, AB = 1 + 1*26,
112 ABC = 2 + 2*26 + 1*26^2 ....
113 On error, this function returns -1
116 ps26_to_int (const char *str)
121 int len = strlen (str);
123 for (i = len - 1 ; i >= 0; --i)
126 if (c < 'A' || c > 'Z')
128 int mantissa = (c - 'A');
130 assert (mantissa >= 0);
131 assert (mantissa < RADIX);
136 result += mantissa * multiplier;
143 /* Convert an integer, which must be non-negative,
145 The caller must free the return value when no longer required. */
152 long long int base = RADIX;
158 while (i > lower + base - 1)
169 ret = xmalloc (exp + 1);
174 ret[exp++] = (i % RADIX) + 'A';
187 create_cell_ref (int col0, int row0)
192 if (col0 < 0) return NULL;
193 if (row0 < 0) return NULL;
195 cs0 = int_to_ps26 (col0);
196 s = c_xasprintf ("%s%d", cs0, row0 + 1);
204 create_cell_range (int col0, int row0, int coli, int rowi)
206 char *s0 = create_cell_ref (col0, row0);
207 char *si = create_cell_ref (coli, rowi);
209 char *s = c_xasprintf ("%s:%s", s0, si);
218 /* Convert a cell reference in the form "A1:B2", to
219 integers. A1 means column zero, row zero.
220 B1 means column 1 row 0. AA1 means column 26, row 0.
223 convert_cell_ref (const char *ref,
224 int *col0, int *row0,
225 int *coli, int *rowi)
233 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
239 str_uppercase (startcol);
240 *col0 = ps26_to_int (startcol);
241 str_uppercase (stopcol);
242 *coli = ps26_to_int (stopcol);
243 *row0 = startrow - 1;
244 *rowi = stoprow - 1 ;