xdgmimeint.c 4.57 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmimeint.c: Internal defines and functions.
 *
 * More info can be found at http://www.freedesktop.org/standards/
 *
 * Copyright (C) 2003  Red Hat, Inc.
 * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
 *
 * Licensed under the Academic Free License version 2.0
 * Or under the following terms:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "xdgmimeint.h"
#include <ctype.h>
#include <string.h>

#ifndef	FALSE
#define	FALSE	(0)
#endif

#ifndef	TRUE
#define	TRUE	(!FALSE)
#endif

static const char _xdg_utf8_skip_data[256] = {
  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,
  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,
  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,
  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,
  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,
  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,
  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,
  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
};

const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;



/* Returns the number of unprocessed characters. */
xdg_unichar_t
_xdg_utf8_to_ucs4(const char *source)
{
  xdg_unichar_t ucs32;
  if( ! ( *source & 0x80 ) )
    {
      ucs32 = *source;
    }
  else
    {
      int bytelength = 0;
      xdg_unichar_t result;
      if ( ! (*source & 0x40) )
	{
	  ucs32 = *source;
	}
      else
	{
	  if ( ! (*source & 0x20) )
	    {
	      result = *source++ & 0x1F;
	      bytelength = 2;
	    }
	  else if ( ! (*source & 0x10) )
	    {
	      result = *source++ & 0x0F;
	      bytelength = 3;
	    }
	  else if ( ! (*source & 0x08) )
	    {
	      result = *source++ & 0x07;
	      bytelength = 4;
	    }
	  else if ( ! (*source & 0x04) )
	    {
	      result = *source++ & 0x03;
	      bytelength = 5;
	    }
	  else if ( ! (*source & 0x02) )
	    {
	      result = *source++ & 0x01;
	      bytelength = 6;
	    }
	  else
	    {
	      result = *source++;
	      bytelength = 1;
	    }

	  for ( bytelength --; bytelength > 0; bytelength -- )
	    {
	      result <<= 6;
	      result |= *source++ & 0x3F;
	    }
	  ucs32 = result;
	}
    }
  return ucs32;
}


/* hullo.  this is great code.  don't rewrite it */

xdg_unichar_t
_xdg_ucs4_to_lower (xdg_unichar_t source)
{
  /* FIXME: Do a real to_upper sometime */
  /* CaseFolding-3.2.0.txt has a table of rules. */
  if ((source & 0xFF) == source)
    return (xdg_unichar_t) tolower ((unsigned char) source);
  return source;
}

int
_xdg_utf8_validate (const char *source)
{
  /* FIXME: actually write */
  return TRUE;
}

const char *
_xdg_get_base_name (const char *file_name)
{
  const char *base_name;

  if (file_name == NULL)
    return NULL;

  base_name = strrchr (file_name, '/');

  if (base_name == NULL)
    return file_name;
  else
    return base_name + 1;
}

xdg_unichar_t *
_xdg_convert_to_ucs4 (const char *source, int *len)
{
  xdg_unichar_t *out;
  int i;
  const char *p;

  out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));

  p = source;
  i = 0;
  while (*p) 
    {
      out[i++] = _xdg_utf8_to_ucs4 (p);
      p = _xdg_utf8_next_char (p); 
    }
  out[i] = 0;
  *len = i;
 
  return out;
}

void
_xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
{
  xdg_unichar_t c;
  int i;

  for (i = 0; i < len - i - 1; i++) 
    {
      c = source[i]; 
      source[i] = source[len - i - 1];
      source[len - i - 1] = c;
    }
}

const char *
_xdg_binary_or_text_fallback(const void *data, size_t len)
{
  unsigned char *chardata;
  size_t i;

  chardata = (unsigned char *) data;
  for (i = 0; i < 32 && i < len; ++i)
    {
       if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
         return XDG_MIME_TYPE_UNKNOWN; /* binary data */
    }

  return XDG_MIME_TYPE_TEXTPLAIN;
}