File: utf8.c

package info (click to toggle)
cabber 0.4.0-test5-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 172 kB
  • ctags: 100
  • sloc: ansic: 1,421; makefile: 82
file content (68 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *
tutf8 (unsigned char ch)
{
  char *ret = malloc (3);
  memset (ret, 0, 3);

  if (ch < 0x80)
    {
      sprintf (ret, "%c", ch);
      return ret;
    }
  if (ch < 0x800)
    {
      sprintf (ret, "%c%c", (0xc0 | (ch >> 6 & 0x1f)), (0x80 | (ch & 0x3f)));
      return ret;
    }
}
unsigned char
fUTF8 (unsigned char lead, unsigned char ch2)
{
  if ((lead & 0x80) == 0)
    return (lead & 0x7f);
  if ((lead & 0xe0) == 0xc0)
    return ((lead & 0x1f) << 6) | (ch2 & 0x3f);
  return lead;
}

char *
fromUTF8 (const char *src)
{
  int i = 0;
  int max = strlen (src) - 1;
  char *ret = malloc (strlen (src) + 1);
  char *aux = ret;

  memset (ret, 0, strlen (src) + 1);
  while (i < max)
    {
      *aux =
	fUTF8 ((unsigned char) (char) src[i],
	       (unsigned char) (char) src[i + 1]);
      if (*aux > 0)
	i++;
      else
	i += 2;
      aux++;
    }
  *aux = fUTF8 (src[i], 0);
  return ret;
}

char *
toUTF8 (const char *src)
{
  int i;
  char *ret = malloc ((strlen (src) * 2) + 1);

  memset (ret, 0, (strlen (src) * 2) + 1);
  for (i = 0; i < strlen (src); i++)
    {
      strcat (ret, tutf8 ((unsigned char) (char) src[i]));
    }
  return ret;
}