2 * $RCSId: xc/lib/fontconfig/src/fcstr.c,v 1.10 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard
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.
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.
31 FcStrCopy (const FcChar8 *s)
37 r = (FcChar8 *) malloc (strlen ((char *) s) + 1);
40 FcMemAlloc (FC_MEM_STRING, strlen ((char *) s) + 1);
41 strcpy ((char *) r, (char *) s);
46 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
48 int l = strlen ((char *)s1) + strlen ((char *) s2) + 1;
49 FcChar8 *s = malloc (l);
53 FcMemAlloc (FC_MEM_STRING, l);
54 strcpy ((char *) s, (char *) s1);
55 strcat ((char *) s, (char *) s2);
60 FcStrFree (FcChar8 *s)
62 FcMemFree (FC_MEM_STRING, strlen ((char *) s) + 1);
67 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
75 if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
78 return (int) c1 - (int) c2;
82 FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
94 if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
97 return (int) c1 - (int) c2;
101 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
114 return (int) c1 - (int) c2;
118 * Is the head of s1 equal to s2?
122 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
134 if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
137 return c1 == c2 || !c2;
141 * Does s1 contain an instance of s2 (ignoring blanks and case)?
145 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
149 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
157 * Is the head of s1 equal to s2?
161 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
169 if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
172 return c1 == c2 || !c2;
176 * Does s1 contain an instance of s2 (ignoring blanks and case)?
180 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
184 if (FcStrIsAtIgnoreCase (s1, s2))
192 FcUtf8ToUcs4 (const FcChar8 *src_orig,
196 const FcChar8 *src = src_orig;
212 else if (!(s & 0x40))
216 else if (!(s & 0x20))
221 else if (!(s & 0x10))
226 else if (!(s & 0x08))
231 else if (!(s & 0x04))
236 else if ( ! (s & 0x02))
253 if ((s & 0xc0) != 0x80)
259 return src - src_orig;
263 FcUtf8Len (const FcChar8 *string,
277 clen = FcUtf8ToUcs4 (string, &c, len);
278 if (clen <= 0) /* malformed UTF8 string */
289 else if (max > 0x100)
297 FcUcs4ToUtf8 (FcChar32 ucs4,
298 FcChar8 dest[FC_UTF8_MAX_LEN])
303 if (ucs4 < 0x80) { *d++= ucs4; bits= -6; }
304 else if (ucs4 < 0x800) { *d++= ((ucs4 >> 6) & 0x1F) | 0xC0; bits= 0; }
305 else if (ucs4 < 0x10000) { *d++= ((ucs4 >> 12) & 0x0F) | 0xE0; bits= 6; }
306 else if (ucs4 < 0x200000) { *d++= ((ucs4 >> 18) & 0x07) | 0xF0; bits= 12; }
307 else if (ucs4 < 0x4000000) { *d++= ((ucs4 >> 24) & 0x03) | 0xF8; bits= 18; }
308 else if (ucs4 < 0x80000000) { *d++= ((ucs4 >> 30) & 0x01) | 0xFC; bits= 24; }
311 for ( ; bits >= 0; bits-= 6) {
312 *d++= ((ucs4 >> bits) & 0x3F) | 0x80;
317 #define GetUtf16(src,endian) \
318 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
319 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
322 FcUtf16ToUcs4 (const FcChar8 *src_orig,
325 int len) /* in bytes */
327 const FcChar8 *src = src_orig;
334 a = GetUtf16 (src, endian); src += 2; len -= 2;
337 * Check for surrogate
339 if ((a & 0xfc00) == 0xd800)
343 b = GetUtf16 (src, endian); src += 2; len -= 2;
345 * Check for invalid surrogate sequence
347 if ((b & 0xfc00) != 0xdc00)
349 result = ((((FcChar32) a & 0x3ff) << 10) |
350 ((FcChar32) b & 0x3ff)) + 0x10000;
355 return src - src_orig;
359 FcUtf16Len (const FcChar8 *string,
361 int len, /* in bytes */
374 clen = FcUtf16ToUcs4 (string, endian, &c, len);
375 if (clen <= 0) /* malformed UTF8 string */
386 else if (max > 0x100)
394 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
397 buf->allocated = FcFalse;
398 buf->failed = FcFalse;
404 FcStrBufDestroy (FcStrBuf *buf)
408 FcMemFree (FC_MEM_STRBUF, buf->size);
410 FcStrBufInit (buf, 0, 0);
415 FcStrBufDone (FcStrBuf *buf)
419 ret = malloc (buf->len + 1);
422 FcMemAlloc (FC_MEM_STRING, buf->len + 1);
423 memcpy (ret, buf->buf, buf->len);
424 ret[buf->len] = '\0';
426 FcStrBufDestroy (buf);
431 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
433 if (buf->len == buf->size)
440 size = buf->size * 2;
441 new = realloc (buf->buf, size);
445 size = buf->size + 1024;
449 buf->allocated = FcTrue;
450 memcpy (new, buf->buf, buf->len);
455 buf->failed = FcTrue;
459 FcMemFree (FC_MEM_STRBUF, buf->size);
460 FcMemAlloc (FC_MEM_STRBUF, size);
464 buf->buf[buf->len++] = c;
469 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
473 if (!FcStrBufChar (buf, c))
479 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
482 if (!FcStrBufChar (buf, *s++))
488 FcStrUsesHome (const FcChar8 *s)
494 FcStrCopyFilename (const FcChar8 *s)
500 FcChar8 *home = FcConfigHome ();
504 size = strlen ((char *) home) + strlen ((char *) s);
505 new = (FcChar8 *) malloc (size);
508 FcMemAlloc (FC_MEM_STRING, size);
509 strcpy ((char *) new, (char *) home);
510 strcat ((char *) new, (char *) s + 1);
514 int size = strlen ((char *) s) + 1;
515 new = (FcChar8 *) malloc (size);
518 FcMemAlloc (FC_MEM_STRING, size);
519 strcpy ((char *) new, (const char *) s);
525 FcStrLastSlash (const FcChar8 *path)
529 slash = (FcChar8 *) strrchr ((const char *) path, '/');
534 backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
535 if (!slash || (backslash && backslash > slash))
544 FcStrDirname (const FcChar8 *file)
549 slash = FcStrLastSlash (file);
551 return FcStrCopy ((FcChar8 *) ".");
552 dir = malloc ((slash - file) + 1);
555 FcMemAlloc (FC_MEM_STRING, (slash - file) + 1);
556 strncpy ((char *) dir, (const char *) file, slash - file);
557 dir[slash - file] = '\0';
562 FcStrBasename (const FcChar8 *file)
566 slash = FcStrLastSlash (file);
568 return FcStrCopy (file);
569 return FcStrCopy (slash + 1);
573 FcStrSetCreate (void)
575 FcStrSet *set = malloc (sizeof (FcStrSet));
578 FcMemAlloc (FC_MEM_STRSET, sizeof (FcStrSet));
587 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
589 if (FcStrSetMember (set, s))
594 if (set->num == set->size)
596 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
600 FcMemAlloc (FC_MEM_STRSET, (set->size + 2) * sizeof (FcChar8 *));
601 set->size = set->size + 1;
603 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
608 set->strs[set->num++] = s;
609 set->strs[set->num] = 0;
614 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
618 for (i = 0; i < set->num; i++)
619 if (!FcStrCmp (set->strs[i], s))
625 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
628 if (sa->num != sb->num)
630 for (i = 0; i < sa->num; i++)
631 if (!FcStrSetMember (sb, sa->strs[i]))
637 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
639 FcChar8 *new = FcStrCopy (s);
642 if (!_FcStrSetAppend (set, new))
651 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
653 FcChar8 *new = FcStrCopyFilename (s);
656 if (!_FcStrSetAppend (set, new))
665 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
669 for (i = 0; i < set->num; i++)
670 if (!FcStrCmp (set->strs[i], s))
672 FcStrFree (set->strs[i]);
674 * copy remaining string pointers and trailing
677 memmove (&set->strs[i], &set->strs[i+1],
678 (set->num - i) * sizeof (FcChar8 *));
686 FcStrSetDestroy (FcStrSet *set)
692 for (i = 0; i < set->num; i++)
693 FcStrFree (set->strs[i]);
694 FcMemFree (FC_MEM_STRSET, (set->size) * sizeof (FcChar8 *));
697 FcMemFree (FC_MEM_STRSET, sizeof (FcStrSet));
703 FcStrListCreate (FcStrSet *set)
707 list = malloc (sizeof (FcStrList));
710 FcMemAlloc (FC_MEM_STRLIST, sizeof (FcStrList));
718 FcStrListNext (FcStrList *list)
720 if (list->n >= list->set->num)
722 return list->set->strs[list->n++];
726 FcStrListDone (FcStrList *list)
728 FcStrSetDestroy (list->set);
729 FcMemFree (FC_MEM_STRLIST, sizeof (FcStrList));