2 * fontconfig/src/fcdir.c
4 * Copyright © 2000 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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 FcFileIsDir (const FcChar8 *file)
33 if (FcStat (file, &statb) != 0)
35 return S_ISDIR(statb.st_mode);
39 FcFileIsLink (const FcChar8 *file)
44 if (lstat ((const char *)file, &statb) != 0)
46 return S_ISLNK (statb.st_mode);
53 FcFileScanFontConfig (FcFontSet *set,
68 * Nothing in the cache, scan the file
70 if (FcDebug () & FC_DBG_SCAN)
72 printf ("\tScanning file %s...", file);
75 font = FcFreeTypeQuery (file, id, blanks, &count);
76 if (FcDebug () & FC_DBG_SCAN)
80 * Edit pattern with user-defined rules
82 if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
84 FcPatternDestroy (font);
94 if (FcDebug() & FC_DBG_SCANV)
96 printf ("Final font pattern:\n");
97 FcPatternPrint (font);
99 if (!FcFontSetAdd (set, font))
101 FcPatternDestroy (font);
107 FcPatternDestroy (font);
109 } while (font && ret && id < count);
114 FcFileScanConfig (FcFontSet *set,
120 if (FcFileIsDir (file))
121 return FcStrSetAdd (dirs, file);
123 return FcFileScanFontConfig (set, blanks, file, config);
127 FcFileScan (FcFontSet *set,
129 FcFileCache *cache FC_UNUSED,
132 FcBool force FC_UNUSED)
134 return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
138 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
141 cmpstringp(const void *p1, const void *p2)
143 return strcmp(* (char **) p1, * (char **) p2);
147 FcDirScanConfig (FcFontSet *set,
151 FcBool force, /* XXX unused */
169 blanks = FcConfigGetBlanks (config);
172 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
178 strcpy ((char *) file, (char *) dir);
179 strcat ((char *) file, "/");
180 base = file + strlen ((char *) file);
182 if (FcDebug () & FC_DBG_SCAN)
183 printf ("\tScanning dir %s\n", dir);
185 d = opendir ((char *) dir);
188 /* Don't complain about missing directories */
194 files = FcStrSetCreate ();
200 while ((e = readdir (d)))
202 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
204 strcpy ((char *) base, (char *) e->d_name);
205 if (!FcStrSetAdd (files, file)) {
213 * Sort files to make things prettier
215 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
218 * Scan file files to build font patterns
220 for (i = 0; i < files->num; i++)
221 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
224 FcStrSetDestroy (files);
235 FcDirScan (FcFontSet *set,
237 FcFileCache *cache, /* XXX unused */
240 FcBool force /* XXX unused */)
245 return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ());
249 * Scan the specified directory and construct a cache of its contents
252 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
256 FcCache *cache = NULL;
257 struct stat dir_stat;
259 if (FcDebug () & FC_DBG_FONTSET)
260 printf ("cache scan dir %s\n", dir);
262 if (FcStatChecksum (dir, &dir_stat) < 0)
265 set = FcFontSetCreate();
269 dirs = FcStrSetCreate ();
276 if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config))
280 * Build the cache object
282 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
287 * Write out the cache file, ignoring any troubles
289 FcDirCacheWrite (cache, config);
292 FcStrSetDestroy (dirs);
294 FcFontSetDestroy (set);
300 * Read (or construct) the cache for a directory
303 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
305 FcCache *cache = NULL;
307 /* Try to use existing cache file */
309 cache = FcDirCacheLoad (dir, config, NULL);
311 /* Not using existing cache file, construct new cache */
313 cache = FcDirCacheScan (dir, config);
319 FcDirSave (FcFontSet *set FC_UNUSED, FcStrSet * dirs FC_UNUSED, const FcChar8 *dir FC_UNUSED)
321 return FcFalse; /* XXX deprecated */
324 #include "fcaliastail.h"