]>
git.wh0rd.org - fontconfig.git/blob - src/fccache.c
2 * $XFree86: xc/lib/fontconfig/src/fccache.c,v 1.4 2002/03/01 01:00:54 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.
28 FcFileCacheHash (const FcChar8
*string
)
33 while ((c
= *string
++))
39 FcFileCacheFind (FcFileCache
*cache
,
46 FcFileCacheEnt
*c
, *name
;
52 hash
= FcFileCacheHash (match
);
55 for (c
= cache
->ents
[hash
% FC_FILE_CACHE_HASH_SIZE
]; c
; c
= c
->next
)
57 if (c
->hash
== hash
&& !strcmp ((const char *) match
, (const char *) c
->file
))
63 if (stat ((char *) file
, &statb
) < 0)
65 if (FcDebug () & FC_DBG_CACHE
)
66 printf (" file missing\n");
69 if (statb
.st_mtime
!= c
->time
)
71 if (FcDebug () & FC_DBG_CACHE
)
72 printf (" timestamp mismatch (was %d is %d)\n",
73 (int) c
->time
, (int) statb
.st_mtime
);
79 c
->referenced
= FcTrue
;
92 * Cache file syntax is quite simple:
94 * "file_name" id time "font_name" \n
98 FcFileCacheReadString (FILE *f
, FcChar8
*dest
, int len
)
106 while ((c
= getc (f
)) != EOF
)
118 while ((c
= getc (f
)) != EOF
)
133 FcChar8
*new = malloc (size
* 2);
136 memcpy (new, d
, size
);
153 FcFileCacheReadUlong (FILE *f
, unsigned long *dest
)
158 while ((c
= getc (f
)) != EOF
)
168 if (c
== EOF
|| isspace (c
))
172 t
= t
* 10 + (c
- '0');
180 FcFileCacheReadInt (FILE *f
, int *dest
)
185 ret
= FcFileCacheReadUlong (f
, &t
);
192 FcFileCacheReadTime (FILE *f
, time_t *dest
)
197 ret
= FcFileCacheReadUlong (f
, &t
);
204 FcFileCacheAdd (FcFileCache
*cache
,
212 FcFileCacheEnt
**prev
, *old
;
215 if (FcDebug () & FC_DBG_CACHE
)
217 printf ("%s face %s/%d as %s\n", replace
? "Replace" : "Add",
220 hash
= FcFileCacheHash (file
);
221 for (prev
= &cache
->ents
[hash
% FC_FILE_CACHE_HASH_SIZE
];
223 prev
= &(*prev
)->next
)
225 if (old
->hash
== hash
&& old
->id
== id
&& !strcmp ((const char *) old
->file
,
226 (const char *) file
))
242 c
= malloc (sizeof (FcFileCacheEnt
) +
243 strlen ((char *) file
) + 1 +
244 strlen ((char *) name
) + 1);
250 c
->file
= (FcChar8
*) (c
+ 1);
252 c
->name
= c
->file
+ strlen ((char *) file
) + 1;
253 strcpy ((char *) c
->file
, (const char *) file
);
255 c
->referenced
= replace
;
256 strcpy ((char *) c
->name
, (const char *) name
);
262 FcFileCacheCreate (void)
267 cache
= malloc (sizeof (FcFileCache
));
270 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
273 cache
->referenced
= 0;
274 cache
->updated
= FcFalse
;
279 FcFileCacheDestroy (FcFileCache
*cache
)
281 FcFileCacheEnt
*c
, *next
;
284 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
286 for (c
= cache
->ents
[h
]; c
; c
= next
)
296 FcFileCacheLoad (FcFileCache
*cache
,
297 const FcChar8
*cache_file
)
300 FcChar8 file_buf
[8192], *file
;
303 FcChar8 name_buf
[8192], *name
;
305 f
= fopen ((char *) cache_file
, "r");
309 cache
->updated
= FcFalse
;
312 while ((file
= FcFileCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
313 FcFileCacheReadInt (f
, &id
) &&
314 FcFileCacheReadTime (f
, &time
) &&
315 (name
= FcFileCacheReadString (f
, name_buf
, sizeof (name_buf
))))
317 (void) FcFileCacheAdd (cache
, file
, id
, time
, name
, FcFalse
);
318 if (file
!= file_buf
)
320 if (name
!= name_buf
)
325 if (file
&& file
!= file_buf
)
327 if (name
&& name
!= name_buf
)
333 FcFileCacheUpdate (FcFileCache
*cache
,
338 const FcChar8
*match
;
344 if (stat ((char *) file
, &statb
) < 0)
346 ret
= FcFileCacheAdd (cache
, match
, id
,
347 statb
.st_mtime
, name
, FcTrue
);
349 cache
->updated
= FcTrue
;
354 FcFileCacheWriteString (FILE *f
, const FcChar8
*string
)
358 if (putc ('"', f
) == EOF
)
360 while ((c
= *string
++))
365 if (putc ('\\', f
) == EOF
)
369 if (putc (c
, f
) == EOF
)
373 if (putc ('"', f
) == EOF
)
379 FcFileCacheWriteUlong (FILE *f
, unsigned long t
)
382 unsigned long temp
, digit
;
395 if (putc ((char) digit
+ '0', f
) == EOF
)
397 temp
= temp
- pow
* digit
;
404 FcFileCacheWriteInt (FILE *f
, int i
)
406 return FcFileCacheWriteUlong (f
, (unsigned long) i
);
410 FcFileCacheWriteTime (FILE *f
, time_t t
)
412 return FcFileCacheWriteUlong (f
, (unsigned long) t
);
416 FcFileCacheSave (FcFileCache
*cache
,
417 const FcChar8
*cache_file
)
424 if (!cache
->updated
&& cache
->referenced
== cache
->entries
)
427 atomic
= FcAtomicCreate (cache_file
);
430 if (!FcAtomicLock (atomic
))
432 f
= fopen ((char *) FcAtomicNewFile(atomic
), "w");
436 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
438 for (c
= cache
->ents
[h
]; c
; c
= c
->next
)
442 if (!FcFileCacheWriteString (f
, c
->file
))
444 if (putc (' ', f
) == EOF
)
446 if (!FcFileCacheWriteInt (f
, c
->id
))
448 if (putc (' ', f
) == EOF
)
450 if (!FcFileCacheWriteTime (f
, c
->time
))
452 if (putc (' ', f
) == EOF
)
454 if (!FcFileCacheWriteString (f
, c
->name
))
456 if (putc ('\n', f
) == EOF
)
461 if (fclose (f
) == EOF
)
464 if (!FcAtomicReplaceOrig (atomic
))
467 FcAtomicUnlock (atomic
);
468 FcAtomicDestroy (atomic
);
470 cache
->updated
= FcFalse
;
476 FcAtomicDeleteNew (atomic
);
478 FcAtomicUnlock (atomic
);
480 FcAtomicDestroy (atomic
);
486 FcFileCacheReadDir (FcFontSet
*set
, const FcChar8
*cache_file
)
492 FcChar8 file_buf
[8192], *file
;
494 FcChar8 name_buf
[8192], *name
;
495 FcBool ret
= FcFalse
;
497 if (FcDebug () & FC_DBG_CACHE
)
499 printf ("FcFileCacheReadDir cache_file \"%s\"\n", cache_file
);
502 f
= fopen ((char *) cache_file
, "r");
505 if (FcDebug () & FC_DBG_CACHE
)
507 printf (" no cache file\n");
512 base
= (FcChar8
*) strrchr ((char *) cache_file
, '/');
516 path
= malloc (base
- cache_file
+ 8192 + 1);
519 memcpy (path
, cache_file
, base
- cache_file
);
520 base
= path
+ (base
- cache_file
);
524 while ((file
= FcFileCacheReadString (f
, file_buf
, sizeof (file_buf
))) &&
525 FcFileCacheReadInt (f
, &id
) &&
526 (name
= FcFileCacheReadString (f
, name_buf
, sizeof (name_buf
))))
528 font
= FcNameParse (name
);
531 strcpy ((char *) base
, (const char *) file
);
532 if (FcDebug () & FC_DBG_CACHEV
)
534 printf (" dir cache file \"%s\"\n", file
);
536 FcPatternAddString (font
, FC_FILE
, path
);
537 if (!FcFontSetAdd (set
, font
))
540 if (file
!= file_buf
)
542 if (name
!= name_buf
)
546 if (FcDebug () & FC_DBG_CACHE
)
548 printf (" cache loaded\n");
554 if (file
&& file
!= file_buf
)
556 if (name
&& name
!= name_buf
)
565 FcFileCacheWriteDir (FcFontSet
*set
, const FcChar8
*cache_file
)
570 const FcChar8
*file
, *base
;
575 if (FcDebug () & FC_DBG_CACHE
)
576 printf ("FcFileCacheWriteDir cache_file \"%s\"\n", cache_file
);
578 f
= fopen ((char *) cache_file
, "w");
581 if (FcDebug () & FC_DBG_CACHE
)
582 printf (" can't create \"%s\"\n", cache_file
);
585 for (n
= 0; n
< set
->nfont
; n
++)
587 font
= set
->fonts
[n
];
588 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
590 base
= (FcChar8
*) strrchr ((char *) file
, '/');
595 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
597 if (FcDebug () & FC_DBG_CACHEV
)
598 printf (" write file \"%s\"\n", base
);
599 if (!FcFileCacheWriteString (f
, base
))
601 if (putc (' ', f
) == EOF
)
603 if (!FcFileCacheWriteInt (f
, id
))
605 if (putc (' ', f
) == EOF
)
607 name
= FcNameUnparse (font
);
610 ret
= FcFileCacheWriteString (f
, name
);
614 if (putc ('\n', f
) == EOF
)
617 if (fclose (f
) == EOF
)
620 if (FcDebug () & FC_DBG_CACHE
)
621 printf (" cache written\n");
627 unlink ((char *) cache_file
);