]>
git.proxmox.com Git - libxdgmime-perl.git/blob - xdgmime-source/src/xdgmimeint.c
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 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
16 #include "xdgmimeint.h"
28 static const char _xdg_utf8_skip_data
[256] = {
29 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
30 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
31 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
32 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
33 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
34 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
35 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,
36 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
39 const char * const _xdg_utf8_skip
= _xdg_utf8_skip_data
;
43 /* Returns the number of unprocessed characters. */
45 _xdg_utf8_to_ucs4(const char *source
)
48 if( ! ( *source
& 0x80 ) )
56 if ( ! (*source
& 0x40) )
62 if ( ! (*source
& 0x20) )
64 result
= *source
++ & 0x1F;
67 else if ( ! (*source
& 0x10) )
69 result
= *source
++ & 0x0F;
72 else if ( ! (*source
& 0x08) )
74 result
= *source
++ & 0x07;
77 else if ( ! (*source
& 0x04) )
79 result
= *source
++ & 0x03;
82 else if ( ! (*source
& 0x02) )
84 result
= *source
++ & 0x01;
93 for ( bytelength
--; bytelength
> 0; bytelength
-- )
96 result
|= *source
++ & 0x3F;
105 /* hullo. this is great code. don't rewrite it */
108 _xdg_ucs4_to_lower (xdg_unichar_t source
)
110 /* FIXME: Do a real to_upper sometime */
111 /* CaseFolding-3.2.0.txt has a table of rules. */
112 if ((source
& 0xFF) == source
)
113 return (xdg_unichar_t
) tolower ((unsigned char) source
);
118 _xdg_utf8_validate (const char *source
)
120 /* FIXME: actually write */
125 _xdg_get_base_name (const char *file_name
)
127 const char *base_name
;
129 if (file_name
== NULL
)
132 base_name
= strrchr (file_name
, '/');
134 if (base_name
== NULL
)
137 return base_name
+ 1;
141 _xdg_convert_to_ucs4 (const char *source
, int *len
)
147 out
= malloc (sizeof (xdg_unichar_t
) * (strlen (source
) + 1));
153 out
[i
++] = _xdg_utf8_to_ucs4 (p
);
154 p
= _xdg_utf8_next_char (p
);
163 _xdg_reverse_ucs4 (xdg_unichar_t
*source
, int len
)
168 for (i
= 0; i
< len
- i
- 1; i
++)
171 source
[i
] = source
[len
- i
- 1];
172 source
[len
- i
- 1] = c
;
177 _xdg_binary_or_text_fallback(const void *data
, size_t len
)
179 unsigned char *chardata
;
182 chardata
= (unsigned char *) data
;
183 for (i
= 0; i
< 128 && i
< len
; ++i
)
185 if (chardata
[i
] < 32 && chardata
[i
] != 9 && chardata
[i
] != 10 && chardata
[i
] != 13)
186 return XDG_MIME_TYPE_UNKNOWN
; /* binary data */
189 return XDG_MIME_TYPE_TEXTPLAIN
;