]>
git.wh0rd.org - fontconfig.git/blob - src/fcstr.c
2 * $XFree86: xc/lib/fontconfig/src/fcstr.c,v 1.10 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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
)
85 return (int) c1
- (int) c2
;
89 FcStrCmpIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
108 return (int) c1
- (int) c2
;
112 FcStrCmp (const FcChar8
*s1
, const FcChar8
*s2
)
127 return (int) c1
- (int) c2
;
131 FcUtf8ToUcs4 (const FcChar8
*src_orig
,
135 const FcChar8
*src
= src_orig
;
151 else if (!(s
& 0x40))
155 else if (!(s
& 0x20))
160 else if (!(s
& 0x10))
165 else if (!(s
& 0x08))
170 else if (!(s
& 0x04))
175 else if ( ! (s
& 0x02))
192 if ((s
& 0xc0) != 0x80)
198 return src
- src_orig
;
202 FcUtf8Len (const FcChar8
*string
,
216 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
217 if (clen
<= 0) /* malformed UTF8 string */
228 else if (max
> 0x100)
236 FcUcs4ToUtf8 (FcChar32 ucs4
,
237 FcChar8 dest
[FC_UTF8_MAX_LEN
])
242 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
243 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
244 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
245 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
246 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
247 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
250 for ( ; bits
>= 0; bits
-= 6) {
251 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
256 #define GetUtf16(src,endian) \
257 ((FcChar16) ((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
258 (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
261 FcUtf16ToUcs4 (const FcChar8
*src_orig
,
264 int len
) /* in bytes */
266 const FcChar8
*src
= src_orig
;
273 a
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
276 * Check for surrogate
278 if ((a
& 0xfc00) == 0xd800)
282 b
= GetUtf16 (src
, endian
); src
+= 2; len
-= 2;
284 * Check for invalid surrogate sequence
286 if ((b
& 0xfc00) != 0xdc00)
288 result
= ((((FcChar32
) a
& 0x3ff) << 10) |
289 ((FcChar32
) b
& 0x3ff)) + 0x10000;
294 return src
- src_orig
;
298 FcUtf16Len (const FcChar8
*string
,
300 int len
, /* in bytes */
313 clen
= FcUtf16ToUcs4 (string
, endian
, &c
, len
);
314 if (clen
<= 0) /* malformed UTF8 string */
325 else if (max
> 0x100)
333 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
336 buf
->allocated
= FcFalse
;
337 buf
->failed
= FcFalse
;
343 FcStrBufDestroy (FcStrBuf
*buf
)
347 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
349 FcStrBufInit (buf
, 0, 0);
354 FcStrBufDone (FcStrBuf
*buf
)
358 ret
= malloc (buf
->len
+ 1);
361 FcMemAlloc (FC_MEM_STRING
, buf
->len
+ 1);
362 memcpy (ret
, buf
->buf
, buf
->len
);
363 ret
[buf
->len
] = '\0';
365 FcStrBufDestroy (buf
);
370 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
372 if (buf
->len
== buf
->size
)
379 size
= buf
->size
* 2;
380 new = realloc (buf
->buf
, size
);
384 size
= buf
->size
+ 1024;
388 buf
->allocated
= FcTrue
;
389 memcpy (new, buf
->buf
, buf
->len
);
394 buf
->failed
= FcTrue
;
398 FcMemFree (FC_MEM_STRBUF
, buf
->size
);
399 FcMemAlloc (FC_MEM_STRBUF
, size
);
403 buf
->buf
[buf
->len
++] = c
;
408 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
412 if (!FcStrBufChar (buf
, c
))
418 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
421 if (!FcStrBufChar (buf
, *s
++))
427 FcStrUsesHome (const FcChar8
*s
)
433 FcStrCopyFilename (const FcChar8
*s
)
439 FcChar8
*home
= FcConfigHome ();
443 size
= strlen ((char *) home
) + strlen ((char *) s
);
444 new = (FcChar8
*) malloc (size
);
447 FcMemAlloc (FC_MEM_STRING
, size
);
448 strcpy ((char *) new, (char *) home
);
449 strcat ((char *) new, (char *) s
+ 1);
453 int size
= strlen ((char *) s
) + 1;
454 new = (FcChar8
*) malloc (size
);
457 FcMemAlloc (FC_MEM_STRING
, size
);
458 strcpy ((char *) new, (const char *) s
);
464 FcStrDirname (const FcChar8
*file
)
469 slash
= (FcChar8
*) strrchr ((char *) file
, '/');
471 return FcStrCopy ((FcChar8
*) ".");
472 dir
= malloc ((slash
- file
) + 1);
475 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
476 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
477 dir
[slash
- file
] = '\0';
482 FcStrBasename (const FcChar8
*file
)
486 slash
= (FcChar8
*) strrchr ((char *) file
, '/');
488 return FcStrCopy (file
);
489 return FcStrCopy (slash
+ 1);
493 FcStrSetCreate (void)
495 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
498 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
507 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
509 if (FcStrSetMember (set
, s
))
514 if (set
->num
== set
->size
)
516 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
520 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
521 set
->size
= set
->size
+ 1;
523 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
528 set
->strs
[set
->num
++] = s
;
529 set
->strs
[set
->num
] = 0;
534 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
538 for (i
= 0; i
< set
->num
; i
++)
539 if (!FcStrCmp (set
->strs
[i
], s
))
545 FcStrSetEqual (FcStrSet
*sa
, FcStrSet
*sb
)
548 if (sa
->num
!= sb
->num
)
550 for (i
= 0; i
< sa
->num
; i
++)
551 if (!FcStrSetMember (sb
, sa
->strs
[i
]))
557 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
559 FcChar8
*new = FcStrCopy (s
);
562 if (!_FcStrSetAppend (set
, new))
571 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
573 FcChar8
*new = FcStrCopyFilename (s
);
576 if (!_FcStrSetAppend (set
, new))
585 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
589 for (i
= 0; i
< set
->num
; i
++)
590 if (!FcStrCmp (set
->strs
[i
], s
))
592 FcStrFree (set
->strs
[i
]);
594 * copy remaining string pointers and trailing
597 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
598 (set
->num
- i
) * sizeof (FcChar8
*));
606 FcStrSetDestroy (FcStrSet
*set
)
612 for (i
= 0; i
< set
->num
; i
++)
613 FcStrFree (set
->strs
[i
]);
614 FcMemFree (FC_MEM_STRSET
, (set
->size
) * sizeof (FcChar8
*));
617 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
623 FcStrListCreate (FcStrSet
*set
)
627 list
= malloc (sizeof (FcStrList
));
630 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
638 FcStrListNext (FcStrList
*list
)
640 if (list
->n
>= list
->set
->num
)
642 return list
->set
->strs
[list
->n
++];
646 FcStrListDone (FcStrList
*list
)
648 FcStrSetDestroy (list
->set
);
649 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));