2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
4 * Copyright © 2002 Keith Packard
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.
25 #include <fontconfig/fontconfig.h>
26 #include <../src/fccache.c>
30 #include <sys/types.h>
37 #define HAVE_GETOPT_LONG 1
45 #ifndef HAVE_GETOPT_LONG
46 #define HAVE_GETOPT_LONG 0
53 const struct option longopts[] = {
54 {"version", 0, 0, 'V'},
61 extern int optind, opterr, optopt;
66 * POSIX has broken stdio so that getc must do thread-safe locking,
67 * this is a serious performance problem for applications doing large
68 * amounts of IO with getc (as is done here). If available, use
69 * the getc_unlocked varient instead.
72 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
73 #define GETC(f) getc_unlocked(f)
74 #define PUTC(c,f) putc_unlocked(c,f)
76 #define GETC(f) getc(f)
77 #define PUTC(c,f) putc(c,f)
81 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
84 while ((c = *chars++))
89 if (PUTC ('\\', f) == EOF)
93 if (PUTC (c, f) == EOF)
101 FcCacheWriteUlong (FILE *f, unsigned long t)
104 unsigned long temp, digit;
117 if (PUTC ((char) digit + '0', f) == EOF)
119 temp = temp - pow * digit;
126 FcCacheWriteInt (FILE *f, int i)
128 return FcCacheWriteUlong (f, (unsigned long) i);
132 FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
135 if (PUTC ('"', f) == EOF)
137 if (!FcCacheWriteChars (f, string))
139 if (PUTC ('"', f) == EOF)
145 usage (char *program)
148 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
151 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
154 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
155 fprintf (stderr, "\n");
157 fprintf (stderr, " -V, --version display font config version and exit\n");
158 fprintf (stderr, " -?, --help display this help and exit\n");
160 fprintf (stderr, " -V (version) display font config version and exit\n");
161 fprintf (stderr, " -? (help) display this help and exit\n");
166 /* read serialized state from the cache file */
168 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
171 char * current_arch_machine_name;
172 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
173 off_t current_arch_start = 0;
174 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
179 current_arch_machine_name = FcCacheMachineSignature();
180 fd = open(cache_file, O_RDONLY);
184 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
185 if (current_arch_start < 0)
188 lseek (fd, current_arch_start, SEEK_SET);
189 if (FcCacheReadString (fd, candidate_arch_machine_name,
190 sizeof (candidate_arch_machine_name)) == 0)
193 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
194 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
196 if (!FcDirCacheConsume (fd, set))
211 * return the path from the directory containing 'cache' to 'file'
214 static const FcChar8 *
215 FcFileBaseName (const char *cache, const FcChar8 *file)
217 const FcChar8 *cache_slash;
219 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
220 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
221 (cache_slash + 1) - (const FcChar8 *)cache))
222 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
227 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
231 const FcChar8 *file, *base;
237 list = FcStrListCreate (dirs);
241 while ((dir = FcStrListNext (list)))
243 base = FcFileBaseName (cache_file, dir);
244 if (!FcCacheWriteStringOld (stdout, base))
246 if (PUTC (' ', stdout) == EOF)
248 if (!FcCacheWriteInt (stdout, 0))
250 if (PUTC (' ', stdout) == EOF)
252 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
254 if (PUTC ('\n', stdout) == EOF)
258 for (n = 0; n < set->nfont; n++)
260 font = set->fonts[n];
261 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
263 base = FcFileBaseName (cache_file, file);
264 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
266 if (FcDebug () & FC_DBG_CACHEV)
267 printf (" write file \"%s\"\n", base);
268 if (!FcCacheWriteStringOld (stdout, base))
270 if (PUTC (' ', stdout) == EOF)
272 if (!FcCacheWriteInt (stdout, id))
274 if (PUTC (' ', stdout) == EOF)
276 name = FcNameUnparse (font);
279 ret = FcCacheWriteStringOld (stdout, name);
283 if (PUTC ('\n', stdout) == EOF)
287 FcStrListDone (list);
292 FcStrListDone (list);
300 main (int argc, char **argv)
304 #if HAVE_GETOPT_LONG || HAVE_GETOPT
306 FcFontSet *fs = FcFontSetCreate();
307 FcStrSet *dirs = FcStrSetCreate();
310 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
312 while ((c = getopt (argc, argv, "fsVv?")) != -1)
317 fprintf (stderr, "fontconfig version %d.%d.%d\n",
318 FC_MAJOR, FC_MINOR, FC_REVISION);
329 if (FcCacheFileRead (fs, dirs, argv[i]))
330 FcCachePrintSet (fs, dirs, argv[i]);
332 FcStrSetDestroy (dirs);
333 FcFontSetDestroy (fs);