Blame gio/xdgmime/xdgmimeparent.c

Packit ae235b
/* -*- mode: C; c-file-style: "gnu" -*- */
Packit ae235b
/* xdgmimealias.c: Private file.  Datastructure for storing the hierarchy.
Packit ae235b
 *
Packit ae235b
 * More info can be found at http://www.freedesktop.org/standards/
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2004  Red Hat, Inc.
Packit ae235b
 * Copyright (C) 2004  Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 *
Packit ae235b
 * Licensed under the Academic Free License version 2.0
Packit ae235b
 * Or under the following terms:
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "xdgmimeparent.h"
Packit ae235b
#include "xdgmimeint.h"
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <assert.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <fnmatch.h>
Packit ae235b
Packit ae235b
#ifndef	FALSE
Packit ae235b
#define	FALSE	(0)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef	TRUE
Packit ae235b
#define	TRUE	(!FALSE)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
typedef struct XdgMimeParents XdgMimeParents;
Packit ae235b
Packit ae235b
struct XdgMimeParents
Packit ae235b
{
Packit ae235b
  char *mime;
Packit ae235b
  char **parents;
Packit ae235b
  int n_parents;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct XdgParentList
Packit ae235b
{
Packit ae235b
  struct XdgMimeParents *parents;
Packit ae235b
  int n_mimes;
Packit ae235b
};
Packit ae235b
Packit ae235b
XdgParentList *
Packit ae235b
_xdg_mime_parent_list_new (void)
Packit ae235b
{
Packit ae235b
  XdgParentList *list;
Packit ae235b
Packit ae235b
  list = malloc (sizeof (XdgParentList));
Packit ae235b
Packit ae235b
  list->parents = NULL;
Packit ae235b
  list->n_mimes = 0;
Packit ae235b
Packit ae235b
  return list;
Packit ae235b
}
Packit ae235b
Packit ae235b
void         
Packit ae235b
_xdg_mime_parent_list_free (XdgParentList *list)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  char **p;
Packit ae235b
Packit ae235b
  if (list->parents)
Packit ae235b
    {
Packit ae235b
      for (i = 0; i < list->n_mimes; i++)
Packit ae235b
	{
Packit ae235b
	  for (p = list->parents[i].parents; *p; p++)
Packit ae235b
	    free (*p);
Packit ae235b
Packit ae235b
	  free (list->parents[i].parents);
Packit ae235b
	  free (list->parents[i].mime);
Packit ae235b
	}
Packit ae235b
      free (list->parents);
Packit ae235b
    }
Packit ae235b
  free (list);
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
parent_entry_cmp (const void *v1, const void *v2)
Packit ae235b
{
Packit ae235b
  return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime);
Packit ae235b
}
Packit ae235b
Packit ae235b
const char **
Packit ae235b
_xdg_mime_parent_list_lookup (XdgParentList *list,
Packit ae235b
			      const char    *mime)
Packit ae235b
{
Packit ae235b
  XdgMimeParents *entry;
Packit ae235b
  XdgMimeParents key;
Packit ae235b
Packit ae235b
  if (list->n_mimes > 0)
Packit ae235b
    {
Packit ae235b
      key.mime = (char *)mime;
Packit ae235b
      key.parents = NULL;
Packit ae235b
      key.n_parents = 0;
Packit ae235b
Packit ae235b
      entry = bsearch (&key, list->parents, list->n_mimes,
Packit ae235b
		       sizeof (XdgMimeParents), &parent_entry_cmp);
Packit ae235b
      if (entry)
Packit ae235b
        return (const char **)entry->parents;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_xdg_mime_parent_read_from_file (XdgParentList *list,
Packit ae235b
				 const char    *file_name)
Packit ae235b
{
Packit ae235b
  FILE *file;
Packit ae235b
  char line[255];
Packit ae235b
  int i, alloc;
Packit ae235b
  XdgMimeParents *entry;
Packit ae235b
Packit ae235b
  file = fopen (file_name, "r");
Packit ae235b
Packit ae235b
  if (file == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  /* FIXME: Not UTF-8 safe.  Doesn't work if lines are greater than 255 chars.
Packit ae235b
   * Blah */
Packit ae235b
  alloc = list->n_mimes + 16;
Packit ae235b
  list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents));
Packit ae235b
  while (fgets (line, 255, file) != NULL)
Packit ae235b
    {
Packit ae235b
      char *sep;
Packit ae235b
      if (line[0] == '#')
Packit ae235b
	continue;
Packit ae235b
Packit ae235b
      sep = strchr (line, ' ');
Packit ae235b
      if (sep == NULL)
Packit ae235b
	continue;
Packit ae235b
      *(sep++) = '\000';
Packit ae235b
      sep[strlen (sep) -1] = '\000';
Packit ae235b
      entry = NULL;
Packit ae235b
      for (i = 0; i < list->n_mimes; i++)
Packit ae235b
	{
Packit ae235b
	  if (strcmp (list->parents[i].mime, line) == 0)
Packit ae235b
	    {
Packit ae235b
	      entry = &(list->parents[i]);
Packit ae235b
	      break;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (!entry)
Packit ae235b
	{
Packit ae235b
	  if (list->n_mimes == alloc)
Packit ae235b
	    {
Packit ae235b
	      alloc <<= 1;
Packit ae235b
	      list->parents = realloc (list->parents, 
Packit ae235b
				       alloc * sizeof (XdgMimeParents));
Packit ae235b
	    }
Packit ae235b
	  list->parents[list->n_mimes].mime = strdup (line);
Packit ae235b
	  list->parents[list->n_mimes].parents = NULL;
Packit ae235b
	  entry = &(list->parents[list->n_mimes]);
Packit ae235b
	  list->n_mimes++;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (!entry->parents)
Packit ae235b
	{
Packit ae235b
	  entry->n_parents = 1;
Packit ae235b
	  entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *));
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  entry->n_parents += 1;
Packit ae235b
	  entry->parents = realloc (entry->parents, 
Packit ae235b
				    (entry->n_parents + 2) * sizeof (char *));
Packit ae235b
	}
Packit ae235b
      entry->parents[entry->n_parents - 1] = strdup (sep);
Packit ae235b
      entry->parents[entry->n_parents] = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  list->parents = realloc (list->parents, 
Packit ae235b
			   list->n_mimes * sizeof (XdgMimeParents));
Packit ae235b
Packit ae235b
  fclose (file);  
Packit ae235b
  
Packit ae235b
  if (list->n_mimes > 1)
Packit ae235b
    qsort (list->parents, list->n_mimes, 
Packit ae235b
           sizeof (XdgMimeParents), &parent_entry_cmp);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef NOT_USED_IN_GIO
Packit ae235b
Packit ae235b
void         
Packit ae235b
_xdg_mime_parent_list_dump (XdgParentList *list)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  char **p;
Packit ae235b
Packit ae235b
  if (list->parents)
Packit ae235b
    {
Packit ae235b
      for (i = 0; i < list->n_mimes; i++)
Packit ae235b
	{
Packit ae235b
	  for (p = list->parents[i].parents; *p; p++)
Packit ae235b
	    printf ("%s %s\n", list->parents[i].mime, *p);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b