GDBusInterfaceVTable: clarify memory handling for the method() virtual function
[platform/upstream/glib.git] / gio / xdgmime / xdgmimeint.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimeint.c: Internal defines and functions.
3  *
4  * More info can be found at http://www.freedesktop.org/standards/
5  *
6  * Copyright (C) 2003  Red Hat, Inc.
7  * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
8  *
9  * Licensed under the Academic Free License version 2.0
10  * Or under the following terms:
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "xdgmimeint.h"
31 #include <ctype.h>
32 #include <string.h>
33
34 #ifndef FALSE
35 #define FALSE   (0)
36 #endif
37
38 #ifndef TRUE
39 #define TRUE    (!FALSE)
40 #endif
41
42 static const char _xdg_utf8_skip_data[256] = {
43   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
44   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
45   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
46   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
47   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
48   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
49   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,
50   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
51 };
52
53 const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
54
55
56
57 /* Returns the number of unprocessed characters. */
58 xdg_unichar_t
59 _xdg_utf8_to_ucs4(const char *source)
60 {
61   xdg_unichar_t ucs32;
62   if( ! ( *source & 0x80 ) )
63     {
64       ucs32 = *source;
65     }
66   else
67     {
68       int bytelength = 0;
69       xdg_unichar_t result;
70       if ( ! (*source & 0x40) )
71         {
72           ucs32 = *source;
73         }
74       else
75         {
76           if ( ! (*source & 0x20) )
77             {
78               result = *source++ & 0x1F;
79               bytelength = 2;
80             }
81           else if ( ! (*source & 0x10) )
82             {
83               result = *source++ & 0x0F;
84               bytelength = 3;
85             }
86           else if ( ! (*source & 0x08) )
87             {
88               result = *source++ & 0x07;
89               bytelength = 4;
90             }
91           else if ( ! (*source & 0x04) )
92             {
93               result = *source++ & 0x03;
94               bytelength = 5;
95             }
96           else if ( ! (*source & 0x02) )
97             {
98               result = *source++ & 0x01;
99               bytelength = 6;
100             }
101           else
102             {
103               result = *source++;
104               bytelength = 1;
105             }
106
107           for ( bytelength --; bytelength > 0; bytelength -- )
108             {
109               result <<= 6;
110               result |= *source++ & 0x3F;
111             }
112           ucs32 = result;
113         }
114     }
115   return ucs32;
116 }
117
118
119 /* hullo.  this is great code.  don't rewrite it */
120
121 xdg_unichar_t
122 _xdg_ucs4_to_lower (xdg_unichar_t source)
123 {
124   /* FIXME: Do a real to_upper sometime */
125   /* CaseFolding-3.2.0.txt has a table of rules. */
126   if ((source & 0xFF) == source)
127     return (xdg_unichar_t) tolower ((unsigned char) source);
128   return source;
129 }
130
131 int
132 _xdg_utf8_validate (const char *source)
133 {
134   /* FIXME: actually write */
135   return TRUE;
136 }
137
138 const char *
139 _xdg_get_base_name (const char *file_name)
140 {
141   const char *base_name;
142
143   if (file_name == NULL)
144     return NULL;
145
146   base_name = strrchr (file_name, '/');
147
148   if (base_name == NULL)
149     return file_name;
150   else
151     return base_name + 1;
152 }
153
154 xdg_unichar_t *
155 _xdg_convert_to_ucs4 (const char *source, int *len)
156 {
157   xdg_unichar_t *out;
158   int i;
159   const char *p;
160
161   out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
162
163   p = source;
164   i = 0;
165   while (*p) 
166     {
167       out[i++] = _xdg_utf8_to_ucs4 (p);
168       p = _xdg_utf8_next_char (p); 
169     }
170   out[i] = 0;
171   *len = i;
172  
173   return out;
174 }
175
176 void
177 _xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
178 {
179   xdg_unichar_t c;
180   int i;
181
182   for (i = 0; i < len - i - 1; i++) 
183     {
184       c = source[i]; 
185       source[i] = source[len - i - 1];
186       source[len - i - 1] = c;
187     }
188 }
189