]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * fontconfig/src/fcstr.c | |
3 | * | |
4 | * Copyright © 2000 Keith Packard | |
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 "fcint.h" | |
26 | #include <stdlib.h> | |
27 | #include <ctype.h> | |
28 | #include <string.h> | |
29 | #ifdef _WIN32 | |
30 | #include <windows.h> | |
31 | #endif | |
32 | ||
33 | FcChar8 * | |
34 | FcStrCopy (const FcChar8 *s) | |
35 | { | |
36 | int len; | |
37 | FcChar8 *r; | |
38 | ||
39 | if (!s) | |
40 | return 0; | |
41 | len = strlen ((char *) s) + 1; | |
42 | r = (FcChar8 *) malloc (len); | |
43 | if (!r) | |
44 | return 0; | |
45 | FcMemAlloc (FC_MEM_STRING, len); | |
46 | memcpy (r, s, len); | |
47 | return r; | |
48 | } | |
49 | ||
50 | FcChar8 * | |
51 | FcStrPlus (const FcChar8 *s1, const FcChar8 *s2) | |
52 | { | |
53 | int l = strlen ((char *)s1) + strlen ((char *) s2) + 1; | |
54 | FcChar8 *s = malloc (l); | |
55 | ||
56 | if (!s) | |
57 | return 0; | |
58 | FcMemAlloc (FC_MEM_STRING, l); | |
59 | strcpy ((char *) s, (char *) s1); | |
60 | strcat ((char *) s, (char *) s2); | |
61 | return s; | |
62 | } | |
63 | ||
64 | void | |
65 | FcStrFree (FcChar8 *s) | |
66 | { | |
67 | FcMemFree (FC_MEM_STRING, strlen ((char *) s) + 1); | |
68 | free (s); | |
69 | } | |
70 | ||
71 | ||
72 | #include "../fc-case/fccase.h" | |
73 | ||
74 | #define FcCaseFoldUpperCount(cf) \ | |
75 | ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count) | |
76 | ||
77 | #define FC_STR_CANON_BUF_LEN 1024 | |
78 | ||
79 | typedef struct _FcCaseWalker { | |
80 | const FcChar8 *read; | |
81 | const FcChar8 *src; | |
82 | FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1]; | |
83 | } FcCaseWalker; | |
84 | ||
85 | static void | |
86 | FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w) | |
87 | { | |
88 | w->src = src; | |
89 | w->read = 0; | |
90 | } | |
91 | ||
92 | static FcChar8 | |
93 | FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r) | |
94 | { | |
95 | FcChar32 ucs4; | |
96 | int slen; | |
97 | int len = strlen((char*)w->src); | |
98 | ||
99 | slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1); | |
100 | if (slen <= 0) | |
101 | return r; | |
102 | if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR) | |
103 | { | |
104 | int min = 0; | |
105 | int max = FC_NUM_CASE_FOLD; | |
106 | ||
107 | while (min <= max) | |
108 | { | |
109 | int mid = (min + max) >> 1; | |
110 | FcChar32 low = fcCaseFold[mid].upper; | |
111 | FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]); | |
112 | ||
113 | if (high <= ucs4) | |
114 | min = mid + 1; | |
115 | else if (ucs4 < low) | |
116 | max = mid - 1; | |
117 | else | |
118 | { | |
119 | const FcCaseFold *fold = &fcCaseFold[mid]; | |
120 | int dlen; | |
121 | ||
122 | switch (fold->method) { | |
123 | case FC_CASE_FOLD_EVEN_ODD: | |
124 | if ((ucs4 & 1) != (fold->upper & 1)) | |
125 | return r; | |
126 | /* fall through ... */ | |
127 | default: | |
128 | dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8); | |
129 | break; | |
130 | case FC_CASE_FOLD_FULL: | |
131 | dlen = fold->count; | |
132 | memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen); | |
133 | break; | |
134 | } | |
135 | ||
136 | /* consume rest of src utf-8 bytes */ | |
137 | w->src += slen - 1; | |
138 | ||
139 | /* read from temp buffer */ | |
140 | w->utf8[dlen] = '\0'; | |
141 | w->read = w->utf8; | |
142 | return *w->read++; | |
143 | } | |
144 | } | |
145 | } | |
146 | return r; | |
147 | } | |
148 | ||
149 | static FcChar8 | |
150 | FcStrCaseWalkerNext (FcCaseWalker *w) | |
151 | { | |
152 | FcChar8 r; | |
153 | ||
154 | if (w->read) | |
155 | { | |
156 | if ((r = *w->read++)) | |
157 | return r; | |
158 | w->read = 0; | |
159 | } | |
160 | r = *w->src++; | |
161 | ||
162 | if ((r & 0xc0) == 0xc0) | |
163 | return FcStrCaseWalkerLong (w, r); | |
164 | if ('A' <= r && r <= 'Z') | |
165 | r = r - 'A' + 'a'; | |
166 | return r; | |
167 | } | |
168 | ||
169 | static FcChar8 | |
170 | FcStrCaseWalkerNextIgnoreBlanks (FcCaseWalker *w) | |
171 | { | |
172 | FcChar8 r; | |
173 | ||
174 | if (w->read) | |
175 | { | |
176 | if ((r = *w->read++)) | |
177 | return r; | |
178 | w->read = 0; | |
179 | } | |
180 | do | |
181 | { | |
182 | r = *w->src++; | |
183 | } while (r == ' '); | |
184 | ||
185 | if ((r & 0xc0) == 0xc0) | |
186 | return FcStrCaseWalkerLong (w, r); | |
187 | if ('A' <= r && r <= 'Z') | |
188 | r = r - 'A' + 'a'; | |
189 | return r; | |
190 | } | |
191 | ||
192 | FcChar8 * | |
193 | FcStrDowncase (const FcChar8 *s) | |
194 | { | |
195 | FcCaseWalker w; | |
196 | int len = 0; | |
197 | FcChar8 *dst, *d; | |
198 | ||
199 | FcStrCaseWalkerInit (s, &w); | |
200 | while (FcStrCaseWalkerNext (&w)) | |
201 | len++; | |
202 | d = dst = malloc (len + 1); | |
203 | if (!d) | |
204 | return 0; | |
205 | FcMemAlloc (FC_MEM_STRING, len + 1); | |
206 | FcStrCaseWalkerInit (s, &w); | |
207 | while ((*d++ = FcStrCaseWalkerNext (&w))); | |
208 | return dst; | |
209 | } | |
210 | ||
211 | int | |
212 | FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2) | |
213 | { | |
214 | FcCaseWalker w1, w2; | |
215 | FcChar8 c1, c2; | |
216 | ||
217 | if (s1 == s2) return 0; | |
218 | ||
219 | FcStrCaseWalkerInit (s1, &w1); | |
220 | FcStrCaseWalkerInit (s2, &w2); | |
221 | ||
222 | for (;;) | |
223 | { | |
224 | c1 = FcStrCaseWalkerNext (&w1); | |
225 | c2 = FcStrCaseWalkerNext (&w2); | |
226 | if (!c1 || (c1 != c2)) | |
227 | break; | |
228 | } | |
229 | return (int) c1 - (int) c2; | |
230 | } | |
231 | ||
232 | int | |
233 | FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2) | |
234 | { | |
235 | FcCaseWalker w1, w2; | |
236 | FcChar8 c1, c2; | |
237 | ||
238 | if (s1 == s2) return 0; | |
239 | ||
240 | FcStrCaseWalkerInit (s1, &w1); | |
241 | FcStrCaseWalkerInit (s2, &w2); | |
242 | ||
243 | for (;;) | |
244 | { | |
245 | c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1); | |
246 | c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2); | |
247 | if (!c1 || (c1 != c2)) | |
248 | break; | |
249 | } | |
250 | return (int) c1 - (int) c2; | |
251 | } | |
252 | ||
253 | int | |
254 | FcStrCmp (const FcChar8 *s1, const FcChar8 *s2) | |
255 | { | |
256 | FcChar8 c1, c2; | |
257 | ||
258 | if (s1 == s2) | |
259 | return 0; | |
260 | for (;;) | |
261 | { | |
262 | c1 = *s1++; | |
263 | c2 = *s2++; | |
264 | if (!c1 || c1 != c2) | |
265 | break; | |
266 | } | |
267 | return (int) c1 - (int) c2; | |
268 | } | |
269 | ||
270 | /* | |
271 | * Return a hash value for a string | |
272 | */ | |
273 | ||
274 | FcChar32 | |
275 | FcStrHashIgnoreCase (const FcChar8 *s) | |
276 | { | |
277 | FcChar32 h = 0; | |
278 | FcCaseWalker w; | |
279 | FcChar8 c; | |
280 | ||
281 | FcStrCaseWalkerInit (s, &w); | |
282 | while ((c = FcStrCaseWalkerNext (&w))) | |
283 | h = ((h << 3) ^ (h >> 3)) ^ c; | |
284 | return h; | |
285 | } | |
286 | ||
287 | /* | |
288 | * Is the head of s1 equal to s2? | |
289 | */ | |
290 | ||
291 | static FcBool | |
292 | FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2) | |
293 | { | |
294 | FcCaseWalker w1, w2; | |
295 | FcChar8 c1, c2; | |
296 | ||
297 | FcStrCaseWalkerInit (s1, &w1); | |
298 | FcStrCaseWalkerInit (s2, &w2); | |
299 | ||
300 | for (;;) | |
301 | { | |
302 | c1 = FcStrCaseWalkerNextIgnoreBlanks (&w1); | |
303 | c2 = FcStrCaseWalkerNextIgnoreBlanks (&w2); | |
304 | if (!c1 || (c1 != c2)) | |
305 | break; | |
306 | } | |
307 | return c1 == c2 || !c2; | |
308 | } | |
309 | ||
310 | /* | |
311 | * Does s1 contain an instance of s2 (ignoring blanks and case)? | |
312 | */ | |
313 | ||
314 | const FcChar8 * | |
315 | FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2) | |
316 | { | |
317 | while (*s1) | |
318 | { | |
319 | if (FcStrIsAtIgnoreBlanksAndCase (s1, s2)) | |
320 | return s1; | |
321 | s1++; | |
322 | } | |
323 | return 0; | |
324 | } | |
325 | ||
326 | static FcBool | |
327 | FcCharIsPunct (const FcChar8 c) | |
328 | { | |
329 | if (c < '0') | |
330 | return FcTrue; | |
331 | if (c <= '9') | |
332 | return FcFalse; | |
333 | if (c < 'A') | |
334 | return FcTrue; | |
335 | if (c <= 'Z') | |
336 | return FcFalse; | |
337 | if (c < 'a') | |
338 | return FcTrue; | |
339 | if (c <= 'z') | |
340 | return FcFalse; | |
341 | if (c <= '~') | |
342 | return FcTrue; | |
343 | return FcFalse; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Is the head of s1 equal to s2? | |
348 | */ | |
349 | ||
350 | static FcBool | |
351 | FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2) | |
352 | { | |
353 | FcCaseWalker w1, w2; | |
354 | FcChar8 c1, c2; | |
355 | ||
356 | FcStrCaseWalkerInit (s1, &w1); | |
357 | FcStrCaseWalkerInit (s2, &w2); | |
358 | ||
359 | for (;;) | |
360 | { | |
361 | c1 = FcStrCaseWalkerNext (&w1); | |
362 | c2 = FcStrCaseWalkerNext (&w2); | |
363 | if (!c1 || (c1 != c2)) | |
364 | break; | |
365 | } | |
366 | return c1 == c2 || !c2; | |
367 | } | |
368 | ||
369 | /* | |
370 | * Does s1 contain an instance of s2 (ignoring blanks and case)? | |
371 | */ | |
372 | ||
373 | const FcChar8 * | |
374 | FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2) | |
375 | { | |
376 | while (*s1) | |
377 | { | |
378 | if (FcStrIsAtIgnoreCase (s1, s2)) | |
379 | return s1; | |
380 | s1++; | |
381 | } | |
382 | return 0; | |
383 | } | |
384 | ||
385 | /* | |
386 | * Does s1 contain an instance of s2 on a word boundary (ignoring case)? | |
387 | */ | |
388 | ||
389 | const FcChar8 * | |
390 | FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2) | |
391 | { | |
392 | FcBool wordStart = FcTrue; | |
393 | int s1len = strlen ((char *) s1); | |
394 | int s2len = strlen ((char *) s2); | |
395 | ||
396 | while (s1len >= s2len) | |
397 | { | |
398 | if (wordStart && | |
399 | FcStrIsAtIgnoreCase (s1, s2) && | |
400 | (s1len == s2len || FcCharIsPunct (s1[s2len]))) | |
401 | { | |
402 | return s1; | |
403 | } | |
404 | wordStart = FcFalse; | |
405 | if (FcCharIsPunct (*s1)) | |
406 | wordStart = FcTrue; | |
407 | s1++; | |
408 | s1len--; | |
409 | } | |
410 | return 0; | |
411 | } | |
412 | ||
413 | const FcChar8 * | |
414 | FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2) | |
415 | { | |
416 | FcCaseWalker w1, w2; | |
417 | FcChar8 c1, c2; | |
418 | const FcChar8 *cur; | |
419 | ||
420 | if (!s1 || !s2) | |
421 | return 0; | |
422 | ||
423 | if (s1 == s2) | |
424 | return s1; | |
425 | ||
426 | FcStrCaseWalkerInit (s1, &w1); | |
427 | FcStrCaseWalkerInit (s2, &w2); | |
428 | ||
429 | c2 = FcStrCaseWalkerNext (&w2); | |
430 | ||
431 | for (;;) | |
432 | { | |
433 | cur = w1.src; | |
434 | c1 = FcStrCaseWalkerNext (&w1); | |
435 | if (!c1) | |
436 | break; | |
437 | if (c1 == c2) | |
438 | { | |
439 | FcCaseWalker w1t = w1; | |
440 | FcCaseWalker w2t = w2; | |
441 | FcChar8 c1t, c2t; | |
442 | ||
443 | for (;;) | |
444 | { | |
445 | c1t = FcStrCaseWalkerNext (&w1t); | |
446 | c2t = FcStrCaseWalkerNext (&w2t); | |
447 | ||
448 | if (!c2t) | |
449 | return cur; | |
450 | if (c2t != c1t) | |
451 | break; | |
452 | } | |
453 | } | |
454 | } | |
455 | return 0; | |
456 | } | |
457 | ||
458 | const FcChar8 * | |
459 | FcStrStr (const FcChar8 *s1, const FcChar8 *s2) | |
460 | { | |
461 | FcChar8 c1, c2; | |
462 | const FcChar8 * p = s1; | |
463 | const FcChar8 * b = s2; | |
464 | ||
465 | if (!s1 || !s2) | |
466 | return 0; | |
467 | ||
468 | if (s1 == s2) | |
469 | return s1; | |
470 | ||
471 | again: | |
472 | c2 = *s2++; | |
473 | ||
474 | if (!c2) | |
475 | return 0; | |
476 | ||
477 | for (;;) | |
478 | { | |
479 | p = s1; | |
480 | c1 = *s1++; | |
481 | if (!c1 || c1 == c2) | |
482 | break; | |
483 | } | |
484 | ||
485 | if (c1 != c2) | |
486 | return 0; | |
487 | ||
488 | for (;;) | |
489 | { | |
490 | c1 = *s1; | |
491 | c2 = *s2; | |
492 | if (c1 && c2 && c1 != c2) | |
493 | { | |
494 | s1 = p + 1; | |
495 | s2 = b; | |
496 | goto again; | |
497 | } | |
498 | if (!c2) | |
499 | return p; | |
500 | if (!c1) | |
501 | return 0; | |
502 | ++ s1; | |
503 | ++ s2; | |
504 | } | |
505 | /* never reached. */ | |
506 | } | |
507 | ||
508 | int | |
509 | FcUtf8ToUcs4 (const FcChar8 *src_orig, | |
510 | FcChar32 *dst, | |
511 | int len) | |
512 | { | |
513 | const FcChar8 *src = src_orig; | |
514 | FcChar8 s; | |
515 | int extra; | |
516 | FcChar32 result; | |
517 | ||
518 | if (len == 0) | |
519 | return 0; | |
520 | ||
521 | s = *src++; | |
522 | len--; | |
523 | ||
524 | if (!(s & 0x80)) | |
525 | { | |
526 | result = s; | |
527 | extra = 0; | |
528 | } | |
529 | else if (!(s & 0x40)) | |
530 | { | |
531 | return -1; | |
532 | } | |
533 | else if (!(s & 0x20)) | |
534 | { | |
535 | result = s & 0x1f; | |
536 | extra = 1; | |
537 | } | |
538 | else if (!(s & 0x10)) | |
539 | { | |
540 | result = s & 0xf; | |
541 | extra = 2; | |
542 | } | |
543 | else if (!(s & 0x08)) | |
544 | { | |
545 | result = s & 0x07; | |
546 | extra = 3; | |
547 | } | |
548 | else if (!(s & 0x04)) | |
549 | { | |
550 | result = s & 0x03; | |
551 | extra = 4; | |
552 | } | |
553 | else if ( ! (s & 0x02)) | |
554 | { | |
555 | result = s & 0x01; | |
556 | extra = 5; | |
557 | } | |
558 | else | |
559 | { | |
560 | return -1; | |
561 | } | |
562 | if (extra > len) | |
563 | return -1; | |
564 | ||
565 | while (extra--) | |
566 | { | |
567 | result <<= 6; | |
568 | s = *src++; | |
569 | ||
570 | if ((s & 0xc0) != 0x80) | |
571 | return -1; | |
572 | ||
573 | result |= s & 0x3f; | |
574 | } | |
575 | *dst = result; | |
576 | return src - src_orig; | |
577 | } | |
578 | ||
579 | FcBool | |
580 | FcUtf8Len (const FcChar8 *string, | |
581 | int len, | |
582 | int *nchar, | |
583 | int *wchar) | |
584 | { | |
585 | int n; | |
586 | int clen; | |
587 | FcChar32 c; | |
588 | FcChar32 max; | |
589 | ||
590 | n = 0; | |
591 | max = 0; | |
592 | while (len) | |
593 | { | |
594 | clen = FcUtf8ToUcs4 (string, &c, len); | |
595 | if (clen <= 0) /* malformed UTF8 string */ | |
596 | return FcFalse; | |
597 | if (c > max) | |
598 | max = c; | |
599 | string += clen; | |
600 | len -= clen; | |
601 | n++; | |
602 | } | |
603 | *nchar = n; | |
604 | if (max >= 0x10000) | |
605 | *wchar = 4; | |
606 | else if (max > 0x100) | |
607 | *wchar = 2; | |
608 | else | |
609 | *wchar = 1; | |
610 | return FcTrue; | |
611 | } | |
612 | ||
613 | int | |
614 | FcUcs4ToUtf8 (FcChar32 ucs4, | |
615 | FcChar8 dest[FC_UTF8_MAX_LEN]) | |
616 | { | |
617 | int bits; | |
618 | FcChar8 *d = dest; | |
619 | ||
620 | if (ucs4 < 0x80) { *d++= ucs4; bits= -6; } | |
621 | else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; } | |
622 | else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; } | |
623 | else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; } | |
624 | else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; } | |
625 | else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; } | |
626 | else return 0; | |
627 | ||
628 | for ( ; bits >= 0; bits-= 6) { | |
629 | *d++= ((ucs4 >> bits) & 0x3F) | 0x80; | |
630 | } | |
631 | return d - dest; | |
632 | } | |
633 | ||
634 | #define GetUtf16(src,endian) \ | |
635 | ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \ | |
636 | (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0])) | |
637 | ||
638 | int | |
639 | FcUtf16ToUcs4 (const FcChar8 *src_orig, | |
640 | FcEndian endian, | |
641 | FcChar32 *dst, | |
642 | int len) /* in bytes */ | |
643 | { | |
644 | const FcChar8 *src = src_orig; | |
645 | FcChar16 a, b; | |
646 | FcChar32 result; | |
647 | ||
648 | if (len < 2) | |
649 | return 0; | |
650 | ||
651 | a = GetUtf16 (src, endian); src += 2; len -= 2; | |
652 | ||
653 | /* | |
654 | * Check for surrogate | |
655 | */ | |
656 | if ((a & 0xfc00) == 0xd800) | |
657 | { | |
658 | if (len < 2) | |
659 | return 0; | |
660 | b = GetUtf16 (src, endian); src += 2; len -= 2; | |
661 | /* | |
662 | * Check for invalid surrogate sequence | |
663 | */ | |
664 | if ((b & 0xfc00) != 0xdc00) | |
665 | return 0; | |
666 | result = ((((FcChar32) a & 0x3ff) << 10) | | |
667 | ((FcChar32) b & 0x3ff)) + 0x10000; | |
668 | } | |
669 | else | |
670 | result = a; | |
671 | *dst = result; | |
672 | return src - src_orig; | |
673 | } | |
674 | ||
675 | FcBool | |
676 | FcUtf16Len (const FcChar8 *string, | |
677 | FcEndian endian, | |
678 | int len, /* in bytes */ | |
679 | int *nchar, | |
680 | int *wchar) | |
681 | { | |
682 | int n; | |
683 | int clen; | |
684 | FcChar32 c; | |
685 | FcChar32 max; | |
686 | ||
687 | n = 0; | |
688 | max = 0; | |
689 | while (len) | |
690 | { | |
691 | clen = FcUtf16ToUcs4 (string, endian, &c, len); | |
692 | if (clen <= 0) /* malformed UTF8 string */ | |
693 | return FcFalse; | |
694 | if (c > max) | |
695 | max = c; | |
696 | string += clen; | |
697 | len -= clen; | |
698 | n++; | |
699 | } | |
700 | *nchar = n; | |
701 | if (max >= 0x10000) | |
702 | *wchar = 4; | |
703 | else if (max > 0x100) | |
704 | *wchar = 2; | |
705 | else | |
706 | *wchar = 1; | |
707 | return FcTrue; | |
708 | } | |
709 | ||
710 | void | |
711 | FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size) | |
712 | { | |
713 | buf->buf = init; | |
714 | buf->allocated = FcFalse; | |
715 | buf->failed = FcFalse; | |
716 | buf->len = 0; | |
717 | buf->size = size; | |
718 | } | |
719 | ||
720 | void | |
721 | FcStrBufDestroy (FcStrBuf *buf) | |
722 | { | |
723 | if (buf->allocated) | |
724 | { | |
725 | FcMemFree (FC_MEM_STRBUF, buf->size); | |
726 | free (buf->buf); | |
727 | FcStrBufInit (buf, 0, 0); | |
728 | } | |
729 | } | |
730 | ||
731 | FcChar8 * | |
732 | FcStrBufDone (FcStrBuf *buf) | |
733 | { | |
734 | FcChar8 *ret; | |
735 | ||
736 | if (buf->failed) | |
737 | ret = NULL; | |
738 | else | |
739 | ret = malloc (buf->len + 1); | |
740 | if (ret) | |
741 | { | |
742 | FcMemAlloc (FC_MEM_STRING, buf->len + 1); | |
743 | memcpy (ret, buf->buf, buf->len); | |
744 | ret[buf->len] = '\0'; | |
745 | } | |
746 | FcStrBufDestroy (buf); | |
747 | return ret; | |
748 | } | |
749 | ||
750 | FcBool | |
751 | FcStrBufChar (FcStrBuf *buf, FcChar8 c) | |
752 | { | |
753 | if (buf->len == buf->size) | |
754 | { | |
755 | FcChar8 *new; | |
756 | int size; | |
757 | ||
758 | if (buf->failed) | |
759 | return FcFalse; | |
760 | ||
761 | if (buf->allocated) | |
762 | { | |
763 | size = buf->size * 2; | |
764 | new = realloc (buf->buf, size); | |
765 | } | |
766 | else | |
767 | { | |
768 | size = buf->size + 64; | |
769 | new = malloc (size); | |
770 | if (new) | |
771 | { | |
772 | buf->allocated = FcTrue; | |
773 | memcpy (new, buf->buf, buf->len); | |
774 | } | |
775 | } | |
776 | if (!new) | |
777 | { | |
778 | buf->failed = FcTrue; | |
779 | return FcFalse; | |
780 | } | |
781 | if (buf->size) | |
782 | FcMemFree (FC_MEM_STRBUF, buf->size); | |
783 | FcMemAlloc (FC_MEM_STRBUF, size); | |
784 | buf->size = size; | |
785 | buf->buf = new; | |
786 | } | |
787 | buf->buf[buf->len++] = c; | |
788 | return FcTrue; | |
789 | } | |
790 | ||
791 | FcBool | |
792 | FcStrBufString (FcStrBuf *buf, const FcChar8 *s) | |
793 | { | |
794 | FcChar8 c; | |
795 | while ((c = *s++)) | |
796 | if (!FcStrBufChar (buf, c)) | |
797 | return FcFalse; | |
798 | return FcTrue; | |
799 | } | |
800 | ||
801 | FcBool | |
802 | FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len) | |
803 | { | |
804 | while (len-- > 0) | |
805 | if (!FcStrBufChar (buf, *s++)) | |
806 | return FcFalse; | |
807 | return FcTrue; | |
808 | } | |
809 | ||
810 | FcBool | |
811 | FcStrUsesHome (const FcChar8 *s) | |
812 | { | |
813 | return *s == '~'; | |
814 | } | |
815 | ||
816 | FcChar8 * | |
817 | FcStrCopyFilename (const FcChar8 *s) | |
818 | { | |
819 | FcChar8 *new; | |
820 | ||
821 | if (*s == '~') | |
822 | { | |
823 | FcChar8 *home = FcConfigHome (); | |
824 | FcChar8 *full; | |
825 | int size; | |
826 | if (!home) | |
827 | return 0; | |
828 | size = strlen ((char *) home) + strlen ((char *) s); | |
829 | full = (FcChar8 *) malloc (size); | |
830 | if (!full) | |
831 | return 0; | |
832 | strcpy ((char *) full, (char *) home); | |
833 | strcat ((char *) full, (char *) s + 1); | |
834 | new = FcStrCanonFilename (full); | |
835 | free (full); | |
836 | } | |
837 | else | |
838 | new = FcStrCanonFilename (s); | |
839 | return new; | |
840 | } | |
841 | ||
842 | FcChar8 * | |
843 | FcStrLastSlash (const FcChar8 *path) | |
844 | { | |
845 | FcChar8 *slash; | |
846 | ||
847 | slash = (FcChar8 *) strrchr ((const char *) path, '/'); | |
848 | #ifdef _WIN32 | |
849 | { | |
850 | FcChar8 *backslash; | |
851 | ||
852 | backslash = (FcChar8 *) strrchr ((const char *) path, '\\'); | |
853 | if (!slash || (backslash && backslash > slash)) | |
854 | slash = backslash; | |
855 | } | |
856 | #endif | |
857 | ||
858 | return slash; | |
859 | } | |
860 | ||
861 | FcChar8 * | |
862 | FcStrDirname (const FcChar8 *file) | |
863 | { | |
864 | FcChar8 *slash; | |
865 | FcChar8 *dir; | |
866 | ||
867 | slash = FcStrLastSlash (file); | |
868 | if (!slash) | |
869 | return FcStrCopy ((FcChar8 *) "."); | |
870 | dir = malloc ((slash - file) + 1); | |
871 | if (!dir) | |
872 | return 0; | |
873 | FcMemAlloc (FC_MEM_STRING, (slash - file) + 1); | |
874 | strncpy ((char *) dir, (const char *) file, slash - file); | |
875 | dir[slash - file] = '\0'; | |
876 | return dir; | |
877 | } | |
878 | ||
879 | FcChar8 * | |
880 | FcStrBasename (const FcChar8 *file) | |
881 | { | |
882 | FcChar8 *slash; | |
883 | ||
884 | slash = FcStrLastSlash (file); | |
885 | if (!slash) | |
886 | return FcStrCopy (file); | |
887 | return FcStrCopy (slash + 1); | |
888 | } | |
889 | ||
890 | static FcChar8 * | |
891 | FcStrCanonAbsoluteFilename (const FcChar8 *s) | |
892 | { | |
893 | FcChar8 *file; | |
894 | FcChar8 *f; | |
895 | const FcChar8 *slash; | |
896 | int size; | |
897 | ||
898 | size = strlen ((char *) s) + 1; | |
899 | file = malloc (size); | |
900 | if (!file) | |
901 | return NULL; | |
902 | FcMemAlloc (FC_MEM_STRING, size); | |
903 | slash = NULL; | |
904 | f = file; | |
905 | for (;;) { | |
906 | if (*s == '/' || *s == '\0') | |
907 | { | |
908 | if (slash) | |
909 | { | |
910 | switch (s - slash) { | |
911 | case 1: | |
912 | f -= 1; /* squash // and trim final / from file */ | |
913 | break; | |
914 | case 2: | |
915 | if (!strncmp ((char *) slash, "/.", 2)) | |
916 | { | |
917 | f -= 2; /* trim /. from file */ | |
918 | } | |
919 | break; | |
920 | case 3: | |
921 | if (!strncmp ((char *) slash, "/..", 3)) | |
922 | { | |
923 | f -= 3; /* trim /.. from file */ | |
924 | while (f > file) { | |
925 | if (*--f == '/') | |
926 | break; | |
927 | } | |
928 | } | |
929 | break; | |
930 | } | |
931 | } | |
932 | slash = s; | |
933 | } | |
934 | if (!(*f++ = *s++)) | |
935 | break; | |
936 | } | |
937 | return file; | |
938 | } | |
939 | ||
940 | #ifdef _WIN32 | |
941 | /* | |
942 | * Convert '\\' to '/' , remove double '/' | |
943 | */ | |
944 | static void | |
945 | FcConvertDosPath (char *str) | |
946 | { | |
947 | size_t len = strlen (str); | |
948 | char *p = str; | |
949 | char *dest = str; | |
950 | char *end = str + len; | |
951 | char last = 0; | |
952 | ||
953 | if (*p == '\\') | |
954 | { | |
955 | *p = '/'; | |
956 | p++; | |
957 | dest++; | |
958 | } | |
959 | while (p < end) | |
960 | { | |
961 | if (*p == '\\') | |
962 | *p = '/'; | |
963 | ||
964 | if (*p != '/' | |
965 | || last != '/') | |
966 | { | |
967 | *dest++ = *p; | |
968 | } | |
969 | ||
970 | last = *p; | |
971 | p++; | |
972 | } | |
973 | ||
974 | *dest = 0; | |
975 | } | |
976 | #endif | |
977 | ||
978 | FcChar8 * | |
979 | FcStrCanonFilename (const FcChar8 *s) | |
980 | { | |
981 | #ifdef _WIN32 | |
982 | FcChar8 full[FC_MAX_FILE_LEN + 2]; | |
983 | int size = GetFullPathName (s, sizeof (full) -1, | |
984 | full, NULL); | |
985 | ||
986 | if (size == 0) | |
987 | perror ("GetFullPathName"); | |
988 | ||
989 | FcConvertDosPath (full); | |
990 | return FcStrCanonAbsoluteFilename (full); | |
991 | #else | |
992 | if (s[0] == '/') | |
993 | return FcStrCanonAbsoluteFilename (s); | |
994 | else | |
995 | { | |
996 | FcChar8 *full; | |
997 | FcChar8 *file; | |
998 | ||
999 | FcChar8 cwd[FC_MAX_FILE_LEN + 2]; | |
1000 | if (getcwd ((char *) cwd, FC_MAX_FILE_LEN) == NULL) | |
1001 | return NULL; | |
1002 | strcat ((char *) cwd, "/"); | |
1003 | full = FcStrPlus (cwd, s); | |
1004 | file = FcStrCanonAbsoluteFilename (full); | |
1005 | FcStrFree (full); | |
1006 | return file; | |
1007 | } | |
1008 | #endif | |
1009 | } | |
1010 | ||
1011 | ||
1012 | FcStrSet * | |
1013 | FcStrSetCreate (void) | |
1014 | { | |
1015 | FcStrSet *set = malloc (sizeof (FcStrSet)); | |
1016 | if (!set) | |
1017 | return 0; | |
1018 | FcMemAlloc (FC_MEM_STRSET, sizeof (FcStrSet)); | |
1019 | set->ref = 1; | |
1020 | set->num = 0; | |
1021 | set->size = 0; | |
1022 | set->strs = 0; | |
1023 | return set; | |
1024 | } | |
1025 | ||
1026 | static FcBool | |
1027 | _FcStrSetAppend (FcStrSet *set, FcChar8 *s) | |
1028 | { | |
1029 | if (FcStrSetMember (set, s)) | |
1030 | { | |
1031 | FcStrFree (s); | |
1032 | return FcTrue; | |
1033 | } | |
1034 | if (set->num == set->size) | |
1035 | { | |
1036 | FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *)); | |
1037 | ||
1038 | if (!strs) | |
1039 | return FcFalse; | |
1040 | FcMemAlloc (FC_MEM_STRSET, (set->size + 2) * sizeof (FcChar8 *)); | |
1041 | if (set->num) | |
1042 | memcpy (strs, set->strs, set->num * sizeof (FcChar8 *)); | |
1043 | if (set->strs) | |
1044 | { | |
1045 | FcMemFree (FC_MEM_STRSET, (set->size + 1) * sizeof (FcChar8 *)); | |
1046 | free (set->strs); | |
1047 | } | |
1048 | set->size = set->size + 1; | |
1049 | set->strs = strs; | |
1050 | } | |
1051 | set->strs[set->num++] = s; | |
1052 | set->strs[set->num] = 0; | |
1053 | return FcTrue; | |
1054 | } | |
1055 | ||
1056 | FcBool | |
1057 | FcStrSetMember (FcStrSet *set, const FcChar8 *s) | |
1058 | { | |
1059 | int i; | |
1060 | ||
1061 | for (i = 0; i < set->num; i++) | |
1062 | if (!FcStrCmp (set->strs[i], s)) | |
1063 | return FcTrue; | |
1064 | return FcFalse; | |
1065 | } | |
1066 | ||
1067 | FcBool | |
1068 | FcStrSetEqual (FcStrSet *sa, FcStrSet *sb) | |
1069 | { | |
1070 | int i; | |
1071 | if (sa->num != sb->num) | |
1072 | return FcFalse; | |
1073 | for (i = 0; i < sa->num; i++) | |
1074 | if (!FcStrSetMember (sb, sa->strs[i])) | |
1075 | return FcFalse; | |
1076 | return FcTrue; | |
1077 | } | |
1078 | ||
1079 | FcBool | |
1080 | FcStrSetAdd (FcStrSet *set, const FcChar8 *s) | |
1081 | { | |
1082 | FcChar8 *new = FcStrCopy (s); | |
1083 | if (!new) | |
1084 | return FcFalse; | |
1085 | if (!_FcStrSetAppend (set, new)) | |
1086 | { | |
1087 | FcStrFree (new); | |
1088 | return FcFalse; | |
1089 | } | |
1090 | return FcTrue; | |
1091 | } | |
1092 | ||
1093 | FcBool | |
1094 | FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s) | |
1095 | { | |
1096 | FcChar8 *new = FcStrCopyFilename (s); | |
1097 | if (!new) | |
1098 | return FcFalse; | |
1099 | if (!_FcStrSetAppend (set, new)) | |
1100 | { | |
1101 | FcStrFree (new); | |
1102 | return FcFalse; | |
1103 | } | |
1104 | return FcTrue; | |
1105 | } | |
1106 | ||
1107 | FcBool | |
1108 | FcStrSetDel (FcStrSet *set, const FcChar8 *s) | |
1109 | { | |
1110 | int i; | |
1111 | ||
1112 | for (i = 0; i < set->num; i++) | |
1113 | if (!FcStrCmp (set->strs[i], s)) | |
1114 | { | |
1115 | FcStrFree (set->strs[i]); | |
1116 | /* | |
1117 | * copy remaining string pointers and trailing | |
1118 | * NULL | |
1119 | */ | |
1120 | memmove (&set->strs[i], &set->strs[i+1], | |
1121 | (set->num - i) * sizeof (FcChar8 *)); | |
1122 | set->num--; | |
1123 | return FcTrue; | |
1124 | } | |
1125 | return FcFalse; | |
1126 | } | |
1127 | ||
1128 | void | |
1129 | FcStrSetDestroy (FcStrSet *set) | |
1130 | { | |
1131 | if (--set->ref == 0) | |
1132 | { | |
1133 | int i; | |
1134 | ||
1135 | for (i = 0; i < set->num; i++) | |
1136 | FcStrFree (set->strs[i]); | |
1137 | if (set->strs) | |
1138 | { | |
1139 | FcMemFree (FC_MEM_STRSET, (set->size + 1) * sizeof (FcChar8 *)); | |
1140 | free (set->strs); | |
1141 | } | |
1142 | FcMemFree (FC_MEM_STRSET, sizeof (FcStrSet)); | |
1143 | free (set); | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | FcStrList * | |
1148 | FcStrListCreate (FcStrSet *set) | |
1149 | { | |
1150 | FcStrList *list; | |
1151 | ||
1152 | list = malloc (sizeof (FcStrList)); | |
1153 | if (!list) | |
1154 | return 0; | |
1155 | FcMemAlloc (FC_MEM_STRLIST, sizeof (FcStrList)); | |
1156 | list->set = set; | |
1157 | set->ref++; | |
1158 | list->n = 0; | |
1159 | return list; | |
1160 | } | |
1161 | ||
1162 | FcChar8 * | |
1163 | FcStrListNext (FcStrList *list) | |
1164 | { | |
1165 | if (list->n >= list->set->num) | |
1166 | return 0; | |
1167 | return list->set->strs[list->n++]; | |
1168 | } | |
1169 | ||
1170 | void | |
1171 | FcStrListDone (FcStrList *list) | |
1172 | { | |
1173 | FcStrSetDestroy (list->set); | |
1174 | FcMemFree (FC_MEM_STRLIST, sizeof (FcStrList)); | |
1175 | free (list); | |
1176 | } | |
1177 | ||
1178 | #define __fcstr__ | |
1179 | #include "fcaliastail.h" | |
1180 | #undef __fcstr__ |