1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2018 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 "output/select.h"
23 #include "libpspp/assertion.h"
24 #include "libpspp/bit-vector.h"
25 #include "libpspp/message.h"
27 #include "gl/c-ctype.h"
28 #include "gl/xalloc.h"
31 output_item_class_to_string (enum output_item_class class)
35 #define OUTPUT_CLASS(ENUM, NAME) case OUTPUT_CLASS_##ENUM: return NAME;
42 enum output_item_class
43 output_item_class_from_string (const char *name)
45 #define OUTPUT_CLASS(ENUM, NAME) \
46 if (!strcmp (name, NAME)) return OUTPUT_CLASS_##ENUM;
50 return (enum output_item_class) OUTPUT_N_CLASSES;
53 enum output_item_class
54 output_item_classify (const struct output_item *item)
56 const char *label = output_item_get_label (item);
62 case OUTPUT_ITEM_CHART:
63 return OUTPUT_CLASS_CHARTS;
65 case OUTPUT_ITEM_GROUP:
66 return OUTPUT_CLASS_OUTLINEHEADERS;
68 case OUTPUT_ITEM_IMAGE:
69 return OUTPUT_CLASS_OTHER;
71 case OUTPUT_ITEM_MESSAGE:
72 return (item->message->severity == MSG_S_NOTE
74 : OUTPUT_CLASS_WARNINGS);
76 case OUTPUT_ITEM_PAGE_BREAK:
77 return OUTPUT_CLASS_OTHER;
79 case OUTPUT_ITEM_TABLE:
80 return (!strcmp (label, "Warnings") ? OUTPUT_CLASS_WARNINGS
81 : !strcmp (label, "Notes") ? OUTPUT_CLASS_NOTES
82 : OUTPUT_CLASS_TABLES);
84 case OUTPUT_ITEM_TEXT:
85 return (!strcmp (label, "Title") ? OUTPUT_CLASS_HEADINGS
86 : !strcmp (label, "Log") ? OUTPUT_CLASS_LOGS
87 : !strcmp (label, "Page Title") ? OUTPUT_CLASS_PAGETITLE
88 : OUTPUT_CLASS_TEXTS);
91 return OUTPUT_CLASS_UNKNOWN;
96 string_matches (const char *pattern, const char *s)
98 /* XXX This should be a Unicode case insensitive comparison. */
99 while (c_tolower (*pattern) == c_tolower (*s))
101 if (*pattern == '\0')
108 return pattern[0] == '*' && pattern[1] == '\0';
112 string_array_matches (const char *name, const struct string_array *array)
119 for (size_t i = 0; i < array->n; i++)
120 if (string_matches (array->strings[i], name))
127 match (const char *name,
128 const struct string_array *white,
129 const struct string_array *black)
131 return (string_array_matches (name, white) != false
132 && string_array_matches (name, black) != true);
136 match_instance (const int *instances, size_t n_instances,
137 int instance_within_command)
140 for (size_t i = 0; i < n_instances; i++)
142 if (instances[i] == instance_within_command)
144 else if (instances[i] == -1)
151 match_command (size_t nth_command, size_t *commands, size_t n_commands)
153 for (size_t i = 0; i < n_commands; i++)
154 if (nth_command == commands[i])
160 select_matches (const struct output_item **items,
161 unsigned int *depths, size_t n_items,
162 const struct output_criteria *c, unsigned long int *include)
164 /* Counting instances within a command. */
165 int instance_within_command = 0;
166 int last_instance = -1;
168 /* Counting commands. */
169 const struct output_item *command_item = NULL;
170 const struct output_item *command_command_item = NULL;
171 size_t nth_command = 0;
173 for (size_t i = 0; i < n_items; i++)
175 const struct output_item *item = items[i];
180 if (last_instance >= 0)
182 bitvector_set1 (include, last_instance);
185 instance_within_command = 0;
188 if (!((1u << output_item_classify (item)) & c->classes))
191 if (!c->include_hidden && item->type != OUTPUT_ITEM_GROUP && !item->show)
194 if (c->error && (!item->spv_info || !item->spv_info->error))
197 if (!match (item->command_name,
198 &c->include.commands, &c->exclude.commands))
203 if (command_item != command_command_item)
205 command_command_item = command_item;
209 if (!match_command (nth_command, c->commands, c->n_commands))
213 char *subtype = output_item_get_subtype (item);
214 bool match_subtype = match (subtype,
215 &c->include.subtypes, &c->exclude.subtypes);
220 if (!match (output_item_get_label (item),
221 &c->include.labels, &c->exclude.labels))
226 const char *members[4];
227 size_t n = spv_info_get_members (item->spv_info, members,
228 sizeof members / sizeof *members);
231 for (size_t j = 0; j < n; j++)
232 if (string_array_matches (members[j], &c->members) == true)
245 instance_within_command++;
247 int include_instance = match_instance (c->instances, c->n_instances,
248 instance_within_command);
249 if (!include_instance)
251 else if (include_instance < 0)
258 bitvector_set1 (include, i);
261 if (last_instance >= 0)
262 bitvector_set1 (include, last_instance);
266 count_items (const struct output_item *item)
269 if (item->type == OUTPUT_ITEM_GROUP)
270 for (size_t i = 0; i < item->group.n_children; i++)
271 n += count_items (item->group.children[i]);
276 flatten_items (const struct output_item *item, size_t index, size_t depth,
277 const struct output_item **items, unsigned int *depths)
280 depths[index] = depth;
283 if (item->type == OUTPUT_ITEM_GROUP)
284 for (size_t i = 0; i < item->group.n_children; i++)
285 index = flatten_items (item->group.children[i], index, depth + 1,
292 unflatten_items (const struct output_item *in, size_t index,
293 unsigned long *include, struct output_item *out)
295 bool include_item = bitvector_is_set (include, index++);
296 if (in->type == OUTPUT_ITEM_GROUP)
298 /* If we should include the group itself, then clone IN inside OUT, and
299 add any children to the clone instead to OUT directly. */
302 struct output_item *group = group_item_clone_empty (in);
303 group_item_add_child (out, group);
307 for (size_t i = 0; i < in->group.n_children; i++)
308 index = unflatten_items (in->group.children[i], index, include, out);
313 group_item_add_child (out, output_item_ref (in));
318 /* Consumes IN, which must be a group, and returns a new output item whose
319 children are all the (direct and indirect) children of IN that meet the NC
322 output_select (struct output_item *in,
323 const struct output_criteria c[], size_t nc)
325 assert (in->type == OUTPUT_ITEM_GROUP);
329 /* Number of items (not counting the root). */
330 size_t n_items = count_items (in) - 1;
332 const struct output_item **items = xnmalloc (n_items, sizeof *items);
333 unsigned int *depths = xnmalloc (n_items, sizeof *depths);
334 size_t n_flattened = 0;
335 for (size_t i = 0; i < in->group.n_children; i++)
336 n_flattened = flatten_items (in->group.children[i], n_flattened,
338 assert (n_flattened == n_items);
340 unsigned long int *include = bitvector_allocate (n_items);
341 for (size_t i = 0; i < nc; i++)
342 select_matches (items, depths, n_items, &c[i], include);
344 struct output_item *out = root_item_create ();
345 size_t n_unflattened = 0;
346 for (size_t i = 0; i < in->group.n_children; i++)
347 n_unflattened = unflatten_items (in->group.children[i], n_unflattened,
349 assert (n_unflattened == n_items);
355 output_item_unref (in);