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 <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>
32 #ifdef ODF_READ_SUPPORT
33 static const bool ODF_READING_SUPPORTED = true;
35 static const bool ODF_READING_SUPPORTED = false;
38 #ifdef GNM_READ_SUPPORT
39 static const bool GNM_READING_SUPPORTED = true;
41 static const bool GNM_READING_SUPPORTED = false;
45 spreadsheet_ref (struct spreadsheet *s)
51 spreadsheet_unref (struct spreadsheet *s)
56 assert (ODF_READING_SUPPORTED);
60 case SPREADSHEET_GNUMERIC:
61 assert (GNM_READING_SUPPORTED);
72 spreadsheet_make_reader (struct spreadsheet *s,
73 const struct spreadsheet_read_options *opts)
75 if (ODF_READING_SUPPORTED)
76 if ( s->type == SPREADSHEET_ODS)
77 return ods_make_reader (s, opts);
79 if (GNM_READING_SUPPORTED)
80 if ( s->type == SPREADSHEET_GNUMERIC)
81 return gnumeric_make_reader (s, opts);
87 spreadsheet_get_sheet_name (struct spreadsheet *s, int n)
89 if (ODF_READING_SUPPORTED)
90 if ( s->type == SPREADSHEET_ODS)
91 return ods_get_sheet_name (s, n);
93 if (GNM_READING_SUPPORTED)
94 if ( s->type == SPREADSHEET_GNUMERIC)
95 return gnumeric_get_sheet_name (s, n);
102 spreadsheet_get_sheet_range (struct spreadsheet *s, int n)
104 if (ODF_READING_SUPPORTED)
105 if ( s->type == SPREADSHEET_ODS)
106 return ods_get_sheet_range (s, n);
108 if (GNM_READING_SUPPORTED)
109 if ( s->type == SPREADSHEET_GNUMERIC)
110 return gnumeric_get_sheet_range (s, n);
119 reverse (char *s, int len)
122 for (i = 0; i < len / 2; ++i)
124 char tmp = s[len - i - 1];
125 s[len - i -1] = s[i];
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 ....
139 ps26_to_int (const char *str)
144 int len = strlen (str);
146 for (i = len - 1 ; i >= 0; --i)
148 int mantissa = (str[i] - 'A');
150 assert (mantissa >= 0);
151 assert (mantissa < RADIX);
156 result += mantissa * multiplier;
169 long long int base = RADIX;
174 while (i > lower + base - 1)
185 ret = xmalloc (exp + 1);
190 ret[exp++] = (i % RADIX) + 'A';
203 create_cell_ref (int col0, int row0)
208 if ( col0 < 0) return NULL;
209 if ( row0 < 0) return NULL;
211 cs0 = int_to_ps26 (col0);
212 s = c_xasprintf ("%s%d", cs0, row0 + 1);
220 create_cell_range (int col0, int row0, int coli, int rowi)
222 char *s0 = create_cell_ref (col0, row0);
223 char *si = create_cell_ref (coli, rowi);
225 char *s = c_xasprintf ("%s:%s", s0, si);
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.
239 convert_cell_ref (const char *ref,
240 int *col0, int *row0,
241 int *coli, int *rowi)
249 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
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 ;