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 * Licensed under the Academic Free License version 2.0
10 * Or under the following terms:
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
32 #include "xdgmimeglob.h"
33 #include "xdgmimeint.h"
41 #define MAX(a,b) ((a) > (b) ? (a) : (b))
52 typedef struct XdgGlobHashNode XdgGlobHashNode;
53 typedef struct XdgGlobList XdgGlobList;
55 struct XdgGlobHashNode
57 xdg_unichar_t character;
58 const char *mime_type;
61 XdgGlobHashNode *next;
62 XdgGlobHashNode *child;
67 const char *mime_type;
75 XdgGlobList *literal_list;
76 XdgGlobHashNode *simple_node;
77 XdgGlobList *full_list;
84 _xdg_glob_list_new (void)
86 XdgGlobList *new_element;
88 new_element = calloc (1, sizeof (XdgGlobList));
93 /* Frees glob_list and all of it's children */
95 _xdg_glob_list_free (XdgGlobList *glob_list)
97 XdgGlobList *ptr, *next;
106 free ((void *) ptr->data);
108 free ((void *) ptr->mime_type);
116 _xdg_glob_list_append (XdgGlobList *glob_list,
118 const char *mime_type,
122 XdgGlobList *new_element;
123 XdgGlobList *tmp_element;
125 tmp_element = glob_list;
126 while (tmp_element != NULL)
128 if (strcmp (tmp_element->data, data) == 0 &&
129 strcmp (tmp_element->mime_type, mime_type) == 0)
132 tmp_element = tmp_element->next;
135 new_element = _xdg_glob_list_new ();
136 new_element->data = data;
137 new_element->mime_type = mime_type;
138 new_element->weight = weight;
139 new_element->case_sensitive = case_sensitive;
140 if (glob_list == NULL)
143 tmp_element = glob_list;
144 while (tmp_element->next != NULL)
145 tmp_element = tmp_element->next;
147 tmp_element->next = new_element;
155 static XdgGlobHashNode *
156 _xdg_glob_hash_node_new (void)
158 XdgGlobHashNode *glob_hash_node;
160 glob_hash_node = calloc (1, sizeof (XdgGlobHashNode));
162 return glob_hash_node;
165 #ifdef NOT_USED_IN_GIO
168 _xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
172 for (i = 0; i < depth; i++)
175 printf ("%c", (char)glob_hash_node->character);
176 if (glob_hash_node->mime_type)
177 printf (" - %s %d\n", glob_hash_node->mime_type, glob_hash_node->weight);
180 if (glob_hash_node->child)
181 _xdg_glob_hash_node_dump (glob_hash_node->child, depth + 1);
182 if (glob_hash_node->next)
183 _xdg_glob_hash_node_dump (glob_hash_node->next, depth);
188 static XdgGlobHashNode *
189 _xdg_glob_hash_insert_ucs4 (XdgGlobHashNode *glob_hash_node,
191 const char *mime_type,
195 XdgGlobHashNode *node;
196 xdg_unichar_t character;
200 if ((glob_hash_node == NULL) ||
201 (character < glob_hash_node->character))
203 node = _xdg_glob_hash_node_new ();
204 node->character = character;
205 node->next = glob_hash_node;
206 glob_hash_node = node;
208 else if (character == glob_hash_node->character)
210 node = glob_hash_node;
214 XdgGlobHashNode *prev_node;
215 int found_node = FALSE;
217 /* Look for the first character of text in glob_hash_node, and insert it if we
219 prev_node = glob_hash_node;
220 node = prev_node->next;
224 if (character < node->character)
226 node = _xdg_glob_hash_node_new ();
227 node->character = character;
228 node->next = prev_node->next;
229 prev_node->next = node;
234 else if (character == node->character)
245 node = _xdg_glob_hash_node_new ();
246 node->character = character;
247 node->next = prev_node->next;
248 prev_node->next = node;
257 if (strcmp (node->mime_type, mime_type) != 0)
259 XdgGlobHashNode *child;
260 int found_node = FALSE;
263 while (child && child->character == 0)
265 if (strcmp (child->mime_type, mime_type) == 0)
275 child = _xdg_glob_hash_node_new ();
276 child->character = 0;
277 child->mime_type = strdup (mime_type);
278 child->weight = weight;
279 child->case_sensitive = case_sensitive;
281 child->next = node->child;
288 node->mime_type = strdup (mime_type);
289 node->weight = weight;
290 node->case_sensitive = case_sensitive;
295 node->child = _xdg_glob_hash_insert_ucs4 (node->child, text, mime_type, weight, case_sensitive);
297 return glob_hash_node;
300 /* glob must be valid UTF-8 */
301 static XdgGlobHashNode *
302 _xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
304 const char *mime_type,
308 XdgGlobHashNode *node;
309 xdg_unichar_t *unitext;
312 unitext = _xdg_convert_to_ucs4 (text, &len);
313 _xdg_reverse_ucs4 (unitext, len);
314 node = _xdg_glob_hash_insert_ucs4 (glob_hash_node, unitext, mime_type, weight, case_sensitive);
325 _xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node,
326 const char *file_name,
328 int case_sensitive_check,
329 MimeWeight mime_types[],
333 XdgGlobHashNode *node;
334 xdg_unichar_t character;
336 if (glob_hash_node == NULL)
339 character = file_name[len - 1];
341 for (node = glob_hash_node; node && character >= node->character; node = node->next)
343 if (character == node->character)
349 n = _xdg_glob_hash_node_lookup_file_name (node->child,
352 case_sensitive_check,
358 if (node->mime_type &&
359 (case_sensitive_check ||
360 !node->case_sensitive))
362 mime_types[n].mime = node->mime_type;
363 mime_types[n].weight = node->weight;
367 while (n < n_mime_types && node && node->character == 0)
369 if (node->mime_type &&
370 (case_sensitive_check ||
371 !node->case_sensitive))
373 mime_types[n].mime = node->mime_type;
374 mime_types[n].weight = node->weight;
387 static int compare_mime_weight (const void *a, const void *b)
389 const MimeWeight *aa = (const MimeWeight *)a;
390 const MimeWeight *bb = (const MimeWeight *)b;
392 return bb->weight - aa->weight;
395 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
397 ascii_tolower (const char *str)
401 lower = strdup (str);
406 *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
412 filter_out_dupes (MimeWeight mimes[], int n_mimes)
419 for (i = 0; i < last; i++)
424 if (strcmp (mimes[i].mime, mimes[j].mime) == 0)
426 mimes[i].weight = MAX (mimes[i].weight, mimes[j].weight);
428 mimes[j].mime = mimes[last].mime;
429 mimes[j].weight = mimes[last].weight;
440 _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
441 const char *file_name,
442 const char *mime_types[],
447 MimeWeight mimes[10];
452 /* First, check the literals */
454 assert (file_name != NULL && n_mime_types > 0);
458 lower_case = ascii_tolower (file_name);
460 for (list = glob_hash->literal_list; list; list = list->next)
462 if (strcmp ((const char *)list->data, file_name) == 0)
464 mime_types[0] = list->mime_type;
470 for (list = glob_hash->literal_list; list; list = list->next)
472 if (!list->case_sensitive &&
473 strcmp ((const char *)list->data, lower_case) == 0)
475 mime_types[0] = list->mime_type;
482 len = strlen (file_name);
483 n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, lower_case, len, FALSE,
486 n += _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, file_name, len, TRUE,
487 mimes + n, n_mimes - n);
491 for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next)
493 if (fnmatch ((const char *)list->data, file_name, 0) == 0)
495 mimes[n].mime = list->mime_type;
496 mimes[n].weight = list->weight;
503 n = filter_out_dupes (mimes, n);
505 qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
507 if (n_mime_types < n)
510 for (i = 0; i < n; i++)
511 mime_types[i] = mimes[i].mime;
522 _xdg_glob_hash_new (void)
524 XdgGlobHash *glob_hash;
526 glob_hash = calloc (1, sizeof (XdgGlobHash));
533 _xdg_glob_hash_free_nodes (XdgGlobHashNode *node)
538 _xdg_glob_hash_free_nodes (node->child);
540 _xdg_glob_hash_free_nodes (node->next);
542 free ((void *) node->mime_type);
548 _xdg_glob_hash_free (XdgGlobHash *glob_hash)
550 _xdg_glob_list_free (glob_hash->literal_list);
551 _xdg_glob_list_free (glob_hash->full_list);
552 _xdg_glob_hash_free_nodes (glob_hash->simple_node);
557 _xdg_glob_determine_type (const char *glob)
560 int maybe_in_simple_glob = FALSE;
561 int first_char = TRUE;
567 if (*ptr == '*' && first_char)
568 maybe_in_simple_glob = TRUE;
569 else if (*ptr == '\\' || *ptr == '[' || *ptr == '?' || *ptr == '*')
570 return XDG_GLOB_FULL;
573 ptr = _xdg_utf8_next_char (ptr);
575 if (maybe_in_simple_glob)
576 return XDG_GLOB_SIMPLE;
578 return XDG_GLOB_LITERAL;
581 /* glob must be valid UTF-8 */
583 _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
585 const char *mime_type,
591 assert (glob_hash != NULL);
592 assert (glob != NULL);
594 type = _xdg_glob_determine_type (glob);
598 case XDG_GLOB_LITERAL:
599 glob_hash->literal_list = _xdg_glob_list_append (glob_hash->literal_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
601 case XDG_GLOB_SIMPLE:
602 glob_hash->simple_node = _xdg_glob_hash_insert_text (glob_hash->simple_node, glob + 1, mime_type, weight, case_sensitive);
605 glob_hash->full_list = _xdg_glob_list_append (glob_hash->full_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
610 #ifdef NOT_USED_IN_GIO
613 _xdg_glob_hash_dump (XdgGlobHash *glob_hash)
616 printf ("LITERAL STRINGS\n");
617 if (!glob_hash || glob_hash->literal_list == NULL)
623 for (list = glob_hash->literal_list; list; list = list->next)
624 printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
626 printf ("\nSIMPLE GLOBS\n");
627 if (!glob_hash || glob_hash->simple_node == NULL)
633 _xdg_glob_hash_node_dump (glob_hash->simple_node, 4);
636 printf ("\nFULL GLOBS\n");
637 if (!glob_hash || glob_hash->full_list == NULL)
643 for (list = glob_hash->full_list; list; list = list->next)
644 printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
651 _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
652 const char *file_name,
659 glob_file = fopen (file_name, "r");
661 if (glob_file == NULL)
664 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
666 while (fgets (line, 255, glob_file) != NULL)
669 char *mimetype, *glob, *end;
673 if (line[0] == '#' || line[0] == 0)
676 end = line + strlen(line) - 1;
683 colon = strchr (p, ':');
693 colon = strchr (p, ':');
701 case_sensitive = FALSE;
703 colon = strchr (p, ':');
704 if (version_two && colon != NULL)
712 /* Flags end at next colon */
713 colon = strchr (p, ':');
717 flag = strstr (p, "cs");
719 /* Start or after comma */
722 /* ends with comma or end of string */
725 case_sensitive = TRUE;
728 _xdg_glob_hash_append_glob (glob_hash, glob, mimetype, weight, case_sensitive);