]>
git.wh0rd.org - fontconfig.git/blob - src/fccache.c
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 char *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 (match
, c
->file
))
63 if (stat (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
, char *dest
, int len
)
103 while ((c
= getc (f
)) != EOF
)
112 while ((c
= getc (f
)) != EOF
)
134 FcFileCacheReadUlong (FILE *f
, unsigned long *dest
)
139 while ((c
= getc (f
)) != EOF
)
149 if (c
== EOF
|| isspace (c
))
153 t
= t
* 10 + (c
- '0');
161 FcFileCacheReadInt (FILE *f
, int *dest
)
166 ret
= FcFileCacheReadUlong (f
, &t
);
173 FcFileCacheReadTime (FILE *f
, time_t *dest
)
178 ret
= FcFileCacheReadUlong (f
, &t
);
185 FcFileCacheAdd (FcFileCache
*cache
,
193 FcFileCacheEnt
**prev
, *old
;
196 if (FcDebug () & FC_DBG_CACHE
)
198 printf ("%s face %s/%d as %s\n", replace
? "Replace" : "Add",
201 hash
= FcFileCacheHash (file
);
202 for (prev
= &cache
->ents
[hash
% FC_FILE_CACHE_HASH_SIZE
];
204 prev
= &(*prev
)->next
)
206 if (old
->hash
== hash
&& old
->id
== id
&& !strcmp (old
->file
, file
))
222 c
= malloc (sizeof (FcFileCacheEnt
) +
230 c
->file
= (char *) (c
+ 1);
232 c
->name
= c
->file
+ strlen (file
) + 1;
233 strcpy (c
->file
, file
);
235 c
->referenced
= replace
;
236 strcpy (c
->name
, name
);
242 FcFileCacheCreate (void)
247 cache
= malloc (sizeof (FcFileCache
));
250 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
253 cache
->referenced
= 0;
254 cache
->updated
= FcFalse
;
259 FcFileCacheDestroy (FcFileCache
*cache
)
261 FcFileCacheEnt
*c
, *next
;
264 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
266 for (c
= cache
->ents
[h
]; c
; c
= next
)
276 FcFileCacheLoad (FcFileCache
*cache
,
277 const char *cache_file
)
285 f
= fopen (cache_file
, "r");
289 cache
->updated
= FcFalse
;
290 while (FcFileCacheReadString (f
, file
, sizeof (file
)) &&
291 FcFileCacheReadInt (f
, &id
) &&
292 FcFileCacheReadTime (f
, &time
) &&
293 FcFileCacheReadString (f
, name
, sizeof (name
)))
295 (void) FcFileCacheAdd (cache
, file
, id
, time
, name
, FcFalse
);
301 FcFileCacheUpdate (FcFileCache
*cache
,
312 if (stat (file
, &statb
) < 0)
314 ret
= FcFileCacheAdd (cache
, match
, id
,
315 statb
.st_mtime
, name
, FcTrue
);
317 cache
->updated
= FcTrue
;
322 FcFileCacheWriteString (FILE *f
, char *string
)
326 if (putc ('"', f
) == EOF
)
328 while ((c
= *string
++))
333 if (putc ('\\', f
) == EOF
)
337 if (putc (c
, f
) == EOF
)
341 if (putc ('"', f
) == EOF
)
347 FcFileCacheWriteUlong (FILE *f
, unsigned long t
)
350 unsigned long temp
, digit
;
363 if (putc ((char) digit
+ '0', f
) == EOF
)
365 temp
= temp
- pow
* digit
;
372 FcFileCacheWriteInt (FILE *f
, int i
)
374 return FcFileCacheWriteUlong (f
, (unsigned long) i
);
378 FcFileCacheWriteTime (FILE *f
, time_t t
)
380 return FcFileCacheWriteUlong (f
, (unsigned long) t
);
384 FcFileCacheSave (FcFileCache
*cache
,
385 const char *cache_file
)
393 if (!cache
->updated
&& cache
->referenced
== cache
->entries
)
396 lck
= malloc (strlen (cache_file
)*2 + 4);
399 tmp
= lck
+ strlen (cache_file
) + 2;
400 strcpy (lck
, cache_file
);
402 strcpy (tmp
, cache_file
);
404 if (link (lck
, cache_file
) < 0 && errno
!= ENOENT
)
406 if (access (tmp
, F_OK
) == 0)
408 f
= fopen (tmp
, "w");
412 for (h
= 0; h
< FC_FILE_CACHE_HASH_SIZE
; h
++)
414 for (c
= cache
->ents
[h
]; c
; c
= c
->next
)
418 if (!FcFileCacheWriteString (f
, c
->file
))
420 if (putc (' ', f
) == EOF
)
422 if (!FcFileCacheWriteInt (f
, c
->id
))
424 if (putc (' ', f
) == EOF
)
426 if (!FcFileCacheWriteTime (f
, c
->time
))
428 if (putc (' ', f
) == EOF
)
430 if (!FcFileCacheWriteString (f
, c
->name
))
432 if (putc ('\n', f
) == EOF
)
437 if (fclose (f
) == EOF
)
440 if (rename (tmp
, cache_file
) < 0)
444 cache
->updated
= FcFalse
;
460 FcFileCacheReadDir (FcFontSet
*set
, const char *cache_file
)
469 FcBool ret
= FcFalse
;
471 if (FcDebug () & FC_DBG_CACHE
)
473 printf ("FcFileCacheReadDir cache_file \"%s\"\n", cache_file
);
476 f
= fopen (cache_file
, "r");
479 if (FcDebug () & FC_DBG_CACHE
)
481 printf (" no cache file\n");
486 base
= strrchr (cache_file
, '/');
490 path
= malloc (base
- cache_file
+ 8192 + 1);
493 memcpy (path
, cache_file
, base
- cache_file
);
494 base
= path
+ (base
- cache_file
);
496 while (FcFileCacheReadString (f
, file
, sizeof (file
)) &&
497 FcFileCacheReadInt (f
, &id
) &&
498 FcFileCacheReadString (f
, name
, sizeof (name
)))
500 font
= FcNameParse (name
);
504 if (FcDebug () & FC_DBG_CACHEV
)
506 printf (" dir cache file \"%s\"\n", file
);
508 FcPatternAddString (font
, FC_FILE
, path
);
509 if (!FcFontSetAdd (set
, font
))
513 if (FcDebug () & FC_DBG_CACHE
)
515 printf (" cache loaded\n");
528 FcFileCacheWriteDir (FcFontSet
*set
, const char *cache_file
)
538 if (FcDebug () & FC_DBG_CACHE
)
539 printf ("FcFileCacheWriteDir cache_file \"%s\"\n", cache_file
);
541 f
= fopen (cache_file
, "w");
544 if (FcDebug () & FC_DBG_CACHE
)
545 printf (" can't create \"%s\"\n", cache_file
);
548 for (n
= 0; n
< set
->nfont
; n
++)
550 font
= set
->fonts
[n
];
551 if (FcPatternGetString (font
, FC_FILE
, 0, &file
) != FcResultMatch
)
553 base
= strrchr (file
, '/');
558 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
560 if (FcDebug () & FC_DBG_CACHEV
)
561 printf (" write file \"%s\"\n", base
);
562 if (!FcFileCacheWriteString (f
, base
))
564 if (putc (' ', f
) == EOF
)
566 if (!FcFileCacheWriteInt (f
, id
))
568 if (putc (' ', f
) == EOF
)
570 name
= FcNameUnparse (font
);
573 ret
= FcFileCacheWriteString (f
, name
);
577 if (putc ('\n', f
) == EOF
)
580 if (fclose (f
) == EOF
)
583 if (FcDebug () & FC_DBG_CACHE
)
584 printf (" cache written\n");