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_phrase (lexer, "=VARLABEL"))
153 labelsource_varlabel = true;
154 labelsource_start = lex_ofs (lexer) - 3;
155 labelsource_end = lex_ofs (lexer) - 1;
157 else if (type == MRSET_MD && lex_match_id (lexer, "VALUE"))
159 if (!lex_force_match (lexer, T_EQUALS))
163 value_ofs = lex_ofs (lexer);
164 if (lex_is_number (lexer))
166 if (!lex_is_integer (lexer))
168 lex_error (lexer, _("Numeric VALUE must be an integer."));
171 value_destroy (&mrset->counted, mrset->width);
172 mrset->counted.f = lex_integer (lexer);
175 else if (lex_is_string (lexer))
180 s = recode_string (dict_get_encoding (dict), "UTF-8",
181 lex_tokcstr (lexer), -1);
184 /* Trim off trailing spaces, but don't trim the string until
185 it's empty because a width of 0 is a numeric type. */
186 while (width > 1 && s[width - 1] == ' ')
189 value_destroy (&mrset->counted, mrset->width);
190 value_init (&mrset->counted, width);
191 memcpy (mrset->counted.s, s, width);
192 mrset->width = width;
198 lex_error (lexer, NULL);
203 else if (type == MRSET_MD && lex_match_id (lexer, "CATEGORYLABELS"))
205 if (!lex_force_match (lexer, T_EQUALS))
208 if (lex_match_id (lexer, "VARLABELS"))
209 mrset->cat_source = MRSET_VARLABELS;
210 else if (lex_match_id (lexer, "COUNTEDVALUES"))
211 mrset->cat_source = MRSET_COUNTEDVALUES;
214 lex_error (lexer, NULL);
220 lex_error (lexer, NULL);
225 if (mrset->name == NULL)
227 lex_spec_missing (lexer, subcommand_name, "NAME");
230 else if (mrset->n_vars == 0)
232 lex_spec_missing (lexer, subcommand_name, "VARIABLES");
236 if (type == MRSET_MD)
238 /* Check that VALUE is specified and is valid for the VARIABLES. */
241 lex_spec_missing (lexer, subcommand_name, "VALUE");
244 else if (var_is_alpha (mrset->vars[0]))
246 if (mrset->width == 0)
248 lex_ofs_error (lexer, value_ofs, value_ofs,
249 _("MDGROUP subcommand for group %s specifies a "
250 "string VALUE, but the variables specified for "
251 "this group are numeric."),
256 const struct variable *shortest_var;
262 for (i = 0; i < mrset->n_vars; i++)
264 int width = var_get_width (mrset->vars[i]);
265 if (width < min_width)
267 shortest_var = mrset->vars[i];
271 if (mrset->width > min_width)
273 lex_ofs_error (lexer, value_ofs, value_ofs,
274 _("VALUE string on MDGROUP subcommand for "
275 "group %s is %d bytes long, but it must be "
276 "no longer than the narrowest variable in "
277 "the group, which is %s with a width of "
279 mrset->name, mrset->width,
280 var_get_name (shortest_var), min_width);
287 if (mrset->width != 0)
289 lex_ofs_error (lexer, value_ofs, value_ofs,
290 _("MDGROUP subcommand for group %s specifies a "
291 "string VALUE, but the variables specified for "
292 "this group are numeric."),
298 /* Implement LABELSOURCE=VARLABEL. */
299 if (labelsource_varlabel)
301 if (mrset->cat_source != MRSET_COUNTEDVALUES)
302 lex_ofs_msg (lexer, SW, labelsource_start, labelsource_end,
303 _("MDGROUP subcommand for group %s specifies "
304 "LABELSOURCE=VARLABEL but not "
305 "CATEGORYLABELS=COUNTEDVALUES. "
306 "Ignoring LABELSOURCE."),
308 else if (mrset->label)
310 msg (SW, _("MDGROUP subcommand for group %s specifies both "
311 "LABEL and LABELSOURCE, but only one of these "
312 "subcommands may be used at a time. "
313 "Ignoring LABELSOURCE."),
315 lex_ofs_msg (lexer, SN, label_start, label_end,
316 _("Here is the %s setting."), "LABEL");
317 lex_ofs_msg (lexer, SN, labelsource_start, labelsource_end,
318 _("Here is the %s setting."), "LABELSOURCE");
324 mrset->label_from_var_label = true;
325 for (i = 0; mrset->label == NULL && i < mrset->n_vars; i++)
327 const char *label = var_get_label (mrset->vars[i]);
330 mrset->label = xstrdup (label);
337 /* Warn if categories cannot be distinguished in output. */
338 if (mrset->cat_source == MRSET_VARLABELS)
340 struct stringi_map seen;
343 stringi_map_init (&seen);
344 for (i = 0; i < mrset->n_vars; i++)
346 const struct variable *var = mrset->vars[i];
347 const char *name = var_get_name (var);
348 const char *label = var_get_label (var);
351 const char *other_name = stringi_map_find (&seen, label);
353 if (other_name == NULL)
354 stringi_map_insert (&seen, label, name);
356 lex_ofs_msg (lexer, SW, vars_start, vars_end,
357 _("Variables %s and %s specified as part of "
358 "multiple dichotomy group %s have the same "
359 "variable label. Categories represented by "
360 "these variables will not be distinguishable "
362 other_name, name, mrset->name);
365 stringi_map_destroy (&seen);
369 struct stringi_map seen;
372 stringi_map_init (&seen);
373 for (i = 0; i < mrset->n_vars; i++)
375 const struct variable *var = mrset->vars[i];
376 const char *name = var_get_name (var);
377 const struct val_labs *val_labs;
381 value_clone (&value, &mrset->counted, mrset->width);
382 value_resize (&value, mrset->width, var_get_width (var));
384 val_labs = var_get_value_labels (var);
385 label = val_labs_find (val_labs, &value);
387 lex_ofs_msg (lexer, SW, vars_start, vars_end,
388 _("Variable %s specified as part of multiple "
389 "dichotomy group %s (which has "
390 "CATEGORYLABELS=COUNTEDVALUES) has no value "
391 "label for its counted value. This category "
392 "will not be distinguishable in output."),
396 const char *other_name = stringi_map_find (&seen, label);
398 if (other_name == NULL)
399 stringi_map_insert (&seen, label, name);
401 lex_ofs_msg (lexer, SW, vars_start, vars_end,
402 _("Variables %s and %s specified as part of "
403 "multiple dichotomy group %s (which has "
404 "CATEGORYLABELS=COUNTEDVALUES) have the same "
405 "value label for the group's counted "
406 "value. These categories will not be "
407 "distinguishable in output."),
408 other_name, name, mrset->name);
411 stringi_map_destroy (&seen);
416 /* Warn if categories cannot be distinguished in output. */
419 struct hmap_node hmap_node;
423 const char *var_name;
427 struct category *c, *next;
428 struct hmap categories;
431 hmap_init (&categories);
432 for (i = 0; i < mrset->n_vars; i++)
434 const struct variable *var = mrset->vars[i];
435 const char *name = var_get_name (var);
436 int width = var_get_width (var);
437 const struct val_labs *val_labs;
438 const struct val_lab *vl;
440 val_labs = var_get_value_labels (var);
441 for (vl = val_labs_first (val_labs); vl != NULL;
442 vl = val_labs_next (val_labs, vl))
444 const union value *value = val_lab_get_value (vl);
445 const char *label = val_lab_get_label (vl);
446 unsigned int hash = value_hash (value, width, 0);
448 HMAP_FOR_EACH_WITH_HASH (c, struct category, hmap_node,
451 if (width == c->width
452 && value_equal (value, &c->value, width))
454 if (!c->warned && utf8_strcasecmp (c->label, label))
456 char *s = data_out (value, var_get_encoding (var),
457 var_get_print_format (var),
458 settings_get_fmt_settings ());
460 lex_ofs_msg (lexer, SW, vars_start, vars_end,
461 _("Variables specified on MCGROUP should "
462 "have the same categories, but %s and "
463 "%s (and possibly others) in multiple "
464 "category group %s have different "
465 "value labels for value %s."),
466 c->var_name, name, mrset->name, s);
473 c = xmalloc (sizeof *c);
474 value_clone (&c->value, value, width);
479 hmap_insert (&categories, &c->hmap_node, hash);
485 HMAP_FOR_EACH_SAFE (c, next, struct category, hmap_node, &categories)
487 value_destroy (&c->value, c->width);
488 hmap_delete (&categories, &c->hmap_node);
491 hmap_destroy (&categories);
494 dict_add_mrset (dict, mrset);
498 mrset_destroy (mrset);
503 parse_mrset_names (struct lexer *lexer, struct dictionary *dict,
504 struct stringi_set *mrset_names)
506 if (!lex_force_match_phrase (lexer, "NAME="))
509 stringi_set_init (mrset_names);
510 if (lex_match (lexer, T_LBRACK))
512 while (!lex_match (lexer, T_RBRACK))
514 if (!lex_force_id (lexer))
516 if (dict_lookup_mrset (dict, lex_tokcstr (lexer)) == NULL)
518 lex_error (lexer, _("No multiple response set named %s."),
519 lex_tokcstr (lexer));
520 stringi_set_destroy (mrset_names);
523 stringi_set_insert (mrset_names, lex_tokcstr (lexer));
527 else if (lex_match (lexer, T_ALL))
529 size_t n_sets = dict_get_n_mrsets (dict);
532 for (i = 0; i < n_sets; i++)
533 stringi_set_insert (mrset_names, dict_get_mrset (dict, i)->name);
540 parse_delete (struct lexer *lexer, struct dictionary *dict)
542 const struct stringi_set_node *node;
543 struct stringi_set mrset_names;
546 if (!parse_mrset_names (lexer, dict, &mrset_names))
549 STRINGI_SET_FOR_EACH (name, node, &mrset_names)
550 dict_delete_mrset (dict, name);
551 stringi_set_destroy (&mrset_names);
557 parse_display (struct lexer *lexer, struct dictionary *dict)
559 struct stringi_set mrset_names_set;
560 if (!parse_mrset_names (lexer, dict, &mrset_names_set))
563 size_t n = stringi_set_count (&mrset_names_set);
566 if (dict_get_n_mrsets (dict) == 0)
567 lex_next_msg (lexer, SN, -1, -1,
568 _("The active dataset dictionary does not contain any "
569 "multiple response sets."));
570 stringi_set_destroy (&mrset_names_set);
574 struct pivot_table *table = pivot_table_create (
575 N_("Multiple Response Sets"));
577 pivot_dimension_create (
578 table, PIVOT_AXIS_COLUMN, N_("Attributes"),
579 N_("Label"), N_("Encoding"), N_("Counted Value"), N_("Member Variables"));
581 struct pivot_dimension *mrsets = pivot_dimension_create (
582 table, PIVOT_AXIS_ROW, N_("Name"));
583 mrsets->root->show_label = true;
585 char **mrset_names = stringi_set_get_sorted_array (&mrset_names_set);
586 for (size_t i = 0; i < n; i++)
588 const struct mrset *mrset = dict_lookup_mrset (dict, mrset_names[i]);
590 int row = pivot_category_create_leaf (
591 mrsets->root, pivot_value_new_user_text (mrset->name, -1));
593 if (mrset->label != NULL)
594 pivot_table_put2 (table, 0, row,
595 pivot_value_new_user_text (mrset->label, -1));
597 pivot_table_put2 (table, 1, row,
598 pivot_value_new_text (mrset->type == MRSET_MD
602 if (mrset->type == MRSET_MD)
603 pivot_table_put2 (table, 2, row,
604 pivot_value_new_value (
605 &mrset->counted, mrset->width,
606 &F_8_0, dict_get_encoding (dict)));
608 /* Variable names. */
609 struct string var_names = DS_EMPTY_INITIALIZER;
610 for (size_t j = 0; j < mrset->n_vars; j++)
611 ds_put_format (&var_names, "%s\n", var_get_name (mrset->vars[j]));
612 ds_chomp_byte (&var_names, '\n');
613 pivot_table_put2 (table, 3, row,
614 pivot_value_new_user_text_nocopy (
615 ds_steal_cstr (&var_names)));
618 stringi_set_destroy (&mrset_names_set);
620 pivot_table_submit (table);