1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011, 2012 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 "data/data-out.h"
20 #include "data/dataset.h"
21 #include "data/dictionary.h"
22 #include "data/mrset.h"
23 #include "data/value-labels.h"
24 #include "data/variable.h"
25 #include "language/command.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/variable-parser.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/hmap.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/str.h"
33 #include "libpspp/stringi-map.h"
34 #include "libpspp/stringi-set.h"
35 #include "output/pivot-table.h"
37 #include "gl/xalloc.h"
40 #define N_(msgid) msgid
41 #define _(msgid) gettext (msgid)
43 static bool parse_group (struct lexer *, struct dictionary *, enum mrset_type);
44 static bool parse_delete (struct lexer *, struct dictionary *);
45 static bool parse_display (struct lexer *, struct dictionary *);
48 cmd_mrsets (struct lexer *lexer, struct dataset *ds)
50 struct dictionary *dict = dataset_dict (ds);
52 while (lex_match (lexer, T_SLASH))
56 if (lex_match_id (lexer, "MDGROUP"))
57 ok = parse_group (lexer, dict, MRSET_MD);
58 else if (lex_match_id (lexer, "MCGROUP"))
59 ok = parse_group (lexer, dict, MRSET_MC);
60 else if (lex_match_id (lexer, "DELETE"))
61 ok = parse_delete (lexer, dict);
62 else if (lex_match_id (lexer, "DISPLAY"))
63 ok = parse_display (lexer, dict);
67 lex_error (lexer, NULL);
78 parse_group (struct lexer *lexer, struct dictionary *dict,
81 const char *subcommand_name = type == MRSET_MD ? "MDGROUP" : "MCGROUP";
82 bool labelsource_varlabel;
85 struct mrset *mrset = XZALLOC (struct mrset);
87 mrset->cat_source = MRSET_VARLABELS;
89 labelsource_varlabel = false;
91 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
93 if (lex_match_id (lexer, "NAME"))
95 if (!lex_force_match (lexer, T_EQUALS) || !lex_force_id (lexer))
97 char *error = mrset_is_valid_name__ (lex_tokcstr (lexer),
98 dict_get_encoding (dict));
101 lex_error (lexer, "%s", error);
107 mrset->name = xstrdup (lex_tokcstr (lexer));
110 else if (lex_match_id (lexer, "VARIABLES"))
112 if (!lex_force_match (lexer, T_EQUALS))
116 if (!parse_variables (lexer, dict, &mrset->vars, &mrset->n_vars,
117 PV_SAME_TYPE | PV_NO_SCRATCH))
120 if (mrset->n_vars < 2)
122 msg (SE, _("VARIABLES specified only variable %s on %s, but "
123 "at least two variables are required."),
124 var_get_name (mrset->vars[0]), subcommand_name);
128 else if (lex_match_id (lexer, "LABEL"))
130 if (!lex_force_match (lexer, T_EQUALS) || !lex_force_string (lexer))
134 mrset->label = ss_xstrdup (lex_tokss (lexer));
137 else if (type == MRSET_MD && lex_match_id (lexer, "LABELSOURCE"))
139 if (!lex_force_match (lexer, T_EQUALS)
140 || !lex_force_match_id (lexer, "VARLABEL"))
143 labelsource_varlabel = true;
145 else if (type == MRSET_MD && lex_match_id (lexer, "VALUE"))
147 if (!lex_force_match (lexer, T_EQUALS))
151 if (lex_is_number (lexer))
153 if (!lex_is_integer (lexer))
155 lex_error (lexer, _("Numeric VALUE must be an integer."));
158 value_destroy (&mrset->counted, mrset->width);
159 mrset->counted.f = lex_integer (lexer);
162 else if (lex_is_string (lexer))
167 s = recode_string (dict_get_encoding (dict), "UTF-8",
168 lex_tokcstr (lexer), -1);
171 /* Trim off trailing spaces, but don't trim the string until
172 it's empty because a width of 0 is a numeric type. */
173 while (width > 1 && s[width - 1] == ' ')
176 value_destroy (&mrset->counted, mrset->width);
177 value_init (&mrset->counted, width);
178 memcpy (mrset->counted.s, s, width);
179 mrset->width = width;
185 lex_error (lexer, NULL);
190 else if (type == MRSET_MD && lex_match_id (lexer, "CATEGORYLABELS"))
192 if (!lex_force_match (lexer, T_EQUALS))
195 if (lex_match_id (lexer, "VARLABELS"))
196 mrset->cat_source = MRSET_VARLABELS;
197 else if (lex_match_id (lexer, "COUNTEDVALUES"))
198 mrset->cat_source = MRSET_COUNTEDVALUES;
201 lex_error (lexer, NULL);
207 lex_error (lexer, NULL);
212 if (mrset->name == NULL)
214 lex_spec_missing (lexer, subcommand_name, "NAME");
217 else if (mrset->n_vars == 0)
219 lex_spec_missing (lexer, subcommand_name, "VARIABLES");
223 if (type == MRSET_MD)
225 /* Check that VALUE is specified and is valid for the VARIABLES. */
228 lex_spec_missing (lexer, subcommand_name, "VALUE");
231 else if (var_is_alpha (mrset->vars[0]))
233 if (mrset->width == 0)
235 msg (SE, _("MDGROUP subcommand for group %s specifies a string "
236 "VALUE, but the variables specified for this group "
242 const struct variable *shortest_var;
248 for (i = 0; i < mrset->n_vars; i++)
250 int width = var_get_width (mrset->vars[i]);
251 if (width < min_width)
253 shortest_var = mrset->vars[i];
257 if (mrset->width > min_width)
259 msg (SE, _("VALUE string on MDGROUP subcommand for group "
260 "%s is %d bytes long, but it must be no longer "
261 "than the narrowest variable in the group, "
262 "which is %s with a width of %d bytes."),
263 mrset->name, mrset->width,
264 var_get_name (shortest_var), min_width);
271 if (mrset->width != 0)
273 msg (SE, _("MDGROUP subcommand for group %s specifies a string "
274 "VALUE, but the variables specified for this group "
281 /* Implement LABELSOURCE=VARLABEL. */
282 if (labelsource_varlabel)
284 if (mrset->cat_source != MRSET_COUNTEDVALUES)
285 msg (SW, _("MDGROUP subcommand for group %s specifies "
286 "LABELSOURCE=VARLABEL but not "
287 "CATEGORYLABELS=COUNTEDVALUES. "
288 "Ignoring LABELSOURCE."),
290 else if (mrset->label)
291 msg (SW, _("MDGROUP subcommand for group %s specifies both LABEL "
292 "and LABELSOURCE, but only one of these subcommands "
293 "may be used at a time. Ignoring LABELSOURCE."),
299 mrset->label_from_var_label = true;
300 for (i = 0; mrset->label == NULL && i < mrset->n_vars; i++)
302 const char *label = var_get_label (mrset->vars[i]);
305 mrset->label = xstrdup (label);
312 /* Warn if categories cannot be distinguished in output. */
313 if (mrset->cat_source == MRSET_VARLABELS)
315 struct stringi_map seen;
318 stringi_map_init (&seen);
319 for (i = 0; i < mrset->n_vars; i++)
321 const struct variable *var = mrset->vars[i];
322 const char *name = var_get_name (var);
323 const char *label = var_get_label (var);
326 const char *other_name = stringi_map_find (&seen, label);
328 if (other_name == NULL)
329 stringi_map_insert (&seen, label, name);
331 msg (SW, _("Variables %s and %s specified as part of "
332 "multiple dichotomy group %s have the same "
333 "variable label. Categories represented by "
334 "these variables will not be distinguishable "
336 other_name, name, mrset->name);
339 stringi_map_destroy (&seen);
343 struct stringi_map seen;
346 stringi_map_init (&seen);
347 for (i = 0; i < mrset->n_vars; i++)
349 const struct variable *var = mrset->vars[i];
350 const char *name = var_get_name (var);
351 const struct val_labs *val_labs;
355 value_clone (&value, &mrset->counted, mrset->width);
356 value_resize (&value, mrset->width, var_get_width (var));
358 val_labs = var_get_value_labels (var);
359 label = val_labs_find (val_labs, &value);
361 msg (SW, _("Variable %s specified as part of multiple "
362 "dichotomy group %s (which has "
363 "CATEGORYLABELS=COUNTEDVALUES) has no value label "
364 "for its counted value. This category will not "
365 "be distinguishable in output."),
369 const char *other_name = stringi_map_find (&seen, label);
371 if (other_name == NULL)
372 stringi_map_insert (&seen, label, name);
374 msg (SW, _("Variables %s and %s specified as part of "
375 "multiple dichotomy group %s (which has "
376 "CATEGORYLABELS=COUNTEDVALUES) have the same "
377 "value label for the group's counted "
378 "value. These categories will not be "
379 "distinguishable in output."),
380 other_name, name, mrset->name);
383 stringi_map_destroy (&seen);
388 /* Warn if categories cannot be distinguished in output. */
391 struct hmap_node hmap_node;
395 const char *var_name;
399 struct category *c, *next;
400 struct hmap categories;
403 hmap_init (&categories);
404 for (i = 0; i < mrset->n_vars; i++)
406 const struct variable *var = mrset->vars[i];
407 const char *name = var_get_name (var);
408 int width = var_get_width (var);
409 const struct val_labs *val_labs;
410 const struct val_lab *vl;
412 val_labs = var_get_value_labels (var);
413 for (vl = val_labs_first (val_labs); vl != NULL;
414 vl = val_labs_next (val_labs, vl))
416 const union value *value = val_lab_get_value (vl);
417 const char *label = val_lab_get_label (vl);
418 unsigned int hash = value_hash (value, width, 0);
420 HMAP_FOR_EACH_WITH_HASH (c, struct category, hmap_node,
423 if (width == c->width
424 && value_equal (value, &c->value, width))
426 if (!c->warned && utf8_strcasecmp (c->label, label))
428 char *s = data_out (value, var_get_encoding (var),
429 var_get_print_format (var),
430 settings_get_fmt_settings ());
432 msg (SW, _("Variables specified on MCGROUP should "
433 "have the same categories, but %s and %s "
434 "(and possibly others) in multiple "
435 "category group %s have different "
436 "value labels for value %s."),
437 c->var_name, name, mrset->name, s);
444 c = xmalloc (sizeof *c);
445 value_clone (&c->value, value, width);
450 hmap_insert (&categories, &c->hmap_node, hash);
456 HMAP_FOR_EACH_SAFE (c, next, struct category, hmap_node, &categories)
458 value_destroy (&c->value, c->width);
459 hmap_delete (&categories, &c->hmap_node);
462 hmap_destroy (&categories);
465 dict_add_mrset (dict, mrset);
469 mrset_destroy (mrset);
474 parse_mrset_names (struct lexer *lexer, struct dictionary *dict,
475 struct stringi_set *mrset_names)
477 if (!lex_force_match_id (lexer, "NAME")
478 || !lex_force_match (lexer, T_EQUALS))
481 stringi_set_init (mrset_names);
482 if (lex_match (lexer, T_LBRACK))
484 while (!lex_match (lexer, T_RBRACK))
486 if (!lex_force_id (lexer))
488 if (dict_lookup_mrset (dict, lex_tokcstr (lexer)) == NULL)
490 lex_error (lexer, _("No multiple response set named %s."),
491 lex_tokcstr (lexer));
492 stringi_set_destroy (mrset_names);
495 stringi_set_insert (mrset_names, lex_tokcstr (lexer));
499 else if (lex_match (lexer, T_ALL))
501 size_t n_sets = dict_get_n_mrsets (dict);
504 for (i = 0; i < n_sets; i++)
505 stringi_set_insert (mrset_names, dict_get_mrset (dict, i)->name);
512 parse_delete (struct lexer *lexer, struct dictionary *dict)
514 const struct stringi_set_node *node;
515 struct stringi_set mrset_names;
518 if (!parse_mrset_names (lexer, dict, &mrset_names))
521 STRINGI_SET_FOR_EACH (name, node, &mrset_names)
522 dict_delete_mrset (dict, name);
523 stringi_set_destroy (&mrset_names);
529 parse_display (struct lexer *lexer, struct dictionary *dict)
531 struct stringi_set mrset_names_set;
532 if (!parse_mrset_names (lexer, dict, &mrset_names_set))
535 size_t n = stringi_set_count (&mrset_names_set);
538 if (dict_get_n_mrsets (dict) == 0)
539 msg (SN, _("The active dataset dictionary does not contain any "
540 "multiple response sets."));
541 stringi_set_destroy (&mrset_names_set);
545 struct pivot_table *table = pivot_table_create (
546 N_("Multiple Response Sets"));
548 pivot_dimension_create (
549 table, PIVOT_AXIS_COLUMN, N_("Attributes"),
550 N_("Label"), N_("Encoding"), N_("Counted Value"), N_("Member Variables"));
552 struct pivot_dimension *mrsets = pivot_dimension_create (
553 table, PIVOT_AXIS_ROW, N_("Name"));
554 mrsets->root->show_label = true;
556 char **mrset_names = stringi_set_get_sorted_array (&mrset_names_set);
557 for (size_t i = 0; i < n; i++)
559 const struct mrset *mrset = dict_lookup_mrset (dict, mrset_names[i]);
561 int row = pivot_category_create_leaf (
562 mrsets->root, pivot_value_new_user_text (mrset->name, -1));
564 if (mrset->label != NULL)
565 pivot_table_put2 (table, 0, row,
566 pivot_value_new_user_text (mrset->label, -1));
568 pivot_table_put2 (table, 1, row,
569 pivot_value_new_text (mrset->type == MRSET_MD
573 if (mrset->type == MRSET_MD)
574 pivot_table_put2 (table, 2, row,
575 pivot_value_new_value (
576 &mrset->counted, mrset->width,
577 &F_8_0, dict_get_encoding (dict)));
579 /* Variable names. */
580 struct string var_names = DS_EMPTY_INITIALIZER;
581 for (size_t j = 0; j < mrset->n_vars; j++)
582 ds_put_format (&var_names, "%s\n", var_get_name (mrset->vars[j]));
583 ds_chomp_byte (&var_names, '\n');
584 pivot_table_put2 (table, 3, row,
585 pivot_value_new_user_text_nocopy (
586 ds_steal_cstr (&var_names)));
589 stringi_set_destroy (&mrset_names_set);
591 pivot_table_submit (table);