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,
137 FcStrSet *dirlist, *filelist;
144 canon_dir = FcStrCanonFilename (dir);
145 if (!canon_dir) canon_dir = (FcChar8 *) dir;
147 if (config && !FcConfigAcceptFilename (config, canon_dir)) {
154 if (FcDirCacheRead (set, dirs, canon_dir, config)) {
160 if (FcDebug () & FC_DBG_FONTSET)
161 printf ("cache scan dir %s\n", canon_dir);
164 file = (FcChar8 *) malloc (strlen ((char *) canon_dir) + 1 + FC_MAX_FILE_LEN + 1);
170 strcpy ((char *) file, (char *) canon_dir);
171 strcat ((char *) file, "/");
172 base = file + strlen ((char *) file);
174 if (FcDebug () & FC_DBG_SCAN)
175 printf ("\tScanning dir %s\n", canon_dir);
177 d = opendir ((char *) canon_dir);
180 /* Don't complain about missing directories */
188 tmpSet = FcFontSetCreate();
195 dirlist = FcStrSetCreate ();
201 filelist = FcStrSetCreate ();
207 while ((e = readdir (d)))
209 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
211 strcpy ((char *) base, (char *) e->d_name);
212 if (FcFileIsDir (file)) {
213 if (!FcStrSetAdd (dirlist, file)) {
218 if (!FcStrSetAdd (filelist, file)) {
226 * Sort files and dirs to make things prettier
228 qsort(dirlist->strs, dirlist->num, sizeof(FcChar8 *), cmpstringp);
229 qsort(filelist->strs, filelist->num, sizeof(FcChar8 *), cmpstringp);
231 for (i = 0; i < filelist->num; i++)
232 FcFileScanFontConfig (tmpSet, blanks, filelist->strs[i], config);
235 * Now that the directory has been scanned,
236 * write out the cache file
238 FcDirCacheWrite (tmpSet, dirlist, canon_dir, config);
241 * Add the discovered fonts to our internal non-cache list
243 for (i = 0; i < tmpSet->nfont; i++)
244 FcFontSetAdd (set, tmpSet->fonts[i]);
247 * the patterns in tmpset now belong to set; don't free them
252 * Add the discovered directories to the list to be scanned
254 for (i = 0; i < dirlist->num; i++)
255 if (!FcStrSetAdd (dirs, dirlist->strs[i])) {
261 FcStrSetDestroy (filelist);
263 FcStrSetDestroy (dirlist);
265 FcFontSetDestroy (tmpSet);
273 if (canon_dir != dir) free (canon_dir);
278 FcDirScan (FcFontSet *set,
280 FcFileCache *cache, /* XXX unused */
285 return FcDirScanConfig (set, dirs, blanks, dir, force, 0);
289 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)