]>
git.wh0rd.org - fontconfig.git/blob - src/fcstr.c
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 FcStrStrIgnoreCase (const FcChar8
*s1
, const FcChar8
*s2
)
195 const FcChar8
* p
= s1
;
196 const FcChar8
* b
= s2
;
215 if (!c1
|| (c1
= FcToLower (c1
)) == c2
)
226 if (c1
&& c2
&& (c1
= FcToLower (c1
)) != (c2
= FcToLower (c2
)))
244 FcStrStr (const FcChar8
*s1
, const FcChar8
*s2
)
247 const FcChar8
* p
= s1
;
248 const FcChar8
* b
= s2
;
277 if (c1
&& c2
&& c1
!= c2
)
295 FcUtf8ToUcs4 (const FcChar8
*src_orig
,
299 const FcChar8
*src
= src_orig
;
315 else if (!(s
& 0x40))
319 else if (!(s
& 0x20))
324 else if (!(s
& 0x10))
329 else if (!(s
& 0x08))
334 else if (!(s
& 0x04))
339 else if ( ! (s
& 0x02))
356 if ((s
& 0xc0) != 0x80)
362 return src
- src_orig
;
366 FcUtf8Len (const FcChar8
*string
,
380 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
381 if (clen
<= 0) /* malformed UTF8 string */
392 else if (max
> 0x100)
400 FcUcs4ToUtf8 (FcChar32 ucs4
,
401 FcChar8 dest
[FC_UTF8_MAX_LEN
])
406 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
407 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
408 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
409 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
410 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
411 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
414 for ( ; bits
>= 0; bits
-= 6) {
415 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
420 #define GetUtf16(src,endian) \
421 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
422 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
425 FcUtf16ToUcs4 (const FcChar8
*src_orig
,
428 int len
) /* in bytes */
430 const FcChar8
*src
= src_orig
;
437 a
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
440 * Check for surrogate
442 if ((a
& 0xfc00) == 0xd800)
446 b
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
448 * Check for invalid surrogate sequence
450 if ((b
& 0xfc00) != 0xdc00)
452 result
= ((((FcChar32
) a
& 0x3ff) << 10) |
453 ((FcChar32
) b
& 0x3ff)) + 0x10000;
458 return src
- src_orig
;
462 FcUtf16Len (const FcChar8
*string
,
464 int len
, /* in bytes */
477 clen
= FcUtf16ToUcs4 (string
, endian
, &c
, len
);
478 if (clen
<= 0) /* malformed UTF8 string */
489 else if (max
> 0x100)
497 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
500 buf
->allocated
= FcFalse
;
501 buf
->failed
= FcFalse
;
507 FcStrBufDestroy (FcStrBuf
*buf
)
511 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
513 FcStrBufInit (buf
, 0, 0);
518 FcStrBufDone (FcStrBuf
*buf
)
522 ret
= malloc (buf
->len
+ 1);
525 FcMemAlloc (FC_MEM_STRING
, buf
->len
+ 1);
526 memcpy (ret
, buf
->buf
, buf
->len
);
527 ret
[buf
->len
] = '\0';
529 FcStrBufDestroy (buf
);
534 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
536 if (buf
->len
== buf
->size
)
543 size
= buf
->size
* 2;
544 new = realloc (buf
->buf
, size
);
548 size
= buf
->size
+ 1024;
552 buf
->allocated
= FcTrue
;
553 memcpy (new, buf
->buf
, buf
->len
);
558 buf
->failed
= FcTrue
;
562 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
563 FcMemAlloc (FC_MEM_STRBUF
, size
);
567 buf
->buf
[buf
->len
++] = c
;
572 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
576 if (!FcStrBufChar (buf
, c
))
582 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
585 if (!FcStrBufChar (buf
, *s
++))
591 FcStrUsesHome (const FcChar8
*s
)
597 FcStrCopyFilename (const FcChar8
*s
)
603 FcChar8
*home
= FcConfigHome ();
607 size
= strlen ((char *) home
) + strlen ((char *) s
);
608 new = (FcChar8
*) malloc (size
);
611 FcMemAlloc (FC_MEM_STRING
, size
);
612 strcpy ((char *) new, (char *) home
);
613 strcat ((char *) new, (char *) s
+ 1);
617 int size
= strlen ((char *) s
) + 1;
618 new = (FcChar8
*) malloc (size
);
621 FcMemAlloc (FC_MEM_STRING
, size
);
622 strcpy ((char *) new, (const char *) s
);
628 FcStrLastSlash (const FcChar8
*path
)
632 slash
= (FcChar8
*) strrchr ((const char *) path
, '/');
637 backslash
= (FcChar8
*) strrchr ((const char *) path
, '\\');
638 if (!slash
|| (backslash
&& backslash
> slash
))
647 FcStrDirname (const FcChar8
*file
)
652 slash
= FcStrLastSlash (file
);
654 return FcStrCopy ((FcChar8
*) ".");
655 dir
= malloc ((slash
- file
) + 1);
658 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
659 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
660 dir
[slash
- file
] = '\0';
665 FcStrBasename (const FcChar8
*file
)
669 slash
= FcStrLastSlash (file
);
671 return FcStrCopy (file
);
672 return FcStrCopy (slash
+ 1);
676 FcStrSetCreate (void)
678 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
681 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
690 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
692 if (FcStrSetMember (set
, s
))
697 if (set
->num
== set
->size
)
699 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
703 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
704 set
->size
= set
->size
+ 1;
706 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
711 set
->strs
[set
->num
++] = s
;
712 set
->strs
[set
->num
] = 0;
717 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
721 for (i
= 0; i
< set
->num
; i
++)
722 if (!FcStrCmp (set
->strs
[i
], s
))
728 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
731 if (sa
->num
!= sb
->num
)
733 for (i
= 0; i
< sa
->num
; i
++)
734 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
740 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
742 FcChar8
*new = FcStrCopy (s
);
745 if (!_FcStrSetAppend (set
, new))
754 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
756 FcChar8
*new = FcStrCopyFilename (s
);
759 if (!_FcStrSetAppend (set
, new))
768 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
772 for (i
= 0; i
< set
->num
; i
++)
773 if (!FcStrCmp (set
->strs
[i
], s
))
775 FcStrFree (set
->strs
[i
]);
777 * copy remaining string pointers and trailing
780 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
781 (set
->num
- i
) * sizeof (FcChar8
*));
789 FcStrSetDestroy (FcStrSet
*set
)
795 for (i
= 0; i
< set
->num
; i
++)
796 FcStrFree (set
->strs
[i
]);
797 FcMemFree (FC_MEM_STRSET
, (set
->size
) * sizeof (FcChar8
*));
800 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
806 FcStrListCreate (FcStrSet
*set
)
810 list
= malloc (sizeof (FcStrList
));
813 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
821 FcStrListNext (FcStrList
*list
)
823 if (list
->n
>= list
->set
->num
)
825 return list
->set
->strs
[list
->n
++];
829 FcStrListDone (FcStrList
*list
)
831 FcStrSetDestroy (list
->set
);
832 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));