7f76b73a85c2b060be68afae786ffa95318a11db
[pspp] / src / output / select.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2018 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "output/select.h"
20
21 #include <string.h>
22
23 #include "libpspp/assertion.h"
24 #include "libpspp/bit-vector.h"
25 #include "libpspp/message.h"
26
27 #include "gl/c-ctype.h"
28 #include "gl/xalloc.h"
29
30 const char *
31 output_item_class_to_string (enum output_item_class class)
32 {
33   switch (class)
34     {
35 #define OUTPUT_CLASS(ENUM, NAME) case OUTPUT_CLASS_##ENUM: return NAME;
36       OUTPUT_CLASSES
37 #undef OUTPUT_CLASS
38     default: return NULL;
39     }
40 }
41
42 enum output_item_class
43 output_item_class_from_string (const char *name)
44 {
45 #define OUTPUT_CLASS(ENUM, NAME) \
46   if (!strcmp (name, NAME)) return OUTPUT_CLASS_##ENUM;
47   OUTPUT_CLASSES
48 #undef OUTPUT_CLASS
49
50   return (enum output_item_class) OUTPUT_N_CLASSES;
51 }
52
53 enum output_item_class
54 output_item_classify (const struct output_item *item)
55 {
56   const char *label = output_item_get_label (item);
57   if (!label)
58     label = "";
59
60   switch (item->type)
61     {
62     case OUTPUT_ITEM_CHART:
63       return OUTPUT_CLASS_CHARTS;
64
65     case OUTPUT_ITEM_GROUP:
66       return OUTPUT_CLASS_OUTLINEHEADERS;
67
68     case OUTPUT_ITEM_IMAGE:
69       return OUTPUT_CLASS_OTHER;
70
71     case OUTPUT_ITEM_MESSAGE:
72       return (item->message->severity == MSG_S_NOTE
73               ? OUTPUT_CLASS_NOTES
74               : OUTPUT_CLASS_WARNINGS);
75
76     case OUTPUT_ITEM_PAGE_BREAK:
77       return OUTPUT_CLASS_OTHER;
78
79     case OUTPUT_ITEM_TABLE:
80       return (!strcmp (label, "Warnings") ? OUTPUT_CLASS_WARNINGS
81               : !strcmp (label, "Notes") ? OUTPUT_CLASS_NOTES
82               : OUTPUT_CLASS_TABLES);
83
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);
89
90     default:
91       return OUTPUT_CLASS_UNKNOWN;
92     }
93 }
94 \f
95 static bool
96 string_matches (const char *pattern, const char *s)
97 {
98   /* XXX This should be a Unicode case insensitive comparison. */
99   while (c_tolower (*pattern) == c_tolower (*s))
100     {
101       if (*pattern == '\0')
102         return true;
103
104       pattern++;
105       s++;
106     }
107
108   return pattern[0] == '*' && pattern[1] == '\0';
109 }
110
111 static int
112 string_array_matches (const char *name, const struct string_array *array)
113 {
114   if (!array->n)
115     return -1;
116   else if (!name)
117     return false;
118
119   for (size_t i = 0; i < array->n; i++)
120     if (string_matches (array->strings[i], name))
121       return true;
122
123   return false;
124 }
125
126 static bool
127 match (const char *name,
128        const struct string_array *white,
129        const struct string_array *black)
130 {
131   return (string_array_matches (name, white) != false
132           && string_array_matches (name, black) != true);
133 }
134
135 static int
136 match_instance (const int *instances, size_t n_instances,
137                 int instance_within_command)
138 {
139   int retval = false;
140   for (size_t i = 0; i < n_instances; i++)
141     {
142       if (instances[i] == instance_within_command)
143         return true;
144       else if (instances[i] == -1)
145         retval = -1;
146     }
147   return retval;
148 }
149
150 static bool
151 match_command (size_t nth_command, size_t *commands, size_t n_commands)
152 {
153   for (size_t i = 0; i < n_commands; i++)
154     if (nth_command == commands[i])
155       return true;
156   return false;
157 }
158
159 static void
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)
163 {
164   /* Counting instances within a command. */
165   int instance_within_command = 0;
166   int last_instance = -1;
167
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;
172
173   for (size_t i = 0; i < n_items; i++)
174     {
175       const struct output_item *item = items[i];
176
177       if (depths[i] == 0)
178         {
179           command_item = item;
180           if (last_instance >= 0)
181             {
182               bitvector_set1 (include, last_instance);
183               last_instance = -1;
184             }
185           instance_within_command = 0;
186         }
187
188       if (!((1u << output_item_classify (item)) & c->classes))
189         continue;
190
191       if (!c->include_hidden && item->type != OUTPUT_ITEM_GROUP && !item->show)
192         continue;
193
194       if (c->error && (!item->spv_info || !item->spv_info->error))
195         continue;
196
197       if (!match (item->command_name,
198                   &c->include.commands, &c->exclude.commands))
199         continue;
200
201       if (c->n_commands)
202         {
203           if (command_item != command_command_item)
204             {
205               command_command_item = command_item;
206               nth_command++;
207             }
208
209           if (!match_command (nth_command, c->commands, c->n_commands))
210             continue;
211         }
212
213       char *subtype = output_item_get_subtype (item);
214       bool match_subtype = match (subtype,
215                                   &c->include.subtypes, &c->exclude.subtypes);
216       if (!match_subtype)
217         continue;
218
219       if (!match (output_item_get_label (item),
220                   &c->include.labels, &c->exclude.labels))
221         continue;
222
223       if (c->members.n)
224         {
225           const char *members[4];
226           size_t n = spv_info_get_members (item->spv_info, members,
227                                            sizeof members / sizeof *members);
228
229           bool found = false;
230           for (size_t j = 0; j < n; j++)
231             if (string_array_matches (members[j], &c->members) == true)
232               {
233                 found = true;
234                 break;
235               }
236           if (!found)
237             continue;
238         }
239
240       if (c->n_instances)
241         {
242           if (!depths[i])
243             continue;
244           instance_within_command++;
245
246           int include_instance = match_instance (c->instances, c->n_instances,
247                                                  instance_within_command);
248           if (!include_instance)
249             continue;
250           else if (include_instance < 0)
251             {
252               last_instance = i;
253               continue;
254             }
255         }
256
257       bitvector_set1 (include, i);
258     }
259
260   if (last_instance >= 0)
261     bitvector_set1 (include, last_instance);
262 }
263
264 static size_t
265 count_items (const struct output_item *item)
266 {
267   size_t n = 1;
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]);
271   return n;
272 }
273
274 static size_t
275 flatten_items (const struct output_item *item, size_t index, size_t depth,
276                const struct output_item **items, unsigned int *depths)
277 {
278   items[index] = item;
279   depths[index] = depth;
280   index++;
281
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,
285                              items, depths);
286
287   return index;
288 }
289
290 static size_t
291 unflatten_items (const struct output_item *in, size_t index,
292                  unsigned long *include, struct output_item *out)
293 {
294   bool include_item = bitvector_is_set (include, index++);
295   if (in->type == OUTPUT_ITEM_GROUP)
296     {
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. */
299       if (include_item)
300         {
301           struct output_item *group = group_item_clone_empty (in);
302           group_item_add_child (out, group);
303           out = group;
304         }
305
306       for (size_t i = 0; i < in->group.n_children; i++)
307         index = unflatten_items (in->group.children[i], index, include, out);
308     }
309   else
310     {
311       if (include_item)
312         group_item_add_child (out, output_item_ref (in));
313     }
314   return index;
315 }
316
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
319    criteria in C[]. */
320 struct output_item *
321 output_select (struct output_item *in,
322                const struct output_criteria c[], size_t nc)
323 {
324   assert (in->type == OUTPUT_ITEM_GROUP);
325   if (!nc)
326     return in;
327
328   /* Number of items (not counting the root). */
329   size_t n_items = count_items (in) - 1;
330
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,
336                                  0, items, depths);
337   assert (n_flattened == n_items);
338
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);
342
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,
347                                      include, out);
348   assert (n_unflattened == n_items);
349
350   free (items);
351   free (depths);
352   free (include);
353
354   output_item_unref (in);
355   return out;
356 }