src/output/select.c (select_matches): Fix memory leak
[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       free (subtype);
217       if (!match_subtype)
218         continue;
219
220       if (!match (output_item_get_label (item),
221                   &c->include.labels, &c->exclude.labels))
222         continue;
223
224       if (c->members.n)
225         {
226           const char *members[4];
227           size_t n = spv_info_get_members (item->spv_info, members,
228                                            sizeof members / sizeof *members);
229
230           bool found = false;
231           for (size_t j = 0; j < n; j++)
232             if (string_array_matches (members[j], &c->members) == true)
233               {
234                 found = true;
235                 break;
236               }
237           if (!found)
238             continue;
239         }
240
241       if (c->n_instances)
242         {
243           if (!depths[i])
244             continue;
245           instance_within_command++;
246
247           int include_instance = match_instance (c->instances, c->n_instances,
248                                                  instance_within_command);
249           if (!include_instance)
250             continue;
251           else if (include_instance < 0)
252             {
253               last_instance = i;
254               continue;
255             }
256         }
257
258       bitvector_set1 (include, i);
259     }
260
261   if (last_instance >= 0)
262     bitvector_set1 (include, last_instance);
263 }
264
265 static size_t
266 count_items (const struct output_item *item)
267 {
268   size_t n = 1;
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]);
272   return n;
273 }
274
275 static size_t
276 flatten_items (const struct output_item *item, size_t index, size_t depth,
277                const struct output_item **items, unsigned int *depths)
278 {
279   items[index] = item;
280   depths[index] = depth;
281   index++;
282
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,
286                              items, depths);
287
288   return index;
289 }
290
291 static size_t
292 unflatten_items (const struct output_item *in, size_t index,
293                  unsigned long *include, struct output_item *out)
294 {
295   bool include_item = bitvector_is_set (include, index++);
296   if (in->type == OUTPUT_ITEM_GROUP)
297     {
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. */
300       if (include_item)
301         {
302           struct output_item *group = group_item_clone_empty (in);
303           group_item_add_child (out, group);
304           out = group;
305         }
306
307       for (size_t i = 0; i < in->group.n_children; i++)
308         index = unflatten_items (in->group.children[i], index, include, out);
309     }
310   else
311     {
312       if (include_item)
313         group_item_add_child (out, output_item_ref (in));
314     }
315   return index;
316 }
317
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
320    criteria in C[]. */
321 struct output_item *
322 output_select (struct output_item *in,
323                const struct output_criteria c[], size_t nc)
324 {
325   assert (in->type == OUTPUT_ITEM_GROUP);
326   if (!nc)
327     return in;
328
329   /* Number of items (not counting the root). */
330   size_t n_items = count_items (in) - 1;
331
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,
337                                  0, items, depths);
338   assert (n_flattened == n_items);
339
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);
343
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,
348                                      include, out);
349   assert (n_unflattened == n_items);
350
351   free (items);
352   free (depths);
353   free (include);
354
355   output_item_unref (in);
356   return out;
357 }