]> git.wh0rd.org - fontconfig.git/blob - src/fcstr.c
fontconfig library: build fixes and compiler warning fixes
[fontconfig.git] / src / fcstr.c
1 /*
2 * $XFree86: $
3 *
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include "fcint.h"
29
30 FcChar8 *
31 FcStrCopy (const FcChar8 *s)
32 {
33 FcChar8 *r;
34
35 if (!s)
36 return 0;
37 r = (FcChar8 *) malloc (strlen ((char *) s) + 1);
38 if (!r)
39 return 0;
40 FcMemAlloc (FC_MEM_STRING, strlen ((char *) s) + 1);
41 strcpy ((char *) r, (char *) s);
42 return r;
43 }
44
45 FcChar8 *
46 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
47 {
48 int l = strlen ((char *)s1) + strlen ((char *) s2) + 1;
49 FcChar8 *s = malloc (l);
50
51 if (!s)
52 return 0;
53 FcMemAlloc (FC_MEM_STRING, l);
54 strcpy ((char *) s, (char *) s1);
55 strcat ((char *) s, (char *) s2);
56 return s;
57 }
58
59 void
60 FcStrFree (FcChar8 *s)
61 {
62 FcMemFree (FC_MEM_STRING, strlen ((char *) s) + 1);
63 free (s);
64 }
65
66 int
67 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
68 {
69 FcChar8 c1, c2;
70
71 for (;;)
72 {
73 c1 = *s1++;
74 c2 = *s2++;
75 if (!c1 || !c2)
76 break;
77 c1 = FcToLower (c1);
78 c2 = FcToLower (c2);
79 if (c1 != c2)
80 break;
81 }
82 return (int) c2 - (int) c1;
83 }
84
85 int
86 FcUtf8ToUcs4 (FcChar8 *src_orig,
87 FcChar32 *dst,
88 int len)
89 {
90 FcChar8 *src = src_orig;
91 FcChar8 s;
92 int extra;
93 FcChar32 result;
94
95 if (len == 0)
96 return 0;
97
98 s = *src++;
99 len--;
100
101 if (!(s & 0x80))
102 {
103 result = s;
104 extra = 0;
105 }
106 else if (!(s & 0x40))
107 {
108 return -1;
109 }
110 else if (!(s & 0x20))
111 {
112 result = s & 0x1f;
113 extra = 1;
114 }
115 else if (!(s & 0x10))
116 {
117 result = s & 0xf;
118 extra = 2;
119 }
120 else if (!(s & 0x08))
121 {
122 result = s & 0x07;
123 extra = 3;
124 }
125 else if (!(s & 0x04))
126 {
127 result = s & 0x03;
128 extra = 4;
129 }
130 else if ( ! (s & 0x02))
131 {
132 result = s & 0x01;
133 extra = 5;
134 }
135 else
136 {
137 return -1;
138 }
139 if (extra > len)
140 return -1;
141
142 while (extra--)
143 {
144 result <<= 6;
145 s = *src++;
146
147 if ((s & 0xc0) != 0x80)
148 return -1;
149
150 result |= s & 0x3f;
151 }
152 *dst = result;
153 return src - src_orig;
154 }
155
156 FcBool
157 FcUtf8Len (FcChar8 *string,
158 int len,
159 int *nchar,
160 int *wchar)
161 {
162 int n;
163 int clen;
164 FcChar32 c;
165 FcChar32 max;
166
167 n = 0;
168 max = 0;
169 while (len)
170 {
171 clen = FcUtf8ToUcs4 (string, &c, len);
172 if (clen <= 0) /* malformed UTF8 string */
173 return FcFalse;
174 if (c > max)
175 max = c;
176 string += clen;
177 len -= clen;
178 n++;
179 }
180 *nchar = n;
181 if (max >= 0x10000)
182 *wchar = 4;
183 else if (max > 0x100)
184 *wchar = 2;
185 else
186 *wchar = 1;
187 return FcTrue;
188 }