2 * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
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 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 FcFileIsDir (const FcChar8 *file)
33 if (stat ((const char *) file, &statb) != 0)
35 return S_ISDIR(statb.st_mode);
39 FcFileScanConfig (FcFontSet *set,
53 FcGlobalCacheFile *cache_file;
54 FcGlobalCacheDir *cache_dir;
57 if (config && !FcConfigAcceptFilename (config, file))
72 if ((cache_file = FcGlobalCacheFileGet (cache, file, id, &count)))
75 * Found a cache entry for the file
77 if (FcGlobalCacheCheckTime (file, &cache_file->info))
79 name = cache_file->name;
81 FcGlobalCacheReferenced (cache, &cache_file->info);
82 /* "." means the file doesn't contain a font */
83 if (FcStrCmp (name, FC_FONT_FILE_INVALID) != 0)
85 font = FcNameParse (name);
87 if (!FcPatternAddString (font, FC_FILE, file))
92 else if ((cache_dir = FcGlobalCacheDirGet (cache, file,
93 strlen ((const char *) file),
96 if (FcGlobalCacheCheckTime (cache_dir->info.file,
101 FcGlobalCacheReferenced (cache, &cache_dir->info);
102 if (!FcStrSetAdd (dirs, file))
108 * Nothing in the cache, scan the file
112 if (FcDebug () & FC_DBG_SCAN)
114 printf ("\tScanning file %s...", file);
117 font = FcFreeTypeQuery (file, id, blanks, &count);
118 if (FcDebug () & FC_DBG_SCAN)
121 if (!font && FcFileIsDir (file))
124 ret = FcStrSetAdd (dirs, file);
133 unparse = FcNameUnparse (font);
136 (void) FcGlobalCacheUpdate (cache, file, id, unparse);
144 if (font && (!config || FcConfigAcceptFont (config, font)))
146 if (!FcFontSetAdd (set, font))
148 FcPatternDestroy (font);
154 FcPatternDestroy (font);
156 } while (font && ret && id < count);
161 FcFileScan (FcFontSet *set,
163 FcGlobalCache *cache,
168 return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
172 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
176 cmpstringp(const void *p1, const void *p2)
178 return strcmp(* (char **) p1, * (char **) p2);
182 * Scan 'dir', adding font files to 'set' and
183 * subdirectories to 'dirs'
187 FcDirScanConfig (FcFontSet *set,
189 FcGlobalCache *cache,
198 int dirlistlen, dirlistalloc;
204 if (config && !FcConfigAcceptFilename (config, dir))
210 * Check fonts.cache-<version> file
212 if (FcDirCacheReadDir (set, dirs, dir, config))
215 FcGlobalCacheReferenceSubdir (cache, dir);
220 * Check ~/.fonts.cache-<version> file
222 if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir, config))
227 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
231 strcpy ((char *) file, (char *) dir);
232 strcat ((char *) file, "/");
233 base = file + strlen ((char *) file);
235 if (FcDebug () & FC_DBG_SCAN)
236 printf ("\tScanning dir %s\n", dir);
238 d = opendir ((char *) dir);
243 /* Don't complain about missing directories */
250 dirlist = malloc(dirlistalloc * sizeof(FcChar8 *));
253 while ((e = readdir (d)))
255 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
257 if (dirlistlen == dirlistalloc)
260 dirlist = realloc(dirlist, dirlistalloc * sizeof(FcChar8 *));
264 dirlist[dirlistlen] = malloc(strlen (e->d_name) + 1);
265 if (!dirlist[dirlistlen])
267 strcpy(dirlist[dirlistlen], e->d_name);
271 qsort(dirlist, dirlistlen, sizeof(FcChar8 *), cmpstringp);
273 while (ret && i < dirlistlen)
275 strcpy ((char *) base, (char *) dirlist[i]);
276 ret = FcFileScanConfig (set, dirs, cache, blanks, file, force, config);
279 for (i = 0; i < dirlistlen; i++)
285 * Now that the directory has been scanned,
286 * add the cache entry
289 FcGlobalCacheUpdate (cache, dir, 0, 0);
295 FcDirScan (FcFontSet *set,
297 FcGlobalCache *cache,
302 return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
306 FcDirSave (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
308 return FcDirCacheWriteDir (set, dirs, dir);