libxputty 0.1
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
xdgmimeglob.c File Reference

Go to the source code of this file.

Data Structures

struct  XdgGlobHashNode
 
struct  XdgGlobList
 
struct  XdgGlobHash
 
struct  MimeWeight
 

Typedefs

typedef struct XdgGlobHashNode XdgGlobHashNode
 
typedef struct XdgGlobList XdgGlobList
 

Functions

int _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash, const char *file_name, const char *mime_types[], int n_mime_types)
 
XdgGlobHash_xdg_glob_hash_new (void)
 
void _xdg_glob_hash_free (XdgGlobHash *glob_hash)
 
XdgGlobType _xdg_glob_determine_type (const char *glob)
 
void _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash, const char *glob, const char *mime_type, int weight, int case_sensitive)
 
void _xdg_glob_hash_dump (XdgGlobHash *glob_hash)
 
void _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash, const char *file_name, int version_two)
 

Typedef Documentation

◆ XdgGlobHashNode

Definition at line 48 of file xdgmimeglob.c.

◆ XdgGlobList

typedef struct XdgGlobList XdgGlobList

Definition at line 49 of file xdgmimeglob.c.

Function Documentation

◆ _xdg_glob_determine_type()

XdgGlobType _xdg_glob_determine_type ( const char *  glob)

Definition at line 519 of file xdgmimeglob.c.

520{
521 const char *ptr;
522 int maybe_in_simple_glob = FALSE;
523 int first_char = TRUE;
524
525 ptr = glob;
526
527 while (*ptr != '\0')
528 {
529 if (*ptr == '*' && first_char)
530 maybe_in_simple_glob = TRUE;
531 else if (*ptr == '\\' || *ptr == '[' || *ptr == '?' || *ptr == '*')
532 return XDG_GLOB_FULL;
533
534 first_char = FALSE;
535 ptr = _xdg_utf8_next_char (ptr);
536 }
537 if (maybe_in_simple_glob)
538 return XDG_GLOB_SIMPLE;
539 else
540 return XDG_GLOB_LITERAL;
541}
@ XDG_GLOB_SIMPLE
Definition xdgmimeglob.h:38
@ XDG_GLOB_FULL
Definition xdgmimeglob.h:39
@ XDG_GLOB_LITERAL
Definition xdgmimeglob.h:37

References XDG_GLOB_FULL, XDG_GLOB_LITERAL, and XDG_GLOB_SIMPLE.

Referenced by _xdg_glob_hash_append_glob().

◆ _xdg_glob_hash_append_glob()

void _xdg_glob_hash_append_glob ( XdgGlobHash glob_hash,
const char *  glob,
const char *  mime_type,
int  weight,
int  case_sensitive 
)

Definition at line 545 of file xdgmimeglob.c.

550{
551 XdgGlobType type;
552
553 assert (glob_hash != NULL);
554 assert (glob != NULL);
555
556 type = _xdg_glob_determine_type (glob);
557
558 switch (type)
559 {
560 case XDG_GLOB_LITERAL:
561 glob_hash->literal_list = _xdg_glob_list_append (glob_hash->literal_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
562 break;
563 case XDG_GLOB_SIMPLE:
564 glob_hash->simple_node = _xdg_glob_hash_insert_text (glob_hash->simple_node, glob + 1, mime_type, weight, case_sensitive);
565 break;
566 case XDG_GLOB_FULL:
567 glob_hash->full_list = _xdg_glob_list_append (glob_hash->full_list, strdup (glob), strdup (mime_type), weight, case_sensitive);
568 break;
569 }
570}
XdgGlobList * full_list
Definition xdgmimeglob.c:73
XdgGlobList * literal_list
Definition xdgmimeglob.c:71
XdgGlobHashNode * simple_node
Definition xdgmimeglob.c:72
XdgGlobType _xdg_glob_determine_type(const char *glob)
XdgGlobType
Definition xdgmimeglob.h:36

References _xdg_glob_determine_type(), XdgGlobHash::full_list, XdgGlobHash::literal_list, XdgGlobHash::simple_node, XDG_GLOB_FULL, XDG_GLOB_LITERAL, and XDG_GLOB_SIMPLE.

Referenced by _xdg_mime_glob_read_from_file().

◆ _xdg_glob_hash_dump()

void _xdg_glob_hash_dump ( XdgGlobHash glob_hash)

Definition at line 573 of file xdgmimeglob.c.

574{
575 XdgGlobList *list;
576 printf ("LITERAL STRINGS\n");
577 if (!glob_hash || glob_hash->literal_list == NULL)
578 {
579 printf (" None\n");
580 }
581 else
582 {
583 for (list = glob_hash->literal_list; list; list = list->next)
584 printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
585 }
586 printf ("\nSIMPLE GLOBS\n");
587 if (!glob_hash || glob_hash->simple_node == NULL)
588 {
589 printf (" None\n");
590 }
591 else
592 {
593 _xdg_glob_hash_node_dump (glob_hash->simple_node, 4);
594 }
595
596 printf ("\nFULL GLOBS\n");
597 if (!glob_hash || glob_hash->full_list == NULL)
598 {
599 printf (" None\n");
600 }
601 else
602 {
603 for (list = glob_hash->full_list; list; list = list->next)
604 printf (" %s - %s %d\n", (char *)list->data, list->mime_type, list->weight);
605 }
606}
const char * mime_type
Definition xdgmimeglob.c:63
XdgGlobList * next
Definition xdgmimeglob.c:66
const char * data
Definition xdgmimeglob.c:62

References XdgGlobList::data, XdgGlobHash::full_list, XdgGlobHash::literal_list, XdgGlobList::mime_type, XdgGlobList::next, XdgGlobHash::simple_node, and XdgGlobList::weight.

Referenced by xdg_mime_dump().

◆ _xdg_glob_hash_free()

void _xdg_glob_hash_free ( XdgGlobHash glob_hash)

Definition at line 510 of file xdgmimeglob.c.

511{
512 _xdg_glob_list_free (glob_hash->literal_list);
513 _xdg_glob_list_free (glob_hash->full_list);
514 _xdg_glob_hash_free_nodes (glob_hash->simple_node);
515 free (glob_hash);
516}

References XdgGlobHash::full_list, XdgGlobHash::literal_list, and XdgGlobHash::simple_node.

Referenced by xdg_mime_shutdown().

◆ _xdg_glob_hash_lookup_file_name()

int _xdg_glob_hash_lookup_file_name ( XdgGlobHash glob_hash,
const char *  file_name,
const char *  mime_types[],
int  n_mime_types 
)

Definition at line 404 of file xdgmimeglob.c.

408{
409 XdgGlobList *list;
410 int i, n;
411 MimeWeight mimes[10];
412 int n_mimes = 10;
413 int len;
414 char *lower_case;
415
416 /* First, check the literals */
417
418 assert (file_name != NULL && n_mime_types > 0);
419
420 n = 0;
421
422 lower_case = ascii_tolower (file_name);
423
424 for (list = glob_hash->literal_list; list; list = list->next)
425 {
426 if (strcmp ((const char *)list->data, file_name) == 0)
427 {
428 mime_types[0] = list->mime_type;
429 free (lower_case);
430 return 1;
431 }
432 }
433
434 for (list = glob_hash->literal_list; list; list = list->next)
435 {
436 if (!list->case_sensitive &&
437 strcmp ((const char *)list->data, lower_case) == 0)
438 {
439 mime_types[0] = list->mime_type;
440 free (lower_case);
441 return 1;
442 }
443 }
444
445
446 len = strlen (file_name);
447 n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, lower_case, len, FALSE,
448 mimes, n_mimes);
449 if (n == 0)
450 n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, file_name, len, TRUE,
451 mimes, n_mimes);
452
453 if (n == 0)
454 {
455 for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next)
456 {
457 if (fnmatch ((const char *)list->data, file_name, 0) == 0)
458 {
459 mimes[n].mime = list->mime_type;
460 mimes[n].weight = list->weight;
461 n++;
462 }
463 }
464 }
465 free (lower_case);
466
467 qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
468
469 if (n_mime_types < n)
470 n = n_mime_types;
471
472 for (i = 0; i < n; i++)
473 mime_types[i] = mimes[i].mime;
474
475 return n;
476}
const char * mime
int case_sensitive
Definition xdgmimeglob.c:65

