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 FcFileIsFile (const FcChar8 *file)
57 if (FcStat (file, &statb) != 0)
59 return S_ISREG (statb.st_mode);
63 FcFileScanFontConfig (FcFontSet *set,
78 * Nothing in the cache, scan the file
80 if (FcDebug () & FC_DBG_SCAN)
82 printf ("\tScanning file %s...", file);
85 font = FcFreeTypeQuery (file, id, blanks, &count);
86 if (FcDebug () & FC_DBG_SCAN)
90 * Edit pattern with user-defined rules
92 if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
94 FcPatternDestroy (font);
104 if (FcDebug() & FC_DBG_SCANV)
106 printf ("Final font pattern:\n");
107 FcPatternPrint (font);
109 if (!FcFontSetAdd (set, font))
111 FcPatternDestroy (font);
117 FcPatternDestroy (font);
119 } while (font && ret && id < count);
124 FcFileScanConfig (FcFontSet *set,
130 if (FcFileIsDir (file))
131 return FcStrSetAdd (dirs, file);
133 return FcFileScanFontConfig (set, blanks, file, config);
137 FcFileScan (FcFontSet *set,
139 FcFileCache *cache FC_UNUSED,
142 FcBool force FC_UNUSED)
144 return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
148 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
151 cmpstringp(const void *p1, const void *p2)
153 return strcmp(* (char **) p1, * (char **) p2);
157 FcDirScanConfig (FcFontSet *set,
161 FcBool force, /* XXX unused */
179 blanks = FcConfigGetBlanks (config);
182 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
188 strcpy ((char *) file, (char *) dir);
189 strcat ((char *) file, "/");
190 base = file + strlen ((char *) file);
192 if (FcDebug () & FC_DBG_SCAN)
193 printf ("\tScanning dir %s\n", dir);
195 d = opendir ((char *) dir);
198 /* Don't complain about missing directories */
204 files = FcStrSetCreate ();
210 while ((e = readdir (d)))
212 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
214 strcpy ((char *) base, (char *) e->d_name);
215 if (!FcStrSetAdd (files, file)) {
223 * Sort files to make things prettier
225 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
228 * Scan file files to build font patterns
230 for (i = 0; i < files->num; i++)
231 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
234 FcStrSetDestroy (files);
245 FcDirScan (FcFontSet *set,
247 FcFileCache *cache, /* XXX unused */
250 FcBool force /* XXX unused */)
255 return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ());
259 * Scan the specified directory and construct a cache of its contents
262 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
266 FcCache *cache = NULL;
267 struct stat dir_stat;
269 if (FcDebug () & FC_DBG_FONTSET)
270 printf ("cache scan dir %s\n", dir);
272 if (FcStatChecksum (dir, &dir_stat) < 0)
275 set = FcFontSetCreate();
279 dirs = FcStrSetCreate ();
286 if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config))
290 * Build the cache object
292 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
297 * Write out the cache file, ignoring any troubles
299 FcDirCacheWrite (cache, config);
302 FcStrSetDestroy (dirs);
304 FcFontSetDestroy (set);
310 * Read (or construct) the cache for a directory
313 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
315 FcCache *cache = NULL;
317 /* Try to use existing cache file */
319 cache = FcDirCacheLoad (dir, config, NULL);
321 /* Not using existing cache file, construct new cache */
323 cache = FcDirCacheScan (dir, config);
329 FcDirSave (FcFontSet *set FC_UNUSED, FcStrSet * dirs FC_UNUSED, const FcChar8 *dir FC_UNUSED)
331 return FcFalse; /* XXX deprecated */
334 #include "fcaliastail.h"