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 FcFileScanFontConfig (FcFontSet *set,
54 * Nothing in the cache, scan the file
56 if (FcDebug () & FC_DBG_SCAN)
58 printf ("\tScanning file %s...", file);
61 font = FcFreeTypeQuery (file, id, blanks, &count);
62 if (FcDebug () & FC_DBG_SCAN)
67 if (font && (!config || FcConfigAcceptFont (config, font)))
69 if (!FcFontSetAdd (set, font))
71 FcPatternDestroy (font);
77 FcPatternDestroy (font);
79 } while (font && ret && id < count);
84 FcFileScanConfig (FcFontSet *set,
91 if (config && !FcConfigAcceptFilename (config, file))
94 if (FcFileIsDir (file))
95 return FcStrSetAdd (dirs, file);
97 return FcFileScanFontConfig (set, blanks, file, config);
101 FcFileScan (FcFontSet *set,
103 FcFileCache *cache, /* XXX unused */
108 return FcFileScanConfig (set, dirs, blanks, file, force, 0);
112 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
116 cmpstringp(const void *p1, const void *p2)
118 return strcmp(* (char **) p1, * (char **) p2);
122 * Scan 'dir', adding font files to 'set' and
123 * subdirectories to 'dirs'
127 FcDirScanConfig (FcFontSet *set,
136 FcStrSet *dirlist, *filelist;
143 if (config && !FcConfigAcceptFilename (config, dir))
148 if (FcDirCacheRead (set, dirs, dir, config))
152 if (FcDebug () & FC_DBG_FONTSET)
153 printf ("cache scan dir %s\n", dir);
156 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
160 strcpy ((char *) file, (char *) dir);
161 strcat ((char *) file, "/");
162 base = file + strlen ((char *) file);
164 if (FcDebug () & FC_DBG_SCAN)
165 printf ("\tScanning dir %s\n", dir);
167 d = opendir ((char *) dir);
171 /* Don't complain about missing directories */
177 tmpSet = FcFontSetCreate();
184 dirlist = FcStrSetCreate ();
190 filelist = FcStrSetCreate ();
196 while ((e = readdir (d)))
198 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
200 strcpy ((char *) base, (char *) e->d_name);
201 if (FcFileIsDir (file)) {
202 if (!FcStrSetAdd (dirlist, file)) {
207 if (!FcStrSetAdd (filelist, file)) {
215 * Sort files and dirs to make things prettier
217 qsort(dirlist->strs, dirlist->num, sizeof(FcChar8 *), cmpstringp);
218 qsort(filelist->strs, filelist->num, sizeof(FcChar8 *), cmpstringp);
220 for (i = 0; i < filelist->num; i++)
221 FcFileScanFontConfig (tmpSet, blanks, filelist->strs[i], config);
224 * Now that the directory has been scanned,
225 * write out the cache file
227 FcDirCacheWrite (tmpSet, dirlist, dir, config);
230 * Add the discovered fonts to our internal non-cache list
232 for (i = 0; i < tmpSet->nfont; i++)
233 FcFontSetAdd (set, tmpSet->fonts[i]);
236 * the patterns in tmpset now belong to set; don't free them
241 * Add the discovered directories to the list to be scanned
243 for (i = 0; i < dirlist->num; i++)
244 if (!FcStrSetAdd (dirs, dirlist->strs[i])) {
250 FcStrSetDestroy (filelist);
252 FcStrSetDestroy (dirlist);
254 FcFontSetDestroy (tmpSet);
264 FcDirScan (FcFontSet *set,
266 FcFileCache *cache, /* XXX unused */
271 return FcDirScanConfig (set, dirs, blanks, dir, force, 0);
275 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
277 return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ());