Change many %g format specifiers to %.*g with precision DBL_DIG + 1.
[pspp] / src / ui / gui / spreadsheet-test.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2013, 2014  Free Software Foundation
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
18 /* This program is useful for testing the spreadsheet readers */
19
20 #include <config.h>
21
22 #include <float.h>
23 #include <gtk/gtk.h>
24
25 #include "psppire-spreadsheet-model.h"
26
27 #include "data/gnumeric-reader.h"
28 #include "data/ods-reader.h"
29 #include "data/spreadsheet-reader.h"
30 #include "data/casereader.h"
31 #include "data/case.h"
32 #include "data/settings.h"
33 #include "libpspp/message.h"
34 #include "libpspp/i18n.h"
35
36 #include "gl/xalloc.h"
37
38
39 struct xxx
40 {
41   struct spreadsheet *sp;
42   GtkWidget *combo_box;
43 };
44
45
46
47 static void
48 on_clicked (GtkButton *button, struct xxx *stuff)
49 {
50   const struct caseproto *proto;
51   int nvals;
52   struct ccase *c;
53   gint x = gtk_combo_box_get_active (GTK_COMBO_BOX (stuff->combo_box));
54   struct casereader *reader ;
55   struct spreadsheet_read_options opts;
56
57   opts.sheet_index = -1;
58   opts.cell_range = spreadsheet_get_sheet_range (stuff->sp, x);
59   opts.sheet_name = CONST_CAST (char *,
60                                 spreadsheet_get_sheet_name (stuff->sp, x));
61   opts.read_names = TRUE;
62   opts.asw = -1;
63
64   reader = spreadsheet_make_reader (stuff->sp, &opts);
65
66   if (reader == NULL)
67     return;
68
69   proto = casereader_get_proto (reader);
70
71   nvals = caseproto_get_n_widths (proto);
72   
73   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
74     {
75       int i;
76
77       for (i = 0; i < nvals ; ++i)
78       {
79         const int width = caseproto_get_width (proto, i);
80         const union value *val = case_data_idx (c, i);
81         if (0 == width)
82           printf ("%.*g ", DBL_DIG + 1, val->f);
83         else
84           {
85             char *ss = xzalloc (width + 1);
86             memcpy (ss, value_str (val, width), width);
87             
88             printf ("%s ", ss);
89             free (ss);
90           }
91       }
92       printf ("\n");
93     }
94
95   casereader_destroy (reader);
96 }
97
98 static void 
99 print_msg (const struct msg *m, void *aux UNUSED)
100 {
101   fprintf (stderr, "%s\n", m->text);
102 }
103
104
105 int
106 main (int argc, char *argv[] )
107 {
108   GtkWidget *window;
109   GtkWidget *hbox;
110   GtkWidget *vbox;
111   GtkWidget *treeview;
112
113   GtkTreeModel *tm;
114   GtkWidget *button;
115   struct xxx stuff;
116
117   i18n_init ();
118   settings_init ();
119
120   gtk_init (&argc, &argv);
121     
122   if ( argc < 2)
123     g_error ("Usage: prog file\n");
124
125   msg_set_handler (print_msg, 0);
126
127   stuff.sp = NULL;
128
129   if (stuff.sp == NULL)
130     stuff.sp = gnumeric_probe (argv[1], false);
131
132   if (stuff.sp == NULL)
133     stuff.sp = ods_probe (argv[1], false);
134   
135   if (stuff.sp == NULL)
136     {
137       g_error ("%s is neither a gnumeric nor a ods file\n", argv[1]);
138       return 0;
139     }
140
141   tm = psppire_spreadsheet_model_new (stuff.sp);
142   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
143   hbox = gtk_hbox_new (FALSE, 5);
144   vbox = gtk_vbox_new (FALSE, 5);
145
146   button = gtk_button_new_with_label ("Test reader");
147   g_signal_connect (button, "clicked", G_CALLBACK (on_clicked), &stuff);
148    
149   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
150   
151   stuff.combo_box = gtk_combo_box_new();
152
153   {
154     GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
155     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (stuff.combo_box), renderer, TRUE);
156     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (stuff.combo_box), renderer,
157                                     "text", 0,
158                                     NULL);
159   }
160
161   gtk_combo_box_set_model (GTK_COMBO_BOX (stuff.combo_box), tm);
162
163   gtk_combo_box_set_active (GTK_COMBO_BOX (stuff.combo_box), 0);
164
165   treeview = gtk_tree_view_new_with_model (tm);
166
167   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
168                                                0, "sheet name",
169                                                gtk_cell_renderer_text_new (),
170                                                "text", 0,
171                                                NULL);
172
173
174   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
175                                                1, "range",
176                                                gtk_cell_renderer_text_new (),
177                                                "text", 1,
178                                                NULL);
179
180
181   gtk_box_pack_start (GTK_BOX (hbox), treeview, TRUE, TRUE, 5);
182
183   gtk_box_pack_start (GTK_BOX (vbox), stuff.combo_box, FALSE, FALSE, 5);
184   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 5);
185   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 5);
186
187   gtk_container_add (GTK_CONTAINER (window), hbox);
188
189   g_signal_connect (window, "destroy", gtk_main_quit, 0);
190
191   gtk_widget_show_all (window);
192
193   gtk_main ();
194
195   spreadsheet_destroy (stuff.sp);
196
197   return 0;
198 }