work on missing value handling
[pspp] / tests / data / spreadsheet-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2020 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 #include "progname.h"
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <getopt.h>
24 #include <string.h>
25
26 #include <data/spreadsheet-reader.h>
27 #include <data/gnumeric-reader.h>
28 #include <data/ods-reader.h>
29
30 enum OPT
31   {
32    OPT_REFCHECK = 0x100,
33    OPT_REVERSE,
34    OPT_SHEET,
35    OPT_METADATA
36   };
37
38 static const struct option long_opts[] =
39   {
40    {"refcheck", no_argument, NULL, OPT_REFCHECK},
41    {"reverse", no_argument, NULL, OPT_REVERSE},
42    {"sheet", required_argument, NULL, OPT_SHEET},
43    {"metadata", no_argument, NULL, OPT_METADATA},
44    {0, 0, 0, 0}
45   };
46
47 int
48 main (int argc, char **argv)
49 {
50   set_program_name (argv[0]);
51
52   bool refcheck = false;
53   bool reverse = false;
54   int sheet = 0;
55   bool get_n_sheets = false;
56   int opt;
57   while ((opt = getopt_long (argc, argv, "", long_opts, NULL)) != -1)
58     {
59       switch (opt)
60         {
61         case OPT_METADATA:
62           get_n_sheets = true;
63           break;
64         case OPT_REFCHECK:
65           refcheck = true;
66           break;
67         case OPT_REVERSE:
68           reverse = true;
69           break;
70         case OPT_SHEET:
71           sheet = atoi (optarg);
72           break;
73         default: /* '?' */
74           fprintf (stderr, "Usage: spreadsheet-test [opts] file\n");
75           exit (EXIT_FAILURE);
76         }
77     }
78
79   if (argc < optind)
80     {
81       fprintf (stderr, "Usage: spreadsheet-test [-s n] file\n");
82       exit (EXIT_FAILURE);
83     }
84
85   struct spreadsheet *ss = NULL;
86
87   char *ext = strrchr (argv[optind], '.');
88   if (ext == NULL)
89     return 1;
90   if (0 == strcmp (ext, ".ods"))
91     ss = ods_probe (argv[optind], true);
92   else if (0 == strcmp (ext, ".gnumeric"))
93     ss = gnumeric_probe (argv[optind], true);
94
95   if (ss == NULL)
96     return 1;
97
98   if (get_n_sheets)
99     {
100       int n_sheets = spreadsheet_get_sheet_n_sheets (ss);
101       printf ("Number of sheets: %d\n", n_sheets);
102       goto end;
103     }
104   int rows = spreadsheet_get_sheet_n_rows (ss, sheet);
105   int columns = spreadsheet_get_sheet_n_columns (ss, sheet);
106
107   printf ("Rows %d; Columns %d\n", rows, columns);
108   for (int r_ = 0; r_ < rows; r_++)
109     {
110       int r = reverse ? (rows - r_ - 1) : r_;
111       for (int c_ = 0; c_ < columns; c_++)
112         {
113           int c = reverse ? (columns - c_ - 1) : c_ ;
114           char *s = spreadsheet_get_cell (ss, sheet, r, c);
115           if (refcheck)
116             {
117               int row, col;
118               sscanf (s, "%d:%d", &row, &col);
119               assert (row == r);
120               assert (col == c);
121             }
122           else
123             {
124               fputs (s ? s : "", stdout);
125               if (c_ < columns - 1)
126                 putchar ('\t');
127             }
128
129
130           free (s);
131         }
132       if (!refcheck)
133         {
134           putchar ('\n');
135         }
136     }
137
138   rows = spreadsheet_get_sheet_n_rows (ss, sheet);
139   columns = spreadsheet_get_sheet_n_columns (ss, sheet);
140
141  end:
142   spreadsheet_unref (ss);
143
144   return 0;
145 }