|
16 | 16 |
|
17 | 17 | #include <strings.h> |
18 | 18 | #include <string.h> |
| 19 | +#include <ctype.h> |
19 | 20 | #include "common/sort-utils.h" |
| 21 | +#include "common/messages.h" |
20 | 22 |
|
21 | 23 | int compare_init(struct compare *comp, const struct sortdef *sortdef) |
22 | 24 | { |
@@ -55,3 +57,157 @@ int compare_add_sort_key(struct compare *comp, const char *key) |
55 | 57 | } |
56 | 58 | return 0; |
57 | 59 | } |
| 60 | + |
| 61 | +/* |
| 62 | + * Append given sort by its @id from associated sortdef. |
| 63 | + * |
| 64 | + * Return: 0 if id is valid |
| 65 | + * -1 if id not in sortdef |
| 66 | + */ |
| 67 | +int compare_add_sort_id(struct compare *comp, int id) |
| 68 | +{ |
| 69 | + int i; |
| 70 | + |
| 71 | + if (!comp->sortdef) |
| 72 | + return -1; |
| 73 | + |
| 74 | + if (id < 0) |
| 75 | + return -1; |
| 76 | + |
| 77 | + for (i = 0; i < SORT_MAX_KEYS; i++) { |
| 78 | + if (comp->sortdef[i].name == NULL) |
| 79 | + return -1; |
| 80 | + if (comp->sortdef[i].id == id) { |
| 81 | + comp->comp[comp->count] = comp->sortdef[i].comp; |
| 82 | + comp->count++; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +/* |
| 90 | + * Consume word-like list of key names (coma separated) and return its id if |
| 91 | + * found in sortdef. The @next pointer is advanced to the next expected key start. |
| 92 | + * Empty and NULL @next is accepted. |
| 93 | + * |
| 94 | + * Key lookup is case insensitive. |
| 95 | + * |
| 96 | + * Retrun: id from sortdef if a matching |
| 97 | + * -1 on error |
| 98 | + * -2 end of buffer |
| 99 | + */ |
| 100 | +int compare_parse_key_to_id(const struct compare *comp, const char **next) |
| 101 | +{ |
| 102 | + const char *tmp = *next, *start = *next; |
| 103 | + |
| 104 | + if (!comp->sortdef) |
| 105 | + return -1; |
| 106 | + |
| 107 | + /* No sort string (use defaults), or last. */ |
| 108 | + if (!*next || !**next) |
| 109 | + return -2; |
| 110 | + |
| 111 | + do { |
| 112 | + /* End of word. */ |
| 113 | + if (*tmp == ',' || *tmp == 0) { |
| 114 | + /* Look up in sortdef. */ |
| 115 | + for (int i = 0; comp->sortdef[i].name; i++) { |
| 116 | + int len = strlen(comp->sortdef[i].name); |
| 117 | + |
| 118 | + if (strncasecmp(start, comp->sortdef[i].name, len) == 0) { |
| 119 | + /* Point to last NUL. */ |
| 120 | + *next = tmp; |
| 121 | + /* Or the next valid char. */ |
| 122 | + if (*tmp) |
| 123 | + (*next)++; |
| 124 | + return comp->sortdef[i].id; |
| 125 | + } |
| 126 | + } |
| 127 | + /* Not found, report which one. */ |
| 128 | + *next = start; |
| 129 | + return -1; |
| 130 | + } |
| 131 | + /* Invalid char found. */ |
| 132 | + if (!isalnum(*tmp)) { |
| 133 | + *next = tmp; |
| 134 | + return -1; |
| 135 | + } |
| 136 | + tmp++; |
| 137 | + } while(1); |
| 138 | + |
| 139 | + /* Not found. */ |
| 140 | + *next = start; |
| 141 | + return -1; |
| 142 | +} |
| 143 | + |
| 144 | +/* Read id of its associated sort @key. Key lookup is case insensitive. */ |
| 145 | +int compare_key_id(const struct compare *comp, const char *key) |
| 146 | +{ |
| 147 | + if (!comp->sortdef) |
| 148 | + return -1; |
| 149 | + |
| 150 | + for (int i = 0; comp->sortdef[i].name; i++) |
| 151 | + if (strcasecmp(comp->sortdef[i].name, key) == 0) |
| 152 | + return comp->sortdef[i].id; |
| 153 | + return -1; |
| 154 | +} |
| 155 | + |
| 156 | +/* Read sort key name associated to @id. */ |
| 157 | +const char *compare_id_name(const struct compare *comp, int id) |
| 158 | +{ |
| 159 | + if (!comp->sortdef) |
| 160 | + return NULL; |
| 161 | + |
| 162 | + for (int i = 0; comp->sortdef[i].name; i++) |
| 163 | + if (comp->sortdef[i].id == id) |
| 164 | + return comp->sortdef[i].name; |
| 165 | + return NULL; |
| 166 | +} |
| 167 | + |
| 168 | +/* |
| 169 | + * Check if the given @id (must exist in the associated sortdef) enabled in |
| 170 | + * @comp. |
| 171 | + */ |
| 172 | +bool compare_has_id(const struct compare *comp, int id) |
| 173 | +{ |
| 174 | + int idx; |
| 175 | + |
| 176 | + if (!comp->sortdef) |
| 177 | + return false; |
| 178 | + |
| 179 | + idx = -1; |
| 180 | + for (int i = 0; comp->sortdef[i].name; i++) |
| 181 | + if (comp->sortdef[i].id == id) |
| 182 | + idx = i; |
| 183 | + |
| 184 | + if (idx < 0) |
| 185 | + return false; |
| 186 | + |
| 187 | + for (int i = 0; i < comp->count; i++) |
| 188 | + if (comp->comp[i] == comp->sortdef[idx].comp) |
| 189 | + return true; |
| 190 | + return false; |
| 191 | +} |
| 192 | + |
| 193 | +/* |
| 194 | + * Set up compare structure with associated sortdef from a user specified list |
| 195 | + * of keys. |
| 196 | + */ |
| 197 | +int compare_setup_sort(struct compare *comp, const struct sortdef *sdef, const char *def) |
| 198 | +{ |
| 199 | + const char *tmp; |
| 200 | + int id; |
| 201 | + |
| 202 | + tmp = def; |
| 203 | + do { |
| 204 | + id = compare_parse_key_to_id(comp, &tmp); |
| 205 | + if (id == -1) { |
| 206 | + error("unknown sort key: %s", tmp); |
| 207 | + return -1; |
| 208 | + } |
| 209 | + compare_add_sort_id(comp, id); |
| 210 | + } while (id >= 0); |
| 211 | + |
| 212 | + return 0; |
| 213 | +} |
0 commit comments