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)
96 || !mrset_is_valid_name (lex_tokcstr (lexer),
97 dict_get_encoding (dict), true))
101 mrset->name = xstrdup (lex_tokcstr (lexer));
104 else if (lex_match_id (lexer, "VARIABLES"))
106 if (!lex_force_match (lexer, T_EQUALS))
110 if (!parse_variables (lexer, dict, &mrset->vars, &mrset->n_vars,
111 PV_SAME_TYPE | PV_NO_SCRATCH))
114 if (mrset->n_vars < 2)
116 msg (SE, _("VARIABLES specified only variable %s on %s, but "
117 "at least two variables are required."),
118 var_get_name (mrset->vars[0]), subcommand_name);
122 else if (lex_match_id (lexer, "LABEL"))
124 if (!lex_force_match (lexer, T_EQUALS) || !lex_force_string (lexer))
128 mrset->label = ss_xstrdup (lex_tokss (lexer));
131 else if (type == MRSET_MD && lex_match_id (lexer, "LABELSOURCE"))
133 if (!lex_force_match (lexer, T_EQUALS)
134 || !lex_force_match_id (lexer, "VARLABEL"))
137 labelsource_varlabel = true;
139 else if (type == MRSET_MD && lex_match_id (lexer, "VALUE"))
141 if (!lex_force_match (lexer, T_EQUALS))
145 if (lex_is_number (lexer))
147 if (!lex_is_integer (lexer))
149 msg (SE, _("Numeric VALUE must be an integer."));
152 value_destroy (&mrset->counted, mrset->width);
153 mrset->counted.f = lex_integer (lexer);
156 else if (lex_is_string (lexer))
161 s = recode_string (dict_get_encoding (dict), "UTF-8",
162 lex_tokcstr (lexer), -1);
165 /* Trim off trailing spaces, but don't trim the string until
166 it's empty because a width of 0 is a numeric type. */
167 while (width > 1 && s[width - 1] == ' ')
170 value_destroy (&mrset->counted, mrset->width);
171 value_init (&mrset->counted, width);
172 memcpy (mrset->counted.s, s, width);
173 mrset->width = width;
179 lex_error (lexer, NULL);
184 else if (type == MRSET_MD && lex_match_id (lexer, "CATEGORYLABELS"))
186 if (!lex_force_match (lexer, T_EQUALS))
189 if (lex_match_id (lexer, "VARLABELS"))
190 mrset->cat_source = MRSET_VARLABELS;
191 else if (lex_match_id (lexer, "COUNTEDVALUES"))
192 mrset->cat_source = MRSET_COUNTEDVALUES;
195 lex_error (lexer, NULL);
201 lex_error (lexer, NULL);
206 if (mrset->name == NULL)
208 lex_spec_missing (lexer, subcommand_name, "NAME");
211 else if (mrset->n_vars == 0)
213 lex_spec_missing (lexer, subcommand_name, "VARIABLES");
217 if (type == MRSET_MD)
219 /* Check that VALUE is specified and is valid for the VARIABLES. */
222 lex_spec_missing (lexer, subcommand_name, "VALUE");
225 else if (var_is_alpha (mrset->vars[0]))
227 if (mrset->width == 0)
229 msg (SE, _("MDGROUP subcommand for group %s specifies a string "
230 "VALUE, but the variables specified for this group "
236 const struct variable *shortest_var;
242 for (i = 0; i < mrset->n_vars; i++)
244 int width = var_get_width (mrset->vars[i]);
245 if (width < min_width)
247 shortest_var = mrset->vars[i];
251 if (mrset->width > min_width)
253 msg (SE, _("VALUE string on MDGROUP subcommand for group "
254 "%s is %d bytes long, but it must be no longer "
255 "than the narrowest variable in the group, "
256 "which is %s with a width of %d bytes."),
257 mrset->name, mrset->width,
258 var_get_name (shortest_var), min_width);
265 if (mrset->width != 0)
267 msg (SE, _("MDGROUP subcommand for group %s specifies a string "
268 "VALUE, but the variables specified for this group "
275 /* Implement LABELSOURCE=VARLABEL. */
276 if (labelsource_varlabel)
278 if (mrset->cat_source != MRSET_COUNTEDVALUES)
279 msg (SW, _("MDGROUP subcommand for group %s specifies "
280 "LABELSOURCE=VARLABEL but not "
281 "CATEGORYLABELS=COUNTEDVALUES. "
282 "Ignoring LABELSOURCE."),
284 else if (mrset->label)
285 msg (SW, _("MDGROUP subcommand for group %s specifies both LABEL "
286 "and LABELSOURCE, but only one of these subcommands "
287 "may be used at a time. Ignoring LABELSOURCE."),
293 mrset->label_from_var_label = true;
294 for (i = 0; mrset->label == NULL && i < mrset->n_vars; i++)
296 const char *label = var_get_label (mrset->vars[i]);
299 mrset->label = xstrdup (label);
306 /* Warn if categories cannot be distinguished in output. */
307 if (mrset->cat_source == MRSET_VARLABELS)
309 struct stringi_map seen;
312 stringi_map_init (&seen);
313 for (i = 0; i < mrset->n_vars; i++)
315 const struct variable *var = mrset->vars[i];
316 const char *name = var_get_name (var);
317 const char *label = var_get_label (var);
320 const char *other_name = stringi_map_find (&seen, label);
322 if (other_name == NULL)
323 stringi_map_insert (&seen, label, name);
325 msg (SW, _("Variables %s and %s specified as part of "
326 "multiple dichotomy group %s have the same "
327 "variable label. Categories represented by "
328 "these variables will not be distinguishable "
330 other_name, name, mrset->name);
333 stringi_map_destroy (&seen);
337 struct stringi_map seen;
340 stringi_map_init (&seen);
341 for (i = 0; i < mrset->n_vars; i++)
343 const struct variable *var = mrset->vars[i];
344 const char *name = var_get_name (var);
345 const struct val_labs *val_labs;
349 value_clone (&value, &mrset->counted, mrset->width);
350 value_resize (&value, mrset->width, var_get_width (var));
352 val_labs = var_get_value_labels (var);
353 label = val_labs_find (val_labs, &value);
355 msg (SW, _("Variable %s specified as part of multiple "
356 "dichotomy group %s (which has "
357 "CATEGORYLABELS=COUNTEDVALUES) has no value label "
358 "for its counted value. This category will not "
359 "be distinguishable in output."),
363 const char *other_name = stringi_map_find (&seen, label);
365 if (other_name == NULL)
366 stringi_map_insert (&seen, label, name);
368 msg (SW, _("Variables %s and %s specified as part of "
369 "multiple dichotomy group %s (which has "
370 "CATEGORYLABELS=COUNTEDVALUES) have the same "
371 "value label for the group's counted "
372 "value. These categories will not be "
373 "distinguishable in output."),
374 other_name, name, mrset->name);
377 stringi_map_destroy (&seen);
382 /* Warn if categories cannot be distinguished in output. */
385 struct hmap_node hmap_node;
389 const char *var_name;
393 struct category *c, *next;
394 struct hmap categories;
397 hmap_init (&categories);
398 for (i = 0; i < mrset->n_vars; i++)
400 const struct variable *var = mrset->vars[i];
401 const char *name = var_get_name (var);
402 int width = var_get_width (var);
403 const struct val_labs *val_labs;
404 const struct val_lab *vl;
406 val_labs = var_get_value_labels (var);
407 for (vl = val_labs_first (val_labs); vl != NULL;
408 vl = val_labs_next (val_labs, vl))
410 const union value *value = val_lab_get_value (vl);
411 const char *label = val_lab_get_label (vl);
412 unsigned int hash = value_hash (value, width, 0);
414 HMAP_FOR_EACH_WITH_HASH (c, struct category, hmap_node,
417 if (width == c->width
418 && value_equal (value, &c->value, width))
420 if (!c->warned && utf8_strcasecmp (c->label, label))
422 char *s = data_out (value, var_get_encoding (var),
423 var_get_print_format (var),
424 settings_get_fmt_settings ());
426 msg (SW, _("Variables specified on MCGROUP should "
427 "have the same categories, but %s and %s "
428 "(and possibly others) in multiple "
429 "category group %s have different "
430 "value labels for value %s."),
431 c->var_name, name, mrset->name, s);
438 c = xmalloc (sizeof *c);
439 value_clone (&c->value, value, width);
444 hmap_insert (&categories, &c->hmap_node, hash);
450 HMAP_FOR_EACH_SAFE (c, next, struct category, hmap_node, &categories)
452 value_destroy (&c->value, c->width);
453 hmap_delete (&categories, &c->hmap_node);
456 hmap_destroy (&categories);
459 dict_add_mrset (dict, mrset);
463 mrset_destroy (mrset);
468 parse_mrset_names (struct lexer *lexer, struct dictionary *dict,
469 struct stringi_set *mrset_names)
471 if (!lex_force_match_id (lexer, "NAME")
472 || !lex_force_match (lexer, T_EQUALS))
475 stringi_set_init (mrset_names);
476 if (lex_match (lexer, T_LBRACK))
478 while (!lex_match (lexer, T_RBRACK))
480 if (!lex_force_id (lexer))
482 if (dict_lookup_mrset (dict, lex_tokcstr (lexer)) == NULL)
484 msg (SE, _("No multiple response set named %s."),
485 lex_tokcstr (lexer));
486 stringi_set_destroy (mrset_names);
489 stringi_set_insert (mrset_names, lex_tokcstr (lexer));
493 else if (lex_match (lexer, T_ALL))
495 size_t n_sets = dict_get_n_mrsets (dict);
498 for (i = 0; i < n_sets; i++)
499 stringi_set_insert (mrset_names, dict_get_mrset (dict, i)->name);
506 parse_delete (struct lexer *lexer, struct dictionary *dict)
508 const struct stringi_set_node *node;
509 struct stringi_set mrset_names;
512 if (!parse_mrset_names (lexer, dict, &mrset_names))
515 STRINGI_SET_FOR_EACH (name, node, &mrset_names)
516 dict_delete_mrset (dict, name);
517 stringi_set_destroy (&mrset_names);
523 parse_display (struct lexer *lexer, struct dictionary *dict)
525 struct stringi_set mrset_names_set;
526 if (!parse_mrset_names (lexer, dict, &mrset_names_set))
529 size_t n = stringi_set_count (&mrset_names_set);
532 if (dict_get_n_mrsets (dict) == 0)
533 msg (SN, _("The active dataset dictionary does not contain any "
534 "multiple response sets."));
535 stringi_set_destroy (&mrset_names_set);
539 struct pivot_table *table = pivot_table_create (
540 N_("Multiple Response Sets"));
542 pivot_dimension_create (
543 table, PIVOT_AXIS_COLUMN, N_("Attributes"),
544 N_("Label"), N_("Encoding"), N_("Counted Value"), N_("Member Variables"));
546 struct pivot_dimension *mrsets = pivot_dimension_create (
547 table, PIVOT_AXIS_ROW, N_("Name"));
548 mrsets->root->show_label = true;
550 char **mrset_names = stringi_set_get_sorted_array (&mrset_names_set);
551 for (size_t i = 0; i < n; i++)
553 const struct mrset *mrset = dict_lookup_mrset (dict, mrset_names[i]);
555 int row = pivot_category_create_leaf (
556 mrsets->root, pivot_value_new_user_text (mrset->name, -1));
558 if (mrset->label != NULL)
559 pivot_table_put2 (table, 0, row,
560 pivot_value_new_user_text (mrset->label, -1));
562 pivot_table_put2 (table, 1, row,
563 pivot_value_new_text (mrset->type == MRSET_MD
567 if (mrset->type == MRSET_MD)
568 pivot_table_put2 (table, 2, row,
569 pivot_value_new_value (
570 &mrset->counted, mrset->width,
571 &F_8_0, dict_get_encoding (dict)));
573 /* Variable names. */
574 struct string var_names = DS_EMPTY_INITIALIZER;
575 for (size_t j = 0; j < mrset->n_vars; j++)
576 ds_put_format (&var_names, "%s\n", var_get_name (mrset->vars[j]));
577 ds_chomp_byte (&var_names, '\n');
578 pivot_table_put2 (table, 3, row,
579 pivot_value_new_user_text_nocopy (
580 ds_steal_cstr (&var_names)));
583 stringi_set_destroy (&mrset_names_set);
585 pivot_table_submit (table);