Back to index

libcitadel  8.12
xdgmimeint.c
Go to the documentation of this file.
00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* xdgmimeint.c: Internal defines and functions.
00003  *
00004  * More info can be found at http://www.freedesktop.org/standards/
00005  *
00006  * Copyright (C) 2003  Red Hat, Inc.
00007  * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
00008  *
00009  * Licensed under the Academic Free License version 2.0
00010  * Or under the following terms:
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with this library; if not, write to the
00024  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00025  * Boston, MA 02111-1307, USA.
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include "xdgmimeint.h"
00033 #include <ctype.h>
00034 #include <string.h>
00035 
00036 #ifndef       FALSE
00037 #define       FALSE  (0)
00038 #endif
00039 
00040 #ifndef       TRUE
00041 #define       TRUE   (!FALSE)
00042 #endif
00043 
00044 static const char _xdg_utf8_skip_data[256] = {
00045   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,
00046   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,
00047   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,
00048   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,
00049   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,
00050   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,
00051   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,
00052   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
00053 };
00054 
00055 const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
00056 
00057 
00058 
00059 /* Returns the number of unprocessed characters. */
00060 xdg_unichar_t
00061 _xdg_utf8_to_ucs4(const char *source)
00062 {
00063   xdg_unichar_t ucs32;
00064   if( ! ( *source & 0x80 ) )
00065     {
00066       ucs32 = *source;
00067     }
00068   else
00069     {
00070       int bytelength = 0;
00071       xdg_unichar_t result;
00072       if ( ! (*source & 0x40) )
00073        {
00074          ucs32 = *source;
00075        }
00076       else
00077        {
00078          if ( ! (*source & 0x20) )
00079            {
00080              result = *source++ & 0x1F;
00081              bytelength = 2;
00082            }
00083          else if ( ! (*source & 0x10) )
00084            {
00085              result = *source++ & 0x0F;
00086              bytelength = 3;
00087            }
00088          else if ( ! (*source & 0x08) )
00089            {
00090              result = *source++ & 0x07;
00091              bytelength = 4;
00092            }
00093          else if ( ! (*source & 0x04) )
00094            {
00095              result = *source++ & 0x03;
00096              bytelength = 5;
00097            }
00098          else if ( ! (*source & 0x02) )
00099            {
00100              result = *source++ & 0x01;
00101              bytelength = 6;
00102            }
00103          else
00104            {
00105              result = *source++;
00106              bytelength = 1;
00107            }
00108 
00109          for ( bytelength --; bytelength > 0; bytelength -- )
00110            {
00111              result <<= 6;
00112              result |= *source++ & 0x3F;
00113            }
00114          ucs32 = result;
00115        }
00116     }
00117   return ucs32;
00118 }
00119 
00120 
00121 /* hullo.  this is great code.  don't rewrite it */
00122 
00123 xdg_unichar_t
00124 _xdg_ucs4_to_lower (xdg_unichar_t source)
00125 {
00126   /* FIXME: Do a real to_upper sometime */
00127   /* CaseFolding-3.2.0.txt has a table of rules. */
00128   if ((source & 0xFF) == source)
00129     return (xdg_unichar_t) tolower ((unsigned char) source);
00130   return source;
00131 }
00132 
00133 int
00134 _xdg_utf8_validate (const char *source)
00135 {
00136   /* FIXME: actually write */
00137   return TRUE;
00138 }
00139 
00140 const char *
00141 _xdg_get_base_name (const char *file_name)
00142 {
00143   const char *base_name;
00144 
00145   if (file_name == NULL)
00146     return NULL;
00147 
00148   base_name = strrchr (file_name, '/');
00149 
00150   if (base_name == NULL)
00151     return file_name;
00152   else
00153     return base_name + 1;
00154 }