2 * $XFree86: xc/lib/fontconfig/src/fcstr.c,v 1.3 2002/02/18 22:29:28 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)
82 return (int) c2 - (int) c1;
86 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
101 return (int) c2 - (int) c1;
105 FcUtf8ToUcs4 (FcChar8 *src_orig,
109 FcChar8 *src = src_orig;
125 else if (!(s & 0x40))
129 else if (!(s & 0x20))
134 else if (!(s & 0x10))
139 else if (!(s & 0x08))
144 else if (!(s & 0x04))
149 else if ( ! (s & 0x02))
166 if ((s & 0xc0) != 0x80)
172 return src - src_orig;
176 FcUtf8Len (FcChar8 *string,
190 clen = FcUtf8ToUcs4 (string, &c, len);
191 if (clen <= 0) /* malformed UTF8 string */
202 else if (max > 0x100)
210 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
213 buf->allocated = FcFalse;
214 buf->failed = FcFalse;
220 FcStrBufDestroy (FcStrBuf *buf)
225 FcStrBufInit (buf, 0, 0);
230 FcStrBufDone (FcStrBuf *buf)
234 ret = malloc (buf->len + 1);
237 memcpy (ret, buf->buf, buf->len);
238 ret[buf->len] = '\0';
240 FcStrBufDestroy (buf);
245 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
247 if (buf->len == buf->size)
254 size = buf->size * 2;
255 new = realloc (buf->buf, size);
259 size = buf->size + 1024;
263 buf->allocated = FcTrue;
264 memcpy (new, buf->buf, buf->len);
269 buf->failed = FcTrue;
275 buf->buf[buf->len++] = c;
280 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
284 if (!FcStrBufChar (buf, c))
290 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
293 if (!FcStrBufChar (buf, *s++))
299 FcStrCopyFilename (const FcChar8 *s)
305 FcChar8 *home = (FcChar8 *) getenv ("HOME");
306 int size = strlen ((char *) home) + strlen ((char *) s);
309 new = (FcChar8 *) malloc (size);
312 FcMemAlloc (FC_MEM_STRING, size);
313 strcpy ((char *) new, (char *) home);
314 strcat ((char *) new, (char *) s + 1);
318 int size = strlen ((char *) s) + 1;
319 new = (FcChar8 *) malloc (size);
322 FcMemAlloc (FC_MEM_STRING, size);
323 strcpy ((char *) new, (const char *) s);
329 FcStrDirname (const FcChar8 *file)
334 slash = (FcChar8 *) strrchr ((char *) file, '/');
336 return FcStrCopy ((FcChar8 *) ".");
337 dir = malloc ((slash - file) + 1);
340 FcMemAlloc (FC_MEM_STRING, (slash - file) + 1);
341 strncpy ((char *) dir, (const char *) file, slash - file);
342 dir[slash - file] = '\0';
347 FcStrBasename (const FcChar8 *file)
351 slash = (FcChar8 *) strrchr ((char *) file, '/');
353 return FcStrCopy (file);
354 return FcStrCopy (slash + 1);
358 FcStrSetCreate (void)
360 FcStrSet *set = malloc (sizeof (FcStrSet));
363 FcMemAlloc (FC_MEM_STRSET, sizeof (FcStrSet));
372 _FcStrSetAppend (FcStrSet *set, FcChar8 *s)
374 if (FcStrSetMember (set, s))
379 if (set->num == set->size)
381 FcChar8 **strs = malloc ((set->size + 2) * sizeof (FcChar8 *));
385 FcMemAlloc (FC_MEM_STRSET, (set->size + 2) * sizeof (FcChar8 *));
386 set->size = set->size + 1;
388 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
393 set->strs[set->num++] = s;
394 set->strs[set->num] = 0;
399 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
403 for (i = 0; i < set->num; i++)
404 if (!FcStrCmp (set->strs[i], s))
410 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
412 FcChar8 *new = FcStrCopy (s);
415 if (!_FcStrSetAppend (set, new))
424 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
426 FcChar8 *new = FcStrCopyFilename (s);
429 if (!_FcStrSetAppend (set, new))
438 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
442 for (i = 0; i < set->num; i++)
443 if (!FcStrCmp (set->strs[i], s))
445 FcStrFree (set->strs[i]);
447 * copy remaining string pointers and trailing
450 memmove (&set->strs[i], &set->strs[i+1],
451 (set->num - i) * sizeof (FcChar8 *));
459 FcStrSetDestroy (FcStrSet *set)
465 for (i = 0; i < set->num; i++)
466 FcStrFree (set->strs[i]);
467 FcMemFree (FC_MEM_STRSET, (set->size) * sizeof (FcChar8 *));
470 FcMemFree (FC_MEM_STRSET, sizeof (FcStrSet));
476 FcStrListCreate (FcStrSet *set)
480 list = malloc (sizeof (FcStrList));
483 FcMemAlloc (FC_MEM_STRLIST, sizeof (FcStrList));
491 FcStrListNext (FcStrList *list)
493 if (list->n >= list->set->num)
495 return list->set->strs[list->n++];
499 FcStrListDone (FcStrList *list)
501 FcStrSetDestroy (list->set);
502 FcMemFree (FC_MEM_STRLIST, sizeof (FcStrList));