]>
git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
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 "../src/fccache.c"
38 #include <sys/types.h>
45 #ifndef HAVE_GETOPT_LONG
46 #define HAVE_GETOPT_LONG 0
53 const struct option longopts
[] = {
54 {"version", 0, 0, 'V'},
55 {"verbose", 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");
171 FcCacheFileOpen (char *cache_file
, off_t
*size
)
174 struct stat file_stat
;
176 fd
= open(cache_file
, O_RDONLY
| O_BINARY
);
180 if (fstat (fd
, &file_stat
) < 0) {
184 *size
= file_stat
.st_size
;
189 * return the path from the directory containing 'cache' to 'file'
192 static const FcChar8
*
193 FcFileBaseName (const char *cache
, const FcChar8
*file
)
195 const FcChar8
*cache_slash
;
196 int cache_len
= strlen (cache
);
198 if (!strncmp (cache
, file
, cache_len
) && file
[cache_len
] == '/')
199 return file
+ cache_len
+ 1;
204 FcCachePrintSet (FcFontSet
*set
, FcStrSet
*dirs
, char *base_name
)
208 const FcChar8
*file
, *base
;
214 list
= FcStrListCreate (dirs
);
218 while ((dir
= FcStrListNext (list
)))
220 base
= FcFileBaseName (base_name
, dir
);
221 if (!FcCacheWriteStringOld (stdout
, base
))
223 if (PUTC (' ', stdout
) == EOF
)
225 if (!FcCacheWriteInt (stdout
, 0))
227 if (PUTC (' ', stdout
) == EOF
)
229 if (!FcCacheWriteStringOld (stdout
, FC_FONT_FILE_DIR
))
231 if (PUTC ('\n', stdout
) == EOF
)
235 for (n
= 0; n
< set
->nfont
; n
++)
237 FcPattern
**fonts
= FcFontSetFonts (set
);
238 FcPattern
*encoded_font
= fonts
[n
];
239 FcPattern
*font
= FcEncodedOffsetToPtr (set
, encoded_font
, FcPattern
);
241 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
243 base
= FcFileBaseName (base_name
, file
);
244 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
246 if (!FcCacheWriteStringOld (stdout
, base
))
248 if (PUTC (' ', stdout
) == EOF
)
250 if (!FcCacheWriteInt (stdout
, id
))
252 if (PUTC (' ', stdout
) == EOF
)
254 name
= FcNameUnparse (font
);
257 ret
= FcCacheWriteStringOld (stdout
, name
);
261 if (PUTC ('\n', stdout
) == EOF
)
265 FcStrListDone (list
);
270 FcStrListDone (list
);
276 FcCacheFileMap (const FcChar8
*file
)
280 struct stat file_stat
;
282 fd
= open (file
, O_RDONLY
| O_BINARY
);
285 if (fstat (fd
, &file_stat
) < 0) {
289 if (FcCacheLoad (fd
, file_stat
.st_size
, &cache
)) {
298 main (int argc
, char **argv
)
307 #if HAVE_GETOPT_LONG || HAVE_GETOPT
311 while ((c
= getopt_long (argc
, argv
, "Vv?", longopts
, NULL
)) != -1)
313 while ((c
= getopt (argc
, argv
, "Vv?")) != -1)
318 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
319 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
333 config
= FcInitLoadConfig ();
336 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
339 FcConfigSetCurrent (config
);
344 for (; i
< argc
; i
++)
348 intptr_t *cache_dirs
;
350 if (FcFileIsDir ((const FcChar8
*)argv
[i
]))
351 cache
= FcDirCacheMap ((const FcChar8
*) argv
[i
], config
);
353 cache
= FcCacheFileMap (argv
[i
]);
361 dirs
= FcStrSetCreate ();
362 fs
= FcCacheSet (cache
);
363 cache_dirs
= FcCacheDirs (cache
);
364 for (j
= 0; j
< cache
->dirs_count
; j
++)
365 FcStrSetAdd (dirs
, FcOffsetToPtr (cache_dirs
,
370 printf ("Name: %s\nDirectory: %s\n", argv
[i
], FcCacheDir(cache
));
371 FcCachePrintSet (fs
, dirs
, FcCacheDir (cache
));
373 FcStrSetDestroy (dirs
);
375 FcDirCacheUnmap (cache
);