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 "spv-select.h"
23 #include "libpspp/assertion.h"
24 #include "libpspp/bit-vector.h"
25 #include "output/spv/spv.h"
27 #include "gl/c-ctype.h"
28 #include "gl/xalloc.h"
31 is_command_item (const struct spv_item *item)
33 return !item->parent || !item->parent->parent;
36 static struct spv_item *
37 find_command_item (struct spv_item *item)
39 while (!is_command_item (item))
45 string_matches (const char *pattern, const char *s)
47 /* XXX This should be a Unicode case insensitive comparison. */
48 while (c_tolower (*pattern) == c_tolower (*s))
57 return pattern[0] == '*' && pattern[1] == '\0';
61 string_array_matches (const char *name, const struct string_array *array)
68 for (size_t i = 0; i < array->n; i++)
69 if (string_matches (array->strings[i], name))
76 match (const char *name,
77 const struct string_array *white,
78 const struct string_array *black)
80 return (string_array_matches (name, white) != false
81 && string_array_matches (name, black) != true);
85 match_instance (const int *instances, size_t n_instances,
86 int instance_within_command)
89 for (size_t i = 0; i < n_instances; i++)
91 if (instances[i] == instance_within_command)
93 else if (instances[i] == -1)
100 match_command (size_t nth_command, size_t *commands, size_t n_commands)
102 for (size_t i = 0; i < n_commands; i++)
103 if (nth_command == commands[i])
109 select_matches (const struct spv_reader *spv, const struct spv_criteria *c,
110 unsigned long int *include)
112 /* Counting instances within a command. */
113 struct spv_item *instance_command_item = NULL;
114 int instance_within_command = 0;
115 int last_instance = -1;
117 /* Counting commands. */
118 struct spv_item *command_command_item = NULL;
119 size_t nth_command = 0;
121 struct spv_item *item;
123 SPV_ITEM_FOR_EACH_SKIP_ROOT (item, spv_get_root (spv))
127 struct spv_item *new_command_item = find_command_item (item);
128 if (new_command_item != instance_command_item)
130 if (last_instance >= 0)
132 bitvector_set1 (include, last_instance);
136 instance_command_item = new_command_item;
137 instance_within_command = 0;
140 if (!((1u << spv_item_get_class (item)) & c->classes))
143 if (!c->include_hidden && !spv_item_is_visible (item))
148 spv_item_load (item);
153 if (!match (spv_item_get_command_id (item),
154 &c->include.commands, &c->exclude.commands))
159 if (new_command_item != command_command_item)
161 command_command_item = new_command_item;
165 if (!match_command (nth_command, c->commands, c->n_commands))
169 if (!match (spv_item_get_subtype (item),
170 &c->include.subtypes, &c->exclude.subtypes))
173 if (!match (spv_item_get_label (item),
174 &c->include.labels, &c->exclude.labels))
178 && !((item->xml_member
179 && string_array_matches (item->xml_member, &c->members)) ||
181 && string_array_matches (item->bin_member, &c->members))))
186 if (is_command_item (item))
188 instance_within_command++;
190 int include_instance = match_instance (c->instances, c->n_instances,
191 instance_within_command);
192 if (!include_instance)
194 else if (include_instance < 0)
196 last_instance = index;
201 bitvector_set1 (include, index);
204 if (last_instance >= 0)
205 bitvector_set1 (include, last_instance);
209 spv_select (const struct spv_reader *spv,
210 const struct spv_criteria c[], size_t nc,
211 struct spv_item ***itemsp, size_t *n_itemsp)
213 struct spv_item *item;
215 struct spv_criteria default_criteria = SPV_CRITERIA_INITIALIZER;
219 c = &default_criteria;
223 size_t max_items = 0;
224 SPV_ITEM_FOR_EACH_SKIP_ROOT (item, spv_get_root (spv))
227 /* Allocate bitmap for items then fill it in with selected items. */
228 unsigned long int *include = bitvector_allocate (max_items);
229 for (size_t i = 0; i < nc; i++)
230 select_matches (spv, &c[i], include);
232 /* Copy selected items into output array. */
234 struct spv_item **items = xnmalloc (bitvector_count (include, max_items),
237 SPV_ITEM_FOR_EACH_SKIP_ROOT (item, spv_get_root (spv))
238 if (bitvector_is_set (include, i++))
239 items[n_items++] = item;