Merge branch 'wip/lantw/gspawn-declare-environ' into 'master'
[glib.git] / gio / xdgmime / xdgmimeint.c
blob35c3635e29c6be80686948362b04105d8c58d771
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimeint.c: Internal defines and functions.
4 * More info can be found at http://www.freedesktop.org/standards/
6 * Copyright (C) 2003 Red Hat, Inc.
7 * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
9 * Licensed under the Academic Free License version 2.0
10 * Or under the following terms:
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.1 of the License, or (at your option) any later version.
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.
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/>.
26 #include "config.h"
28 #include "xdgmimeint.h"
29 #include <ctype.h>
30 #include <string.h>
32 #ifndef FALSE
33 #define FALSE (0)
34 #endif
36 #ifndef TRUE
37 #define TRUE (!FALSE)
38 #endif
40 static const char _xdg_utf8_skip_data[256] = {
41 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
42 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
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 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,
48 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 const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
55 /* Returns the number of unprocessed characters. */
56 xdg_unichar_t
57 _xdg_utf8_to_ucs4(const char *source)
59 xdg_unichar_t ucs32;
60 if( ! ( *source & 0x80 ) )
62 ucs32 = *source;
64 else
66 int bytelength = 0;
67 xdg_unichar_t result;
68 if ( ! (*source & 0x40) )
70 ucs32 = *source;
72 else
74 if ( ! (*source & 0x20) )
76 result = *source++ & 0x1F;
77 bytelength = 2;
79 else if ( ! (*source & 0x10) )
81 result = *source++ & 0x0F;
82 bytelength = 3;
84 else if ( ! (*source & 0x08) )
86 result = *source++ & 0x07;
87 bytelength = 4;
89 else if ( ! (*source & 0x04) )
91 result = *source++ & 0x03;
92 bytelength = 5;
94 else if ( ! (*source & 0x02) )
96 result = *source++ & 0x01;
97 bytelength = 6;
99 else
101 result = *source++;
102 bytelength = 1;
105 for ( bytelength --; bytelength > 0; bytelength -- )
107 result <<= 6;
108 result |= *source++ & 0x3F;
110 ucs32 = result;
113 return ucs32;
117 /* hullo. this is great code. don't rewrite it */
119 xdg_unichar_t
120 _xdg_ucs4_to_lower (xdg_unichar_t source)
122 /* FIXME: Do a real to_upper sometime */
123 /* CaseFolding-3.2.0.txt has a table of rules. */
124 if ((source & 0xFF) == source)
125 return (xdg_unichar_t) tolower ((unsigned char) source);
126 return source;
130 _xdg_utf8_validate (const char *source)
132 /* FIXME: actually write */
133 return TRUE;
136 const char *
137 _xdg_get_base_name (const char *file_name)
139 const char *base_name;
141 if (file_name == NULL)
142 return NULL;
144 base_name = strrchr (file_name, '/');
146 if (base_name == NULL)
147 return file_name;
148 else
149 return base_name + 1;
152 xdg_unichar_t *
153 _xdg_convert_to_ucs4 (const char *source, int *len)
155 xdg_unichar_t *out;
156 int i;
157 const char *p;
159 out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
161 p = source;
162 i = 0;
163 while (*p)
165 out[i++] = _xdg_utf8_to_ucs4 (p);
166 p = _xdg_utf8_next_char (p);
168 out[i] = 0;
169 *len = i;
171 return out;
174 void
175 _xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
177 xdg_unichar_t c;
178 int i;
180 for (i = 0; i < len - i - 1; i++)
182 c = source[i];
183 source[i] = source[len - i - 1];
184 source[len - i - 1] = c;
188 const char *
189 _xdg_binary_or_text_fallback(const void *data, size_t len)
191 unsigned char *chardata;
192 int i;
194 chardata = (unsigned char *) data;
195 for (i = 0; i < 128 && i < len; ++i)
197 if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
198 return XDG_MIME_TYPE_UNKNOWN; /* binary data */
201 return XDG_MIME_TYPE_TEXTPLAIN;