Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / unicode / utf8_chset.c
1 /*
2 ** Copyright 2000 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 **
5 ** $Id: utf8_chset.c,v 1.5 2004/05/23 14:28:25 mrsam Exp $
6 */
7
8 #include "unicode.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 /*
14 ** UTF8.toupper/tolower/totitle is implemented by converting UTF8 to
15 ** UCS-4, applying the unicode table lookup, then converting it back to
16 ** UTF8
17 */
18
19 static char *toupper_func(const struct unicode_info *u,
20 const char *cp, int *ip)
21 {
22 unicode_char *uc=unicode_utf8_tou(cp, ip), *p;
23 char *s;
24
25 if (!uc) return (0);
26
27 for (p=uc; *p; p++)
28 *p=unicode_uc(*p);
29
30 s=unicode_utf8_fromu(uc, NULL);
31 free(uc);
32 return (s);
33 }
34
35 static char *tolower_func(const struct unicode_info *u,
36 const char *cp, int *ip)
37 {
38 unicode_char *uc=unicode_utf8_tou(cp, ip), *p;
39 char *s;
40
41 if (!uc) return (0);
42
43 for (p=uc; *p; p++)
44 *p=unicode_lc(*p);
45
46 s=unicode_utf8_fromu(uc, NULL);
47 free(uc);
48 return (s);
49 }
50
51 static char *totitle_func(const struct unicode_info *u,
52 const char *cp, int *ip)
53 {
54 unicode_char *uc=unicode_utf8_tou(cp, ip), *p;
55 char *s;
56
57 if (!uc) return (0);
58
59 for (p=uc; *p; p++)
60 *p=unicode_tc(*p);
61
62 s=unicode_utf8_fromu(uc, NULL);
63 free(uc);
64 return (s);
65 }
66
67 static unicode_char *tou(const struct unicode_info *i, const char *p,
68 int *err)
69 {
70 return unicode_utf8_tou(p, err);
71 }
72
73 static char *fromu(const struct unicode_info *i, const unicode_char *p,
74 int *err)
75 {
76 return unicode_utf8_fromu(p, err);
77 }
78
79 const struct unicode_info unicode_UTF8 = {
80 "UTF-8",
81 UNICODE_UTF | UNICODE_MB | UNICODE_USASCII | UNICODE_HEADER_QUOPRI
82 | UNICODE_BODY_QUOPRI,
83 tou,
84 fromu,
85 toupper_func,
86 tolower_func,
87 totitle_func};
88