Back to index

libcitadel  8.12
Classes | Typedefs | Functions
xdgmimeparent.c File Reference
#include "xdgmimeparent.h"
#include "xdgmimeint.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <fnmatch.h>

Go to the source code of this file.

Classes

struct  XdgMimeParents
struct  XdgParentList

Typedefs

typedef struct XdgMimeParents

Functions

XdgParentList_xdg_mime_parent_list_new (void)
void _xdg_mime_parent_list_free (XdgParentList *list)
static int parent_entry_cmp (const void *v1, const void *v2)
const char ** _xdg_mime_parent_list_lookup (XdgParentList *list, const char *mime)
void _xdg_mime_parent_read_from_file (XdgParentList *list, const char *file_name)
void _xdg_mime_parent_list_dump (XdgParentList *list)

Class Documentation

struct XdgMimeParents

Definition at line 50 of file xdgmimeparent.c.

Class Members
char * mime
int n_parents
char ** parents
struct XdgParentList

Definition at line 57 of file xdgmimeparent.c.

Collaboration diagram for XdgParentList:
Class Members
int n_mimes
struct XdgMimeParents * parents

Typedef Documentation

typedef struct XdgMimeParents

Definition at line 48 of file xdgmimeparent.c.


Function Documentation

Definition at line 204 of file xdgmimeparent.c.

{
  int i;
  char **p;

  if (list->parents)
    {
      for (i = 0; i < list->n_mimes; i++)
       {
         for (p = list->parents[i].parents; *p; p++)
           printf ("%s %s\n", list->parents[i].mime, *p);
       }
    }
}

Here is the caller graph for this function:

Definition at line 77 of file xdgmimeparent.c.

{
  int i;
  char **p;

  if (list->parents)
    {
      for (i = 0; i < list->n_mimes; i++)
       {
         for (p = list->parents[i].parents; *p; p++)
           free (*p);

         free (list->parents[i].parents);
         free (list->parents[i].mime);
       }
      free (list->parents);
    }
  free (list);
}

Here is the caller graph for this function:

const char** _xdg_mime_parent_list_lookup ( XdgParentList list,
const char *  mime 
)

Definition at line 104 of file xdgmimeparent.c.

{
  XdgMimeParents *entry;
  XdgMimeParents key;

  if (list->n_mimes > 0)
    {
      key.mime = (char *)mime;
      key.parents = NULL;

      entry = bsearch (&key, list->parents, list->n_mimes,
                     sizeof (XdgMimeParents), &parent_entry_cmp);
      if (entry)
        return (const char **)entry->parents;
    }

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 64 of file xdgmimeparent.c.

{
  XdgParentList *list;

  list = malloc (sizeof (XdgParentList));

  list->parents = NULL;
  list->n_mimes = 0;

  return list;
}

Here is the caller graph for this function:

void _xdg_mime_parent_read_from_file ( XdgParentList list,
const char *  file_name 
)

Definition at line 125 of file xdgmimeparent.c.

{
  FILE *file;
  char line[255];
  int i, alloc;
  XdgMimeParents *entry;

  file = fopen (file_name, "r");

  if (file == NULL)
    return;

  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
   * Blah */
  alloc = list->n_mimes + 16;
  list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
  while (fgets (line, 255, file) != NULL)
    {
      char *sep;
      if (line[0] == '#')
       continue;

      sep = strchr (line, ' ');
      if (sep == NULL)
       continue;
      *(sep++) = '\000';
      sep[strlen (sep) -1] = '\000';
      entry = NULL;
      for (i = 0; i < list->n_mimes; i++)
       {
         if (strcmp (list->parents[i].mime, line) == 0)
           {
             entry = &(list->parents[i]);
             break;
           }
       }
      
      if (!entry)
       {
         if (list->n_mimes == alloc)
           {
             alloc <<= 1;
             list->parents = realloc (list->parents, 
                                   alloc * sizeof (XdgMimeParents));
           }
         list->parents[list->n_mimes].mime = strdup (line);
         list->parents[list->n_mimes].parents = NULL;
         entry = &(list->parents[list->n_mimes]);
         list->n_mimes++;
       }

      if (!entry->parents)
       {
         entry->n_parents = 1;
         entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
       }
      else
       {
         entry->n_parents += 1;
         entry->parents = realloc (entry->parents, 
                                (entry->n_parents + 2) * sizeof (char *));
       }
      entry->parents[entry->n_parents - 1] = strdup (sep);
      entry->parents[entry->n_parents] = NULL;
    }

  list->parents = realloc (list->parents, 
                        list->n_mimes * sizeof (XdgMimeParents));

  fclose (file);  
  
  if (list->n_mimes > 1)
    qsort (list->parents, list->n_mimes, 
           sizeof (XdgMimeParents), &parent_entry_cmp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int parent_entry_cmp ( const void *  v1,
const void *  v2 
) [static]

Definition at line 98 of file xdgmimeparent.c.

{
  return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
}

Here is the caller graph for this function: