int _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash, const char *file_name, const char *mime_types[], int n_mime_types) { XdgGlobList *list; int i, n; MimeWeight mimes[10]; int n_mimes = 10; xdg_unichar_t *ucs4; int len; /* First, check the literals */ assert (file_name != NULL && n_mime_types > 0); n = 0; for (list = glob_hash->literal_list; list; list = list->next) { if (strcmp ((const char *)list->data, file_name) == 0) { mime_types[0] = list->mime_type; return 1; } } ucs4 = _xdg_convert_to_ucs4 (file_name, &len); n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ucs4, len, FALSE, mimes, n_mimes); if (n == 0) n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ucs4, len, TRUE, mimes, n_mimes); free(ucs4); /* FIXME: Not UTF-8 safe */ if (n == 0) { for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next) { if (fnmatch ((const char *)list->data, file_name, 0) == 0) { mimes[n].mime = list->mime_type; mimes[n].weight = list->weight; n++; } } } qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight); if (n_mime_types < n) n = n_mime_types; for (i = 0; i < n; i++) mime_types[i] = mimes[i].mime; return n; }
/* glob must be valid UTF-8 */ static XdgGlobHashNode * _xdg_glob_hash_insert_text (XdgGlobHashNode * glob_hash_node, const char *text, const char *mime_type, int weight) { XdgGlobHashNode *node; xdg_unichar_t *unitext; int len; unitext = _xdg_convert_to_ucs4 (text, &len); _xdg_reverse_ucs4 (unitext, len); node = _xdg_glob_hash_insert_ucs4 (glob_hash_node, unitext, mime_type, weight); free (unitext); return node; }
static int cache_glob_lookup_file_name (const char *file_name, const char *mime_types[], int n_mime_types) { int n; MimeWeight mimes[10]; int n_mimes = 10; int i; xdg_unichar_t *ucs4; int len; assert (file_name != NULL && n_mime_types > 0); /* First, check the literals */ n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types); if (n > 0) return n; ucs4 = _xdg_convert_to_ucs4 (file_name, &len); n = cache_glob_lookup_suffix (ucs4, len, FALSE, mimes, n_mimes); if (n == 0) n = cache_glob_lookup_suffix (ucs4, len, TRUE, mimes, n_mimes); free(ucs4); /* Last, try fnmatch */ if (n == 0) n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes); qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight); if (n_mime_types < n) n = n_mime_types; for (i = 0; i < n; i++) mime_types[i] = mimes[i].mime; return n; }