xdgmimeint.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, write to the
  24. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25. * Boston, MA 02111-1307, USA.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include "xdgmimeint.h"
  31. #include <ctype.h>
  32. #include <string.h>
  33. #ifndef FALSE
  34. #define FALSE (0)
  35. #endif
  36. #ifndef TRUE
  37. #define TRUE (!FALSE)
  38. #endif
  39. static const char _xdg_utf8_skip_data[256] = {
  40. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  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. 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,
  47. 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
  48. };
  49. const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
  50. /* Returns the number of unprocessed characters. */
  51. xdg_unichar_t
  52. _xdg_utf8_to_ucs4(const char *source)
  53. {
  54. xdg_unichar_t ucs32;
  55. if( ! ( *source & 0x80 ) )
  56. {
  57. ucs32 = *source;
  58. }
  59. else
  60. {
  61. int bytelength = 0;
  62. xdg_unichar_t result;
  63. if ( ! (*source & 0x40) )
  64. {
  65. ucs32 = *source;
  66. }
  67. else
  68. {
  69. if ( ! (*source & 0x20) )
  70. {
  71. result = *source++ & 0x1F;
  72. bytelength = 2;
  73. }
  74. else if ( ! (*source & 0x10) )
  75. {
  76. result = *source++ & 0x0F;
  77. bytelength = 3;
  78. }
  79. else if ( ! (*source & 0x08) )
  80. {
  81. result = *source++ & 0x07;
  82. bytelength = 4;
  83. }
  84. else if ( ! (*source & 0x04) )
  85. {
  86. result = *source++ & 0x03;
  87. bytelength = 5;
  88. }
  89. else if ( ! (*source & 0x02) )
  90. {
  91. result = *source++ & 0x01;
  92. bytelength = 6;
  93. }
  94. else
  95. {
  96. result = *source++;
  97. bytelength = 1;
  98. }
  99. for ( bytelength --; bytelength > 0; bytelength -- )
  100. {
  101. result <<= 6;
  102. result |= *source++ & 0x3F;
  103. }
  104. ucs32 = result;
  105. }
  106. }
  107. return ucs32;
  108. }
  109. /* hullo. this is great code. don't rewrite it */
  110. xdg_unichar_t
  111. _xdg_ucs4_to_lower (xdg_unichar_t source)
  112. {
  113. /* FIXME: Do a real to_upper sometime */
  114. /* CaseFolding-3.2.0.txt has a table of rules. */
  115. if ((source & 0xFF) == source)
  116. return (xdg_unichar_t) tolower ((unsigned char) source);
  117. return source;
  118. }
  119. int
  120. _xdg_utf8_validate (const char *source)
  121. {
  122. /* FIXME: actually write */
  123. return TRUE;
  124. }
  125. const char *
  126. _xdg_get_base_name (const char *file_name)
  127. {
  128. const char *base_name;
  129. if (file_name == NULL)
  130. return NULL;
  131. base_name = strrchr (file_name, '/');
  132. if (base_name == NULL)
  133. return file_name;
  134. else
  135. return base_name + 1;
  136. }
  137. xdg_unichar_t *
  138. _xdg_convert_to_ucs4 (const char *source, int *len)
  139. {
  140. xdg_unichar_t *out;
  141. int i;
  142. const char *p;
  143. out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
  144. p = source;
  145. i = 0;
  146. while (*p)
  147. {
  148. out[i++] = _xdg_utf8_to_ucs4 (p);
  149. p = _xdg_utf8_next_char (p);
  150. }
  151. out[i] = 0;
  152. *len = i;
  153. return out;
  154. }
  155. void
  156. _xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
  157. {
  158. xdg_unichar_t c;
  159. int i;
  160. for (i = 0; i < len - i - 1; i++)
  161. {
  162. c = source[i];
  163. source[i] = source[len - i - 1];
  164. source[len - i - 1] = c;
  165. }
  166. }
  167. const char *
  168. _xdg_binary_or_text_fallback(const void *data, size_t len)
  169. {
  170. unsigned char *chardata;
  171. size_t i;
  172. chardata = (unsigned char *) data;
  173. for (i = 0; i < 128 && i < len; ++i)
  174. {
  175. if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
  176. return XDG_MIME_TYPE_UNKNOWN; /* binary data */
  177. }
  178. return XDG_MIME_TYPE_TEXTPLAIN;
  179. }