Independent Samples T-Test Dialog: Fix Crash
[pspp.git] / src / data / spreadsheet-reader.c
blob863df368b2502270caa654795057a213bbc4072b
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/>. */
17 #include <config.h>
19 #include "spreadsheet-reader.h"
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
25 #include "data/gnumeric-reader.h"
26 #include "data/ods-reader.h"
27 #include "libpspp/assertion.h"
28 #include "libpspp/str.h"
30 #include "gl/xalloc.h"
31 #include "gl/c-xvasprintf.h"
32 #include "gl/intprops.h"
34 struct spreadsheet *
35 spreadsheet_ref (struct spreadsheet *s)
37 s->ref_cnt++;
38 return s;
41 void
42 spreadsheet_unref (struct spreadsheet *s)
44 if (--s->ref_cnt == 0)
45 s->destroy (s);
49 struct casereader *
50 spreadsheet_make_reader (struct spreadsheet *s,
51 const struct spreadsheet_read_options *opts)
53 return s->make_reader (s, opts);
56 const char *
57 spreadsheet_get_sheet_name (struct spreadsheet *s, int n)
59 return s->get_sheet_name (s, n);
63 char *
64 spreadsheet_get_sheet_range (struct spreadsheet *s, int n)
66 return s->get_sheet_range (s, n);
69 int
70 spreadsheet_get_sheet_n_sheets (struct spreadsheet *s)
72 return s->get_sheet_n_sheets (s);
75 unsigned int
76 spreadsheet_get_sheet_n_rows (struct spreadsheet *s, int n)
78 return s->get_sheet_n_rows (s, n);
81 unsigned int
82 spreadsheet_get_sheet_n_columns (struct spreadsheet *s, int n)
84 return s->get_sheet_n_columns (s, n);
87 char *
88 spreadsheet_get_cell (struct spreadsheet *s, int n, int row, int column)
90 return s->get_sheet_cell (s, n, row, column);
94 char *
95 create_cell_ref (int col0, int row0)
97 if (col0 < 0 || row0 < 0)
98 return NULL;
100 char s[F26ADIC_STRLEN_MAX + INT_STRLEN_BOUND (row0) + 1];
101 str_format_26adic (col0 + 1, true, s, sizeof s);
102 size_t len = strlen (s);
103 snprintf (s + len, sizeof s - len, "%d", row0 + 1);
105 return xstrdup (s);
108 char *
109 create_cell_range (int col0, int row0, int coli, int rowi)
111 char *s0 = create_cell_ref (col0, row0);
112 char *si = create_cell_ref (coli, rowi);
114 char *s = c_xasprintf ("%s:%s", s0, si);
116 free (s0);
117 free (si);
119 return s;
123 /* Convert a cell reference in the form "A1:B2", to
124 integers. A1 means column zero, row zero.
125 B1 means column 1 row 0. AA1 means column 26, row 0.
127 bool
128 convert_cell_ref (const char *ref,
129 int *col0, int *row0,
130 int *coli, int *rowi)
132 char startcol[5];
133 char stopcol [5];
135 int startrow;
136 int stoprow;
138 int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
139 startcol, &startrow,
140 stopcol, &stoprow);
141 if (n != 4)
142 return false;
144 *col0 = str_parse_26adic (startcol);
145 *coli = str_parse_26adic (stopcol);
146 *row0 = startrow - 1;
147 *rowi = stoprow - 1 ;
149 return true;