References XdgGlobList::case_sensitive, XdgGlobList::data, XdgGlobHash::full_list, XdgGlobHash::literal_list, MimeWeight::mime, XdgGlobList::mime_type, XdgGlobList::next, XdgGlobHash::simple_node, MimeWeight::weight, and XdgGlobList::weight.

Referenced by xdg_mime_get_mime_type_for_file(), xdg_mime_get_mime_type_from_file_name(), and xdg_mime_get_mime_types_from_file_name().

◆ _xdg_glob_hash_new()

XdgGlobHash * _xdg_glob_hash_new ( void  )

Definition at line 484 of file xdgmimeglob.c.

485{
486 XdgGlobHash *glob_hash;
487
488 glob_hash = calloc (1, sizeof (XdgGlobHash));
489
490 return glob_hash;
491}

◆ _xdg_mime_glob_read_from_file()

void _xdg_mime_glob_read_from_file ( XdgGlobHash glob_hash,
const char *  file_name,
int  version_two 
)

Definition at line 610 of file xdgmimeglob.c.

613{
614 FILE *glob_file;
615 char line[255];
616 char *p;
617
618 glob_file = fopen (file_name, "r");
619
620 if (glob_file == NULL)
621 return;
622
623 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
624 * Blah */
625 while (fgets (line, 255, glob_file) != NULL)
626 {
627 char *colon;
628 char *mimetype, *glob, *end;
629 int weight;
630 int case_sensitive;
631
632 if (line[0] == '#' || line[0] == 0)
633 continue;
634
635 end = line + strlen(line) - 1;
636 if (*end == '\n')
637 *end = 0;
638
639 p = line;
640 if (version_two)
641 {
642 colon = strchr (p, ':');
643 if (colon == NULL)
644 continue;
645 *colon = 0;
646 weight = atoi (p);
647 p = colon + 1;
648 }
649 else
650 weight = 50;
651
652 colon = strchr (p, ':');
653 if (colon == NULL)
654 continue;
655 *colon = 0;
656
657 mimetype = p;
658 p = colon + 1;
659 glob = p;
660 case_sensitive = FALSE;
661
662 colon = strchr (p, ':');
663 if (version_two && colon != NULL)
664 {
665 char *flag;
666
667 /* We got flags */
668 *colon = 0;
669 p = colon + 1;
670
671 /* Flags end at next colon */
672 colon = strchr (p, ':');
673 if (colon != NULL)
674 *colon = 0;
675
676 flag = strstr (p, "cs");
677 if (flag != NULL &&
678 /* Start or after comma */
679 (flag == p ||
680 flag[-1] == ',') &&
681 /* ends with comma or end of string */
682 (flag[2] == 0 ||
683 flag[2] == ','))
684 case_sensitive = TRUE;
685 }
686
687 _xdg_glob_hash_append_glob (glob_hash, glob, mimetype, weight, case_sensitive);
688 }
689
690 fclose (glob_file);
691}
void _xdg_glob_hash_append_glob(XdgGlobHash *glob_hash, const char *glob, const char *mime_type, int weight, int case_sensitive)

References _xdg_glob_hash_append_glob().