]> git.wh0rd.org - fontconfig.git/blob - src/fcdir.c
Hoist FcFileIsDir check out of FcFileScanConfig loop.
[fontconfig.git] / src / fcdir.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
3 *
4 * Copyright © 2000 Keith Packard
5 *
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.
15 *
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.
23 */
24
25 #include "fcint.h"
26 #include <dirent.h>
27
28 FcBool
29 FcFileIsDir (const FcChar8 *file)
30 {
31 struct stat statb;
32
33 if (stat ((const char *) file, &statb) != 0)
34 return FcFalse;
35 return S_ISDIR(statb.st_mode);
36 }
37
38 FcBool
39 FcFileScanConfig (FcFontSet *set,
40 FcStrSet *dirs,
41 FcGlobalCache *cache,
42 FcBlanks *blanks,
43 const FcChar8 *file,
44 FcBool force,
45 FcConfig *config)
46 {
47 int id;
48 FcPattern *font;
49 FcBool ret = FcTrue;
50 int count = 0;
51
52 if (config && !FcConfigAcceptFilename (config, file))
53 return FcTrue;
54
55 if (FcFileIsDir (file))
56 return FcStrSetAdd (dirs, file);
57
58 if (force)
59 cache = 0;
60
61 id = 0;
62 do
63 {
64 font = 0;
65 /*
66 * Nothing in the cache, scan the file
67 */
68 if (FcDebug () & FC_DBG_SCAN)
69 {
70 printf ("\tScanning file %s...", file);
71 fflush (stdout);
72 }
73 font = FcFreeTypeQuery (file, id, blanks, &count);
74 if (FcDebug () & FC_DBG_SCAN)
75 printf ("done\n");
76 /*
77 * Add the font
78 */
79 if (font && (!config || FcConfigAcceptFont (config, font)))
80 {
81 if (!FcFontSetAdd (set, font))
82 {
83 FcPatternDestroy (font);
84 font = 0;
85 ret = FcFalse;
86 }
87 }
88 else if (font)
89 FcPatternDestroy (font);
90 id++;
91 } while (font && ret && id < count);
92 return ret;
93 }
94
95 FcBool
96 FcFileScan (FcFontSet *set,
97 FcStrSet *dirs,
98 FcGlobalCache *cache,
99 FcBlanks *blanks,
100 const FcChar8 *file,
101 FcBool force)
102 {
103 return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
104 }
105
106 /*
107 * Scan 'dir', adding font files to 'set' and
108 * subdirectories to 'dirs'
109 */
110
111 FcBool
112 FcDirScanConfig (FcFontSet *set,
113 FcStrSet *dirs,
114 FcGlobalCache *cache,
115 FcBlanks *blanks,
116 const FcChar8 *dir,
117 FcBool force,
118 FcConfig *config)
119 {
120 DIR *d;
121 struct dirent *e;
122 FcChar8 *file;
123 FcChar8 *base;
124 FcBool ret = FcTrue;
125 FcFontSet *tmpSet;
126 int i;
127
128 if (config && !FcConfigAcceptFilename (config, dir))
129 return FcTrue;
130
131 if (config)
132 dir = FcConfigNormalizeFontDir (config, dir);
133
134 /* refuse to scan a directory that can't be normalized. */
135 if (!dir)
136 return FcFalse;
137
138 if (!force)
139 {
140 /*
141 * Check ~/.fonts.cache-<version> file
142 */
143 if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
144 return FcTrue;
145
146 if (FcDirCacheValid (dir) && FcDirCacheRead (set, dirs, dir, config))
147 return FcTrue;
148 }
149
150 /* freed below */
151 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
152 if (!file)
153 return FcFalse;
154
155 strcpy ((char *) file, (char *) dir);
156 strcat ((char *) file, "/");
157 base = file + strlen ((char *) file);
158
159 if (FcDebug () & FC_DBG_SCAN)
160 printf ("\tScanning dir %s\n", dir);
161
162 d = opendir ((char *) dir);
163 if (!d)
164 {
165 free (file);
166 /* Don't complain about missing directories */
167 if (errno == ENOENT)
168 return FcTrue;
169 return FcFalse;
170 }
171
172 tmpSet = FcFontSetCreate();
173 if (!tmpSet)
174 {
175 free (file);
176 return FcFalse;
177 }
178
179 while (ret && (e = readdir (d)))
180 {
181 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
182 {
183 strcpy ((char *) base, (char *) e->d_name);
184 ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
185 }
186 }
187 free (file);
188 closedir (d);
189 /*
190 * Now that the directory has been scanned,
191 * add the cache entry
192 */
193 if (ret && cache)
194 FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config);
195
196 for (i = 0; i < tmpSet->nfont; i++)
197 FcFontSetAdd (set, tmpSet->fonts[i]);
198
199 if (tmpSet->fonts)
200 {
201 FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
202 free (tmpSet->fonts);
203 }
204 FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
205 free (tmpSet);
206
207 return ret;
208 }
209
210 FcBool
211 FcDirScan (FcFontSet *set,
212 FcStrSet *dirs,
213 FcGlobalCache *cache,
214 FcBlanks *blanks,
215 const FcChar8 *dir,
216 FcBool force)
217 {
218 return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
219 }
220
221 FcBool
222 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
223 {
224 return FcDirCacheWrite (set, dirs, dir);
225 }