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'},
56 {"verbose", 0, 0, 'v'},
63 extern int optind, opterr, optopt;
68 * POSIX has broken stdio so that getc must do thread-safe locking,
69 * this is a serious performance problem for applications doing large
70 * amounts of IO with getc (as is done here). If available, use
71 * the getc_unlocked varient instead.
74 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
75 #define GETC(f) getc_unlocked(f)
76 #define PUTC(c,f) putc_unlocked(c,f)
78 #define GETC(f) getc(f)
79 #define PUTC(c,f) putc(c,f)
83 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
86 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
89 while ((c = *chars++))
94 if (PUTC ('\\', f) == EOF)
98 if (PUTC (c, f) == EOF)
106 FcCacheWriteUlong (FILE *f, unsigned long t)
109 unsigned long temp, digit;
122 if (PUTC ((char) digit + '0', f) == EOF)
124 temp = temp - pow * digit;
131 FcCacheWriteInt (FILE *f, int i)
133 return FcCacheWriteUlong (f, (unsigned long) i);
137 FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
140 if (PUTC ('"', f) == EOF)
142 if (!FcCacheWriteChars (f, string))
144 if (PUTC ('"', f) == EOF)
150 usage (char *program)
153 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
156 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
159 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
160 fprintf (stderr, "\n");
162 fprintf (stderr, " -V, --version display font config version and exit\n");
163 fprintf (stderr, " -?, --help display this help and exit\n");
165 fprintf (stderr, " -V (version) display font config version and exit\n");
166 fprintf (stderr, " -? (help) display this help and exit\n");
172 FcCacheFileOpen (char *cache_file, off_t *size)
175 struct stat file_stat;
177 fd = open(cache_file, O_RDONLY | O_BINARY);
181 if (fstat (fd, &file_stat) < 0) {
185 *size = file_stat.st_size;
190 * return the path from the directory containing 'cache' to 'file'
193 static const FcChar8 *
194 FcFileBaseName (const char *cache, const FcChar8 *file)
196 const FcChar8 *cache_slash;
197 int cache_len = strlen (cache);
199 if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
200 return file + cache_len + 1;
205 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
209 const FcChar8 *file, *base;
215 list = FcStrListCreate (dirs);
219 while ((dir = FcStrListNext (list)))
221 base = FcFileBaseName (base_name, dir);
222 if (!FcCacheWriteStringOld (stdout, base))
224 if (PUTC (' ', stdout) == EOF)
226 if (!FcCacheWriteInt (stdout, 0))
228 if (PUTC (' ', stdout) == EOF)
230 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
232 if (PUTC ('\n', stdout) == EOF)
236 for (n = 0; n < set->nfont; n++)
238 FcPattern **fonts = FcFontSetFonts (set);
239 FcPattern *encoded_font = fonts[n];
240 FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
242 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
244 base = FcFileBaseName (base_name, file);
245 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
247 if (!FcCacheWriteStringOld (stdout, base))
249 if (PUTC (' ', stdout) == EOF)
251 if (!FcCacheWriteInt (stdout, id))
253 if (PUTC (' ', stdout) == EOF)
255 name = FcNameUnparse (font);
258 ret = FcCacheWriteStringOld (stdout, name);
262 if (PUTC ('\n', stdout) == EOF)
266 FcStrListDone (list);
271 FcStrListDone (list);
277 FcCacheFileMap (const FcChar8 *file)
281 struct stat file_stat;
283 fd = open (file, O_RDONLY | O_BINARY);
286 if (fstat (fd, &file_stat) < 0) {
290 if (FcCacheLoad (fd, file_stat.st_size, &cache)) {
299 main (int argc, char **argv)
308 #if HAVE_GETOPT_LONG || HAVE_GETOPT
312 while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1)
314 while ((c = getopt (argc, argv, "Vv?")) != -1)
319 fprintf (stderr, "fontconfig version %d.%d.%d\n",
320 FC_MAJOR, FC_MINOR, FC_REVISION);
334 config = FcInitLoadConfig ();
337 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
340 FcConfigSetCurrent (config);
345 for (; i < argc; i++)
349 intptr_t *cache_dirs;
351 if (FcFileIsDir ((const FcChar8 *)argv[i]))
352 cache = FcDirCacheMap ((const FcChar8 *) argv[i], config);
354 cache = FcCacheFileMap (argv[i]);
362 dirs = FcStrSetCreate ();
363 fs = FcCacheSet (cache);
364 cache_dirs = FcCacheDirs (cache);
365 for (j = 0; j < cache->dirs_count; j++)
366 FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
371 printf ("Name: %s\nDirectory: %s\n", argv[i], FcCacheDir(cache));
372 FcCachePrintSet (fs, dirs, FcCacheDir (cache));
374 FcStrSetDestroy (dirs);
376 FcDirCacheUnmap (cache);