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";
83 struct mrset *mrset = XZALLOC (struct mrset);
85 mrset->cat_source = MRSET_VARLABELS;
87 bool labelsource_varlabel = false;
88 bool has_value = false;
93 int labelsource_start = 0;
94 int labelsource_end = 0;
97 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
99 if (lex_match_id (lexer, "NAME"))
101 if (!lex_force_match (lexer, T_EQUALS) || !lex_force_id (lexer))
103 char *error = mrset_is_valid_name__ (lex_tokcstr (lexer),
104 dict_get_encoding (dict));
107 lex_error (lexer, "%s", error);
113 mrset->name = xstrdup (lex_tokcstr (lexer));
116 else if (lex_match_id (lexer, "VARIABLES"))
118 if (!lex_force_match (lexer, T_EQUALS))
122 vars_start = lex_ofs (lexer);
123 if (!parse_variables (lexer, dict, &mrset->vars, &mrset->n_vars,
124 PV_SAME_TYPE | PV_NO_SCRATCH))
126 vars_end = lex_ofs (lexer) - 1;
128 if (mrset->n_vars < 2)
130 lex_ofs_error (lexer, vars_start, vars_end,
131 _("VARIABLES specified only variable %s on %s, but "
132 "at least two variables are required."),
133 var_get_name (mrset->vars[0]), subcommand_name);
137 else if (lex_match_id (lexer, "LABEL"))
139 label_start = lex_ofs (lexer) - 1;
140 if (!lex_force_match (lexer, T_EQUALS) || !lex_force_string (lexer))
142 label_end = lex_ofs (lexer);
145 mrset->label = ss_xstrdup (lex_tokss (lexer));
148 else if (type == MRSET_MD && lex_match_id (lexer, "LABELSOURCE"))
150 if (!lex_force_match (lexer, T_EQUALS)
151 || !lex_force_match_id (lexer, "VARLABEL"))
154 labelsource_varlabel = true;
155 labelsource_start = lex_ofs (lexer) - 3;
156 labelsource_end = lex_ofs (lexer) - 1;
158 else if (type == MRSET_MD && lex_match_id (lexer, "VALUE"))
160 if (!lex_force_match (lexer, T_EQUALS))
164 value_ofs = lex_ofs (lexer);
165 if (lex_is_number (lexer))
167 if (!lex_is_integer (lexer))
169 lex_error (lexer, _("Numeric VALUE must be an integer."));
172 value_destroy (&mrset->counted, mrset->width);
173 mrset->counted.f = lex_integer (lexer);
176 else if (lex_is_string (lexer))
181 s = recode_string (dict_get_encoding (dict), "UTF-8",
182 lex_tokcstr (lexer), -1);
185 /* Trim off trailing spaces, but don't trim the string until
186 it's empty because a width of 0 is a numeric type. */
187 while (width > 1 && s[width - 1] == ' ')
190 value_destroy (&mrset->counted, mrset->width);
191 value_init (&mrset->counted, width);
192 memcpy (mrset->counted.s, s, width);
193 mrset->width = width;
199 lex_error (lexer, NULL);
204 else if (type == MRSET_MD && lex_match_id (lexer, "CATEGORYLABELS"))
206 if (!lex_force_match (lexer, T_EQUALS))
209 if (lex_match_id (lexer, "VARLABELS"))
210 mrset->cat_source = MRSET_VARLABELS;
211 else if (lex_match_id (lexer, "COUNTEDVALUES"))
212 mrset->cat_source = MRSET_COUNTEDVALUES;
215 lex_error (lexer, NULL);
221 lex_error (lexer, NULL);
226 if (mrset->name == NULL)
228 lex_spec_missing (lexer, subcommand_name, "NAME");
231 else if (mrset->n_vars == 0)
233 lex_spec_missing (lexer, subcommand_name, "VARIABLES");
237 if (type == MRSET_MD)
239 /* Check that VALUE is specified and is valid for the VARIABLES. */
242 lex_spec_missing (lexer, subcommand_name, "VALUE");
245 else if (var_is_alpha (mrset->vars[0]))
247 if (mrset->width == 0)
249 lex_ofs_error (lexer, value_ofs, value_ofs,
250 _("MDGROUP subcommand for group %s specifies a "
251 "string VALUE, but the variables specified for "
252 "this group are numeric."),
257 const struct variable *shortest_var;
263 for (i = 0; i < mrset->n_vars; i++)
265 int width = var_get_width (mrset->vars[i]);
266 if (width < min_width)
268 shortest_var = mrset->vars[i];
272 if (mrset->width > min_width)
274 lex_ofs_error (lexer, value_ofs, value_ofs,
275 _("VALUE string on MDGROUP subcommand for "
276 "group %s is %d bytes long, but it must be "
277 "no longer than the narrowest variable in "
278 "the group, which is %s with a width of "
280 mrset->name, mrset->width,
281 var_get_name (shortest_var), min_width);
288 if (mrset->width != 0)
290 lex_ofs_error (lexer, value_ofs, value_ofs,
291 _("MDGROUP subcommand for group %s specifies a "
292 "string VALUE, but the variables specified for "
293 "this group are numeric."),
299 /* Implement LABELSOURCE=VARLABEL. */
300 if (labelsource_varlabel)
302 if (mrset->cat_source != MRSET_COUNTEDVALUES)
303 lex_ofs_msg (lexer, SW, labelsource_start, labelsource_end,
304 _("MDGROUP subcommand for group %s specifies "
305 "LABELSOURCE=VARLABEL but not "
306 "CATEGORYLABELS=COUNTEDVALUES. "
307 "Ignoring LABELSOURCE."),
309 else if (mrset->label)
311 msg (SW, _("MDGROUP subcommand for group %s specifies both "
312 "LABEL and LABELSOURCE, but only one of these "
313 "subcommands may be used at a time. "
314 "Ignoring LABELSOURCE."),
316 lex_ofs_msg (lexer, SN, label_start, label_end,
317 _("Here is the %s setting."), "LABEL");
318 lex_ofs_msg (lexer, SN, labelsource_start, labelsource_end,
319 _("Here is the %s setting."), "LABELSOURCE");
325 mrset->label_from_var_label = true;
326 for (i = 0; mrset->label == NULL && i < mrset->n_vars; i++)
328 const char *label = var_get_label (mrset->vars[i]);
331 mrset->label = xstrdup (label);
338 /* Warn if categories cannot be distinguished in output. */
339 if (mrset->cat_source == MRSET_VARLABELS)
341 struct stringi_map seen;
344 stringi_map_init (&seen);
345 for (i = 0; i < mrset->n_vars; i++)
347 const struct variable *var = mrset->vars[i];
348 const char *name = var_get_name (var);
349 const char *label = var_get_label (var);
352 const char *other_name = stringi_map_find (&seen, label);
354 if (other_name == NULL)
355 stringi_map_insert (&seen, label, name);
357 lex_ofs_msg (lexer, SW, vars_start, vars_end,
358 _("Variables %s and %s specified as part of "
359 "multiple dichotomy group %s have the same "
360 "variable label. Categories represented by "
361 "these variables will not be distinguishable "
363 other_name, name, mrset->name);
366 stringi_map_destroy (&seen);
370 struct stringi_map seen;
373 stringi_map_init (&seen);
374 for (i = 0; i < mrset->n_vars; i++)
376 const struct variable *var = mrset->vars[i];
377 const char *name = var_get_name (var);
378 const struct val_labs *val_labs;
382 value_clone (&value, &mrset->counted, mrset->width);
383 value_resize (&value, mrset->width, var_get_width (var));
385 val_labs = var_get_value_labels (var);
386 label = val_labs_find (val_labs, &value);
388 lex_ofs_msg (lexer, SW, vars_start, vars_end,
389 _("Variable %s specified as part of multiple "
390 "dichotomy group %s (which has "
391 "CATEGORYLABELS=COUNTEDVALUES) has no value "
392 "label for its counted value. This category "
393 "will not be distinguishable in output."),
397 const char *other_name = stringi_map_find (&seen, label);
399 if (other_name == NULL)
400 stringi_map_insert (&seen, label, name);
402 lex_ofs_msg (lexer, SW, vars_start, vars_end,
403 _("Variables %s and %s specified as part of "
404 "multiple dichotomy group %s (which has "
405 "CATEGORYLABELS=COUNTEDVALUES) have the same "
406 "value label for the group's counted "
407 "value. These categories will not be "
408 "distinguishable in output."),
409 other_name, name, mrset->name);
412 stringi_map_destroy (&seen);
417 /* Warn if categories cannot be distinguished in output. */
420 struct hmap_node hmap_node;
424 const char *var_name;
428 struct category *c, *next;
429 struct hmap categories;
432 hmap_init (&categories);
433 for (i = 0; i < mrset->n_vars; i++)
435 const struct variable *var = mrset->vars[i];
436 const char *name = var_get_name (var);
437 int width = var_get_width (var);
438 const struct val_labs *val_labs;
439 const struct val_lab *vl;
441 val_labs = var_get_value_labels (var);
442 for (vl = val_labs_first (val_labs); vl != NULL;
443 vl = val_labs_next (val_labs, vl))
445 const union value *value = val_lab_get_value (vl);
446 const char *label = val_lab_get_label (vl);
447 unsigned int hash = value_hash (value, width, 0);
449 HMAP_FOR_EACH_WITH_HASH (c, struct category, hmap_node,
452 if (width == c->width
453 && value_equal (value, &c->value, width))
455 if (!c->warned && utf8_strcasecmp (c->label, label))
457 char *s = data_out (value, var_get_encoding (var),
458 var_get_print_format (var),
459 settings_get_fmt_settings ());
461 lex_ofs_msg (lexer, SW, vars_start, vars_end,
462 _("Variables specified on MCGROUP should "
463 "have the same categories, but %s and "
464 "%s (and possibly others) in multiple "
465 "category group %s have different "
466 "value labels for value %s."),
467 c->var_name, name, mrset->name, s);
474 c = xmalloc (sizeof *c);
475 value_clone (&c->value, value, width);
480 hmap_insert (&categories, &c->hmap_node, hash);
486 HMAP_FOR_EACH_SAFE (c, next, struct category, hmap_node, &categories)
488 value_destroy (&c->value, c->width);
489 hmap_delete (&categories, &c->hmap_node);
492 hmap_destroy (&categories);
495 dict_add_mrset (dict, mrset);
499 mrset_destroy (mrset);
504 parse_mrset_names (struct lexer *lexer, struct dictionary *dict,
505 struct stringi_set *mrset_names)
507 if (!lex_force_match_id (lexer, "NAME")
508 || !lex_force_match (lexer, T_EQUALS))
511 stringi_set_init (mrset_names);
512 if (lex_match (lexer, T_LBRACK))
514 while (!lex_match (lexer, T_RBRACK))
516 if (!lex_force_id (lexer))
518 if (dict_lookup_mrset (dict, lex_tokcstr (lexer)) == NULL)
520 lex_error (lexer, _("No multiple response set named %s."),
521 lex_tokcstr (lexer));
522 stringi_set_destroy (mrset_names);
525 stringi_set_insert (mrset_names, lex_tokcstr (lexer));
529 else if (lex_match (lexer, T_ALL))
531 size_t n_sets = dict_get_n_mrsets (dict);
534 for (i = 0; i < n_sets; i++)
535 stringi_set_insert (mrset_names, dict_get_mrset (dict, i)->name);
542 parse_delete (struct lexer *lexer, struct dictionary *dict)
544 const struct stringi_set_node *node;
545 struct stringi_set mrset_names;
548 if (!parse_mrset_names (lexer, dict, &mrset_names))
551 STRINGI_SET_FOR_EACH (name, node, &mrset_names)
552 dict_delete_mrset (dict, name);
553 stringi_set_destroy (&mrset_names);
559 parse_display (struct lexer *lexer, struct dictionary *dict)
561 struct stringi_set mrset_names_set;
562 if (!parse_mrset_names (lexer, dict, &mrset_names_set))
565 size_t n = stringi_set_count (&mrset_names_set);
568 if (dict_get_n_mrsets (dict) == 0)
569 lex_next_msg (lexer, SN, -1, -1,
570 _("The active dataset dictionary does not contain any "
571 "multiple response sets."));
572 stringi_set_destroy (&mrset_names_set);
576 struct pivot_table *table = pivot_table_create (
577 N_("Multiple Response Sets"));
579 pivot_dimension_create (
580 table, PIVOT_AXIS_COLUMN, N_("Attributes"),
581 N_("Label"), N_("Encoding"), N_("Counted Value"), N_("Member Variables"));
583 struct pivot_dimension *mrsets = pivot_dimension_create (
584 table, PIVOT_AXIS_ROW, N_("Name"));
585 mrsets->root->show_label = true;
587 char **mrset_names = stringi_set_get_sorted_array (&mrset_names_set);
588 for (size_t i = 0; i < n; i++)
590 const struct mrset *mrset = dict_lookup_mrset (dict, mrset_names[i]);
592 int row = pivot_category_create_leaf (
593 mrsets->root, pivot_value_new_user_text (mrset->name, -1));
595 if (mrset->label != NULL)
596 pivot_table_put2 (table, 0, row,
597 pivot_value_new_user_text (mrset->label, -1));
599 pivot_table_put2 (table, 1, row,
600 pivot_value_new_text (mrset->type == MRSET_MD
604 if (mrset->type == MRSET_MD)
605 pivot_table_put2 (table, 2, row,
606 pivot_value_new_value (
607 &mrset->counted, mrset->width,
608 &F_8_0, dict_get_encoding (dict)));
610 /* Variable names. */
611 struct string var_names = DS_EMPTY_INITIALIZER;
612 for (size_t j = 0; j < mrset->n_vars; j++)
613 ds_put_format (&var_names, "%s\n", var_get_name (mrset->vars[j]));
614 ds_chomp_byte (&var_names, '\n');
615 pivot_table_put2 (table, 3, row,
616 pivot_value_new_user_text_nocopy (
617 ds_steal_cstr (&var_names)));
620 stringi_set_destroy (&mrset_names_set);
622 pivot_table_submit (table);