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);
219 if (!match (output_item_get_label (item),
220 &c->include.labels, &c->exclude.labels))
225 const char *members[4];
226 size_t n = spv_info_get_members (item->spv_info, members,
227 sizeof members / sizeof *members);
230 for (size_t j = 0; j < n; j++)
231 if (string_array_matches (members[j], &c->members) == true)
244 instance_within_command++;
246 int include_instance = match_instance (c->instances, c->n_instances,
247 instance_within_command);
248 if (!include_instance)
250 else if (include_instance < 0)
257 bitvector_set1 (include, i);
260 if (last_instance >= 0)
261 bitvector_set1 (include, last_instance);
265 count_items (const struct output_item *item)
268 if (item->type == OUTPUT_ITEM_GROUP)
269 for (size_t i = 0; i < item->group.n_children; i++)
270 n += count_items (item->group.children[i]);
275 flatten_items (const struct output_item *item, size_t index, size_t depth,
276 const struct output_item **items, unsigned int *depths)
279 depths[index] = depth;
282 if (item->type == OUTPUT_ITEM_GROUP)
283 for (size_t i = 0; i < item->group.n_children; i++)
284 index = flatten_items (item->group.children[i], index, depth + 1,
291 unflatten_items (const struct output_item *in, size_t index,
292 unsigned long *include, struct output_item *out)
294 bool include_item = bitvector_is_set (include, index++);
295 if (in->type == OUTPUT_ITEM_GROUP)
297 /* If we should include the group itself, then clone IN inside OUT, and
298 add any children to the clone instead to OUT directly. */
301 struct output_item *group = group_item_clone_empty (in);
302 group_item_add_child (out, group);
306 for (size_t i = 0; i < in->group.n_children; i++)
307 index = unflatten_items (in->group.children[i], index, include, out);
312 group_item_add_child (out, output_item_ref (in));
317 /* Consumes IN, which must be a group, and returns a new output item whose
318 children are all the (direct and indirect) children of IN that meet the NC
321 output_select (struct output_item *in,
322 const struct output_criteria c[], size_t nc)
324 assert (in->type == OUTPUT_ITEM_GROUP);
328 /* Number of items (not counting the root). */
329 size_t n_items = count_items (in) - 1;
331 const struct output_item **items = xnmalloc (n_items, sizeof *items);
332 unsigned int *depths = xnmalloc (n_items, sizeof *depths);
333 size_t n_flattened = 0;
334 for (size_t i = 0; i < in->group.n_children; i++)
335 n_flattened = flatten_items (in->group.children[i], n_flattened,
337 assert (n_flattened == n_items);
339 unsigned long int *include = bitvector_allocate (n_items);
340 for (size_t i = 0; i < nc; i++)
341 select_matches (items, depths, n_items, &c[i], include);
343 struct output_item *out = root_item_create ();
344 size_t n_unflattened = 0;
345 for (size_t i = 0; i < in->group.n_children; i++)
346 n_unflattened = unflatten_items (in->group.children[i], n_unflattened,
348 assert (n_unflattened == n_items);
354 output_item_unref (in);