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.
29 #define HAVE_GETOPT_LONG 1
34 #include <fontconfig/fontconfig.h>
35 #include <../src/fccache.c>
39 #include <sys/types.h>
46 #ifndef HAVE_GETOPT_LONG
47 #define HAVE_GETOPT_LONG 0
54 const struct option longopts[] = {
55 {"version", 0, 0, 'V'},
62 extern int optind, opterr, optopt;
67 * POSIX has broken stdio so that getc must do thread-safe locking,
68 * this is a serious performance problem for applications doing large
69 * amounts of IO with getc (as is done here). If available, use
70 * the getc_unlocked varient instead.
73 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
74 #define GETC(f) getc_unlocked(f)
75 #define PUTC(c,f) putc_unlocked(c,f)
77 #define GETC(f) getc(f)
78 #define PUTC(c,f) putc(c,f)
82 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
85 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
88 while ((c = *chars++))
93 if (PUTC ('\\', f) == EOF)
97 if (PUTC (c, f) == EOF)
105 FcCacheWriteUlong (FILE *f, unsigned long t)
108 unsigned long temp, digit;
121 if (PUTC ((char) digit + '0', f) == EOF)
123 temp = temp - pow * digit;
130 FcCacheWriteInt (FILE *f, int i)
132 return FcCacheWriteUlong (f, (unsigned long) i);
136 FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
139 if (PUTC ('"', f) == EOF)
141 if (!FcCacheWriteChars (f, string))
143 if (PUTC ('"', f) == EOF)
149 usage (char *program)
152 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
155 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
158 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
159 fprintf (stderr, "\n");
161 fprintf (stderr, " -V, --version display font config version and exit\n");
162 fprintf (stderr, " -?, --help display this help and exit\n");
164 fprintf (stderr, " -V (version) display font config version and exit\n");
165 fprintf (stderr, " -? (help) display this help and exit\n");
170 /* read serialized state from the cache file */
172 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
176 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
177 static char name_buf[8192];
185 file = fopen(cache_file, "rb");
189 if (!FcDirCacheConsume (file, set, dirs, NULL, name_buf))
194 printf ("fc-cat: printing directory cache for cache which would be named %s\n",
206 * return the path from the directory containing 'cache' to 'file'
209 static const FcChar8 *
210 FcFileBaseName (const char *cache, const FcChar8 *file)
212 const FcChar8 *cache_slash;
214 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
215 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
216 (cache_slash + 1) - (const FcChar8 *)cache))
217 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
222 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
226 const FcChar8 *file, *base;
232 list = FcStrListCreate (dirs);
236 while ((dir = FcStrListNext (list)))
238 base = FcFileBaseName (base_name, dir);
239 if (!FcCacheWriteStringOld (stdout, base))
241 if (PUTC (' ', stdout) == EOF)
243 if (!FcCacheWriteInt (stdout, 0))
245 if (PUTC (' ', stdout) == EOF)
247 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
249 if (PUTC ('\n', stdout) == EOF)
253 for (n = 0; n < set->nfont; n++)
255 font = set->fonts[n];
256 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
258 base = FcFileBaseName (base_name, file);
259 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
261 if (FcDebug () & FC_DBG_CACHEV)
262 printf (" write file \"%s\"\n", base);
263 if (!FcCacheWriteStringOld (stdout, base))
265 if (PUTC (' ', stdout) == EOF)
267 if (!FcCacheWriteInt (stdout, id))
269 if (PUTC (' ', stdout) == EOF)
271 name = FcNameUnparse (font);
274 ret = FcCacheWriteStringOld (stdout, name);
278 if (PUTC ('\n', stdout) == EOF)
282 FcStrListDone (list);
287 FcStrListDone (list);
293 main (int argc, char **argv)
296 #if HAVE_GETOPT_LONG || HAVE_GETOPT
298 FcFontSet *fs = FcFontSetCreate();
299 FcStrSet *dirs = FcStrSetCreate();
304 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
306 while ((c = getopt (argc, argv, "fsVv?")) != -1)
311 fprintf (stderr, "fontconfig version %d.%d.%d\n",
312 FC_MAJOR, FC_MINOR, FC_REVISION);
323 config = FcInitLoadConfig ();
326 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
329 FcConfigSetCurrent (config);
334 if (FcFileIsDir ((const FcChar8 *)argv[i]))
336 char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i],
337 (FcChar8 *)"/dummy");
338 if (!FcDirScanConfig (fs, dirs, 0,
339 (const FcChar8 *)argv[i], FcFalse, config))
340 fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
343 /* sorry, we can't tell you where the cache file is. */
344 FcCachePrintSet (fs, dirs, dummy_name);
345 FcStrFree ((FcChar8 *)dummy_name);
348 else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0)
349 FcCachePrintSet (fs, dirs, name_buf);
352 printf ("nothing to do\n");
355 FcStrSetDestroy (dirs);
356 FcFontSetDestroy (fs);