1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimeglob.c: Private file. Datastructure for storing the globs.
4 * More info can be found at http://www.freedesktop.org/standards/
6 * Copyright (C) 2003 Red Hat, Inc.
7 * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
9 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
16 #include "xdgmimeglob.h"
17 #include "xdgmimeint.h"
25 #define MAX(a,b) ((a) > (b) ? (a) : (b))
36 typedef struct XdgGlobHashNode XdgGlobHashNode;
37 typedef struct XdgGlobList XdgGlobList;
39 struct XdgGlobHashNode
41 xdg_unichar_t character;
42 const char *mime_type;
45 XdgGlobHashNode *next;
46 XdgGlobHashNode *child;
51 const char *mime_type;
59 XdgGlobList *literal_list;
60 XdgGlobHashNode *simple_node;
61 XdgGlobList *full_list;
68 _xdg_glob_list_new (void)
70 XdgGlobList *new_element;
72 new_element = calloc (1, sizeof (XdgGlobList));
77 /* Frees glob_list and all of its children */
79 _xdg_glob_list_free (XdgGlobList *glob_list)
81 XdgGlobList *ptr, *next;
90 free ((void *) ptr->data);
92 free ((void *) ptr->mime_type);
100 _xdg_glob_list_append (XdgGlobList *glob_list,
102 const char *mime_type,
106 XdgGlobList *new_element;
107 XdgGlobList *tmp_element;
109 tmp_element = glob_list;
110 while (tmp_element != NULL)
112 if (strcmp (tmp_element->data, data) == 0 &&
113 strcmp (tmp_element->mime_type, mime_type) == 0)
116 tmp_element = tmp_element->next;
119 new_element = _xdg_glob_list_new ();
120 new_element->data = data;
121 new_element->mime_type = mime_type;
122 new_element->weight = weight;
123 new_element->case_sensitive = case_sensitive;
124 if (glob_list == NULL)
127 tmp_element = glob_list;
128 while (tmp_element->next != NULL)
129 tmp_element = tmp_element->next;
131 tmp_element->next = new_element;
139 static XdgGlobHashNode *
140 _xdg_glob_hash_node_new (void)
142 XdgGlobHashNode *glob_hash_node;
144 glob_hash_node = calloc (1, sizeof (XdgGlobHashNode));
146 return glob_hash_node;
150 _xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
154 for (i = 0; i < depth; i++)
157 printf ("%c", (char)glob_hash_node->character);
158 if (glob_hash_node->mime_type)
159 printf (" - %s %d\n", glob_hash_node->mime_type, glob_hash_node->weight);
162 if (glob_hash_node->child)
163 _xdg_glob_hash_node_dump (glob_hash_node->child, depth + 1);
164 if (glob_hash_node->next)
165 _xdg_glob_hash_node_dump (glob_hash_node->next, depth);
168 static XdgGlobHashNode *
169 _xdg_glob_hash_insert_ucs4 (XdgGlobHashNode *glob_hash_node,
171 const char *mime_type,
175 XdgGlobHashNode *node;
176 xdg_unichar_t character;
180 if ((glob_hash_node == NULL) ||
181 (character < glob_hash_node->character))
183 node = _xdg_glob_hash_node_new ();
184 node->character = character;
185 node->next = glob_hash_node;
186 glob_hash_node = node;
188 else if (character == glob_hash_node->character)
190 node = glob_hash_node;
194 XdgGlobHashNode *prev_node;
195 int found_node = FALSE;
197 /* Look for the first character of text in glob_hash_node, and insert it if we
199 prev_node = glob_hash_node;
200 node = prev_node->next;
204 if (character < node->character)
206 node = _xdg_glob_hash_node_new ();
207 node->character = character;
208 node->next = prev_node->next;
209 prev_node->next = node;
214 else if (character == node->character)
225 node = _xdg_glob_hash_node_new ();
226 node->character = character;
227 node->next = prev_node->next;
228 prev_node->next = node;
237 if (strcmp (node->mime_type, mime_type) != 0)
239 XdgGlobHashNode *child;
240 int found_node = FALSE;
243 while (child && child->character == 0)
245 if (strcmp (child->mime_type, mime_type) == 0)
255 child = _xdg_glob_hash_node_new ();
256 child->character = 0;
257 child->mime_type = strdup (mime_type);
258 child->weight = weight;
259 child->case_sensitive = case_sensitive;
261 child->next = node->child;
268 node->mime_type = strdup (mime_type);
269 node->weight = weight;
270 node->case_sensitive = case_sensitive;
275 node->child = _xdg_glob_hash_insert_ucs4 (node->child, text, mime_type, weight, case_sensitive);
277 return glob_hash_node;
280 /* glob must be valid UTF-8 */
281 static XdgGlobHashNode *
282 _xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
284 const char *mime_type,
288 XdgGlobHashNode *node;
289 xdg_unichar_t *unitext;
292 unitext = _xdg_convert_to_ucs4 (text, &len);
293 _xdg_reverse_ucs4 (unitext, len);
294 node = _xdg_glob_hash_insert_ucs4 (glob_hash_node, unitext, mime_type, weight, case_sensitive);
305 _xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node,
306 const char *file_name,
308 int case_sensitive_check,
309 MimeWeight mime_types[],
313 XdgGlobHashNode *node;
314 xdg_unichar_t character;
316 if (glob_hash_node == NULL)
319 character = file_name[len - 1];
321 for (node = glob_hash_node; node && character >= node->character; node = node->next)
323 if (character == node->character)
329 n = _xdg_glob_hash_node_lookup_file_name (node->child,
332 case_sensitive_check,
338 if (node->mime_type &&
339 (case_sensitive_check ||
340 !node->case_sensitive))
342 mime_types[n].mime = node->mime_type;
343 mime_types[n].weight = node->weight;
347 while (n < n_mime_types && node && node->character == 0)
349 if (node->mime_type &&
350 (case_sensitive_check ||
351 !node->case_sensitive))
353 mime_types[n].mime = node->mime_type;
354 mime_types[n].weight = node->weight;
367 static int compare_mime_weight (const void *a, const void *b)
369 const MimeWeight *aa = (const MimeWeight *)a;
370 const MimeWeight *bb = (const MimeWeight *)b;
372 return bb->weight - aa->weight;
375 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
377 ascii_tolower (const char *str)
381 lower = strdup (str);
386 *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
392 filter_out_dupes (MimeWeight mimes[], int n_mimes)
399 for (i = 0; i < last; i++)
404 if (strcmp (mimes[i].mime, mimes[j].mime) == 0)
406 mimes[i].weight = MAX (mimes[i].weight, mimes[j].weight);
408 mimes[j].mime = mimes[last].mime;
409 mimes[j].weight = mimes[last].weight;
420 _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
421 const char *file_name,
422 const char *mime_types[],
427 MimeWeight mimes[10];
432 /* First, check the literals */
434 assert (file_name != NULL && n_mime_types > 0);
438 lower_case = ascii_tolower (file_name);
440 for (list = glob_hash->literal_list; list; list = list->next)
442 if (strcmp ((const char *)list->data, file_name) == 0)
444 mime_types[0] = list->mime_type;
450 for (list = glob_hash->literal_list; list; list = list->next)
452 if (!list->case_sensitive &&
453 strcmp ((const char *)list->data, lower_case) == 0)
455 mime_types[0] = list->mime_type;
462 len = strlen (file_name);
463 n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, lower_case, len, FALSE,
466 n += _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, file_name, len, TRUE,
467 mimes + n, n_mimes - n);
471 for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next)
473 if (fnmatch ((const char *)list->data, file_name, 0) == 0)
475 mimes[n].mime = list->mime_type;
476 mimes[n].weight = list->weight;
483 n = filter_out_dupes (mimes, n);
485 qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
487 if (n_mime_types < n)
490 for (i = 0; i < n; i++)
491 mime_types[i] = mimes[i].mime;
502 _xdg_glob_hash_new (void)
504 XdgGlobHash *glob_hash;
506 glob_hash = calloc (1, sizeof (XdgGlobHash));
513 _xdg_glob_hash_free_nodes (XdgGlobHashNode *node)
518 _xdg_glob_hash_free_nodes (node->child);
520 _xdg_glob_hash_free_nodes (node->next);
522 free ((void *) node->mime_type);
528 _xdg_glob_hash_free (XdgGlobHash *glob_hash)
530 _xdg_glob_list_free (glob_hash->literal_list);
531 _xdg_glob_list_free (glob_hash->full_list);
532 _xdg_glob_hash_free_nodes (glob_hash->simple_node);
537 _xdg_glob_determine_type (const char *glob)
540 int maybe_in_simple_glob = FALSE;
541 int first_char = TRUE;
547 if (*ptr == '*' && first_char)
548 maybe_in_simple_glob = TRUE;
549 else if (*ptr == '\\' || *ptr == '[' || *ptr == '?' || *ptr == '*')
550 return XDG_GLOB_FULL;
553 ptr = _xdg_utf8_next_char (ptr);
555 if (maybe_in_simple_glob)
556 return XDG_GLOB_SIMPLE;
558 return XDG_GLOB_LITERAL;
561 /* glob must be valid UTF-8 */
563 _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
565 const char *mime_type,
571 assert (glob_hash != NULL);
572 assert (glob != NULL);
574 type = _xdg_glob_determine_type (glob);
578 case XDG_GLOB_LITERAL:
579 glob_hash->literal_list = _xdg_glob_list_append (glob_hash->literal_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
581 case XDG_GLOB_SIMPLE:
582 glob_hash->simple_node = _xdg_glob_hash_insert_text (glob_hash->simple_node, glob + 1, mime_type, weight, case_sensitive);
585 glob_hash->full_list = _xdg_glob_list_append (glob_hash->full_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
591 _xdg_glob_hash_dump (XdgGlobHash *glob_hash)
594 printf ("LITERAL STRINGS\n");
595 if (!glob_hash || glob_hash->literal_list == NULL)
601 for (list = glob_hash->literal_list; list; list = list->next)
602 printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
604 printf ("\nSIMPLE GLOBS\n");
605 if (!glob_hash || glob_hash->simple_node == NULL)
611 _xdg_glob_hash_node_dump (glob_hash->simple_node, 4);
614 printf ("\nFULL GLOBS\n");
615 if (!glob_hash || glob_hash->full_list == NULL)
621 for (list = glob_hash->full_list; list; list = list->next)
622 printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
628 _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
629 const char *file_name,
636 glob_file = fopen (file_name, "r");
638 if (glob_file == NULL)
641 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
643 while (fgets (line, 255, glob_file) != NULL)
646 char *mimetype, *glob, *end;
650 if (line[0] == '#' || line[0] == 0)
653 end = line + strlen(line) - 1;
660 colon = strchr (p, ':');
670 colon = strchr (p, ':');
678 case_sensitive = FALSE;
680 colon = strchr (p, ':');
681 if (version_two && colon != NULL)
689 /* Flags end at next colon */
690 colon = strchr (p, ':');
694 flag = strstr (p, "cs");
696 /* Start or after comma */
699 /* ends with comma or end of string */
702 case_sensitive = TRUE;
705 _xdg_glob_hash_append_glob (glob_hash, glob, mimetype, weight, case_sensitive);