]>
git.wh0rd.org - fontconfig.git/blob - src/fcstr.c
2 * $XFree86: xc/lib/fontconfig/src/fcstr.c,v 1.5 2002/05/29 22:07:33 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) c1
- (int) c2
;
86 FcStrCmpIgnoreBlanksAndCase (const FcChar8
*s1
, const FcChar8
*s2
)
105 return (int) c1
- (int) c2
;
109 FcStrCmp (const FcChar8
*s1
, const FcChar8
*s2
)
124 return (int) c1
- (int) c2
;
128 FcUtf8ToUcs4 (FcChar8
*src_orig
,
132 FcChar8
*src
= src_orig
;
148 else if (!(s
& 0x40))
152 else if (!(s
& 0x20))
157 else if (!(s
& 0x10))
162 else if (!(s
& 0x08))
167 else if (!(s
& 0x04))
172 else if ( ! (s
& 0x02))
189 if ((s
& 0xc0) != 0x80)
195 return src
- src_orig
;
199 FcUtf8Len (FcChar8
*string
,
213 clen
= FcUtf8ToUcs4 (string
, &c
, len
);
214 if (clen
<= 0) /* malformed UTF8 string */
225 else if (max
> 0x100)
233 FcStrBufInit (FcStrBuf
*buf
, FcChar8
*init
, int size
)
236 buf
->allocated
= FcFalse
;
237 buf
->failed
= FcFalse
;
243 FcStrBufDestroy (FcStrBuf
*buf
)
248 FcStrBufInit (buf
, 0, 0);
253 FcStrBufDone (FcStrBuf
*buf
)
257 ret
= malloc (buf
->len
+ 1);
260 memcpy (ret
, buf
->buf
, buf
->len
);
261 ret
[buf
->len
] = '\0';
263 FcStrBufDestroy (buf
);
268 FcStrBufChar (FcStrBuf
*buf
, FcChar8 c
)
270 if (buf
->len
== buf
->size
)
277 size
= buf
->size
* 2;
278 new = realloc (buf
->buf
, size
);
282 size
= buf
->size
+ 1024;
286 buf
->allocated
= FcTrue
;
287 memcpy (new, buf
->buf
, buf
->len
);
292 buf
->failed
= FcTrue
;
298 buf
->buf
[buf
->len
++] = c
;
303 FcStrBufString (FcStrBuf
*buf
, const FcChar8
*s
)
307 if (!FcStrBufChar (buf
, c
))
313 FcStrBufData (FcStrBuf
*buf
, const FcChar8
*s
, int len
)
316 if (!FcStrBufChar (buf
, *s
++))
322 FcStrCopyFilename (const FcChar8
*s
)
328 FcChar8
*home
= (FcChar8
*) getenv ("HOME");
329 int size
= strlen ((char *) home
) + strlen ((char *) s
);
332 new = (FcChar8
*) malloc (size
);
335 FcMemAlloc (FC_MEM_STRING
, size
);
336 strcpy ((char *) new, (char *) home
);
337 strcat ((char *) new, (char *) s
+ 1);
341 int size
= strlen ((char *) s
) + 1;
342 new = (FcChar8
*) malloc (size
);
345 FcMemAlloc (FC_MEM_STRING
, size
);
346 strcpy ((char *) new, (const char *) s
);
352 FcStrDirname (const FcChar8
*file
)
357 slash
= (FcChar8
*) strrchr ((char *) file
, '/');
359 return FcStrCopy ((FcChar8
*) ".");
360 dir
= malloc ((slash
- file
) + 1);
363 FcMemAlloc (FC_MEM_STRING
, (slash
- file
) + 1);
364 strncpy ((char *) dir
, (const char *) file
, slash
- file
);
365 dir
[slash
- file
] = '\0';
370 FcStrBasename (const FcChar8
*file
)
374 slash
= (FcChar8
*) strrchr ((char *) file
, '/');
376 return FcStrCopy (file
);
377 return FcStrCopy (slash
+ 1);
381 FcStrSetCreate (void)
383 FcStrSet
*set
= malloc (sizeof (FcStrSet
));
386 FcMemAlloc (FC_MEM_STRSET
, sizeof (FcStrSet
));
395 _FcStrSetAppend (FcStrSet
*set
, FcChar8
*s
)
397 if (FcStrSetMember (set
, s
))
402 if (set
->num
== set
->size
)
404 FcChar8
**strs
= malloc ((set
->size
+ 2) * sizeof (FcChar8
*));
408 FcMemAlloc (FC_MEM_STRSET
, (set
->size
+ 2) * sizeof (FcChar8
*));
409 set
->size
= set
->size
+ 1;
411 memcpy (strs
, set
->strs
, set
->num
* sizeof (FcChar8
*));
416 set
->strs
[set
->num
++] = s
;
417 set
->strs
[set
->num
] = 0;
422 FcStrSetMember (FcStrSet
*set
, const FcChar8
*s
)
426 for (i
= 0; i
< set
->num
; i
++)
427 if (!FcStrCmp (set
->strs
[i
], s
))
433 FcStrSetAdd (FcStrSet
*set
, const FcChar8
*s
)
435 FcChar8
*new = FcStrCopy (s
);
438 if (!_FcStrSetAppend (set
, new))
447 FcStrSetAddFilename (FcStrSet
*set
, const FcChar8
*s
)
449 FcChar8
*new = FcStrCopyFilename (s
);
452 if (!_FcStrSetAppend (set
, new))
461 FcStrSetDel (FcStrSet
*set
, const FcChar8
*s
)
465 for (i
= 0; i
< set
->num
; i
++)
466 if (!FcStrCmp (set
->strs
[i
], s
))
468 FcStrFree (set
->strs
[i
]);
470 * copy remaining string pointers and trailing
473 memmove (&set
->strs
[i
], &set
->strs
[i
+1],
474 (set
->num
- i
) * sizeof (FcChar8
*));
482 FcStrSetDestroy (FcStrSet
*set
)
488 for (i
= 0; i
< set
->num
; i
++)
489 FcStrFree (set
->strs
[i
]);
490 FcMemFree (FC_MEM_STRSET
, (set
->size
) * sizeof (FcChar8
*));
493 FcMemFree (FC_MEM_STRSET
, sizeof (FcStrSet
));
499 FcStrListCreate (FcStrSet
*set
)
503 list
= malloc (sizeof (FcStrList
));
506 FcMemAlloc (FC_MEM_STRLIST
, sizeof (FcStrList
));
514 FcStrListNext (FcStrList
*list
)
516 if (list
->n
>= list
->set
->num
)
518 return list
->set
->strs
[list
->n
++];
522 FcStrListDone (FcStrList
*list
)
524 FcStrSetDestroy (list
->set
);
525 FcMemFree (FC_MEM_STRLIST
, sizeof (FcStrList
));