Blame gio/xdgmime/xdgmimeint.c

Packit ae235b
/* -*- mode: C; c-file-style: "gnu" -*- */
Packit ae235b
/* xdgmimeint.c: Internal defines and functions.
Packit ae235b
 *
Packit ae235b
 * More info can be found at http://www.freedesktop.org/standards/
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2003  Red Hat, Inc.
Packit ae235b
 * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
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 "xdgmimeint.h"
Packit ae235b
#include <ctype.h>
Packit ae235b
#include <string.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
static const char _xdg_utf8_skip_data[256] = {
Packit ae235b
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
Packit ae235b
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
Packit ae235b
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
Packit ae235b
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
Packit ae235b
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
Packit ae235b
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
Packit ae235b
  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
Packit ae235b
  3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
Packit ae235b
};
Packit ae235b
Packit ae235b
const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/* Returns the number of unprocessed characters. */
Packit ae235b
xdg_unichar_t
Packit ae235b
_xdg_utf8_to_ucs4(const char *source)
Packit ae235b
{
Packit ae235b
  xdg_unichar_t ucs32;
Packit ae235b
  if( ! ( *source & 0x80 ) )
Packit ae235b
    {
Packit ae235b
      ucs32 = *source;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      int bytelength = 0;
Packit ae235b
      xdg_unichar_t result;
Packit ae235b
      if ( ! (*source & 0x40) )
Packit ae235b
	{
Packit ae235b
	  ucs32 = *source;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  if ( ! (*source & 0x20) )
Packit ae235b
	    {
Packit ae235b
	      result = *source++ & 0x1F;
Packit ae235b
	      bytelength = 2;
Packit ae235b
	    }
Packit ae235b
	  else if ( ! (*source & 0x10) )
Packit ae235b
	    {
Packit ae235b
	      result = *source++ & 0x0F;
Packit ae235b
	      bytelength = 3;
Packit ae235b
	    }
Packit ae235b
	  else if ( ! (*source & 0x08) )
Packit ae235b
	    {
Packit ae235b
	      result = *source++ & 0x07;
Packit ae235b
	      bytelength = 4;
Packit ae235b
	    }
Packit ae235b
	  else if ( ! (*source & 0x04) )
Packit ae235b
	    {
Packit ae235b
	      result = *source++ & 0x03;
Packit ae235b
	      bytelength = 5;
Packit ae235b
	    }
Packit ae235b
	  else if ( ! (*source & 0x02) )
Packit ae235b
	    {
Packit ae235b
	      result = *source++ & 0x01;
Packit ae235b
	      bytelength = 6;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      result = *source++;
Packit ae235b
	      bytelength = 1;
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  for ( bytelength --; bytelength > 0; bytelength -- )
Packit ae235b
	    {
Packit ae235b
	      result <<= 6;
Packit ae235b
	      result |= *source++ & 0x3F;
Packit ae235b
	    }
Packit ae235b
	  ucs32 = result;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  return ucs32;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/* hullo.  this is great code.  don't rewrite it */
Packit ae235b
Packit ae235b
xdg_unichar_t
Packit ae235b
_xdg_ucs4_to_lower (xdg_unichar_t source)
Packit ae235b
{
Packit ae235b
  /* FIXME: Do a real to_upper sometime */
Packit ae235b
  /* CaseFolding-3.2.0.txt has a table of rules. */
Packit ae235b
  if ((source & 0xFF) == source)
Packit ae235b
    return (xdg_unichar_t) tolower ((unsigned char) source);
Packit ae235b
  return source;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
_xdg_utf8_validate (const char *source)
Packit ae235b
{
Packit ae235b
  /* FIXME: actually write */
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
const char *
Packit ae235b
_xdg_get_base_name (const char *file_name)
Packit ae235b
{
Packit ae235b
  const char *base_name;
Packit ae235b
Packit ae235b
  if (file_name == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  base_name = strrchr (file_name, '/');
Packit ae235b
Packit ae235b
  if (base_name == NULL)
Packit ae235b
    return file_name;
Packit ae235b
  else
Packit ae235b
    return base_name + 1;
Packit ae235b
}
Packit ae235b
Packit ae235b
xdg_unichar_t *
Packit ae235b
_xdg_convert_to_ucs4 (const char *source, int *len)
Packit ae235b
{
Packit ae235b
  xdg_unichar_t *out;
Packit ae235b
  int i;
Packit ae235b
  const char *p;
Packit ae235b
Packit ae235b
  out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
Packit ae235b
Packit ae235b
  p = source;
Packit ae235b
  i = 0;
Packit ae235b
  while (*p) 
Packit ae235b
    {
Packit ae235b
      out[i++] = _xdg_utf8_to_ucs4 (p);
Packit ae235b
      p = _xdg_utf8_next_char (p); 
Packit ae235b
    }
Packit ae235b
  out[i] = 0;
Packit ae235b
  *len = i;
Packit ae235b
 
Packit ae235b
  return out;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
Packit ae235b
{
Packit ae235b
  xdg_unichar_t c;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  for (i = 0; i < len - i - 1; i++) 
Packit ae235b
    {
Packit ae235b
      c = source[i]; 
Packit ae235b
      source[i] = source[len - i - 1];
Packit ae235b
      source[len - i - 1] = c;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b