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