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