]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2000 Keith Packard |
24330d27 KP |
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 | ||
24330d27 | 25 | #include "fcint.h" |
179c3995 KP |
26 | #include <dirent.h> |
27 | ||
2d9c79c0 | 28 | FcBool |
179c3995 KP |
29 | FcFileIsDir (const FcChar8 *file) |
30 | { | |
31 | struct stat statb; | |
24330d27 | 32 | |
179c3995 KP |
33 | if (stat ((const char *) file, &statb) != 0) |
34 | return FcFalse; | |
35 | return S_ISDIR(statb.st_mode); | |
36 | } | |
24330d27 KP |
37 | |
38 | FcBool | |
d47c9d6e KP |
39 | FcFileScanConfig (FcFontSet *set, |
40 | FcStrSet *dirs, | |
41 | FcGlobalCache *cache, | |
42 | FcBlanks *blanks, | |
43 | const FcChar8 *file, | |
44 | FcBool force, | |
45 | FcConfig *config) | |
24330d27 | 46 | { |
327a7fd4 | 47 | int id; |
327a7fd4 KP |
48 | FcPattern *font; |
49 | FcBool ret = FcTrue; | |
327a7fd4 | 50 | int count = 0; |
24330d27 | 51 | |
d47c9d6e KP |
52 | if (config && !FcConfigAcceptFilename (config, file)) |
53 | return FcTrue; | |
54 | ||
98592bbb PL |
55 | if (FcFileIsDir (file)) |
56 | return FcStrSetAdd (dirs, file); | |
57 | ||
24330d27 KP |
58 | id = 0; |
59 | do | |
60 | { | |
327a7fd4 KP |
61 | font = 0; |
62 | /* | |
eb0cf671 | 63 | * Nothing in the cache, scan the file |
327a7fd4 | 64 | */ |
eb0cf671 | 65 | if (FcDebug () & FC_DBG_SCAN) |
24330d27 | 66 | { |
eb0cf671 PL |
67 | printf ("\tScanning file %s...", file); |
68 | fflush (stdout); | |
24330d27 | 69 | } |
eb0cf671 PL |
70 | font = FcFreeTypeQuery (file, id, blanks, &count); |
71 | if (FcDebug () & FC_DBG_SCAN) | |
72 | printf ("done\n"); | |
327a7fd4 KP |
73 | /* |
74 | * Add the font | |
75 | */ | |
4f27c1c0 | 76 | if (font && (!config || FcConfigAcceptFont (config, font))) |
24330d27 KP |
77 | { |
78 | if (!FcFontSetAdd (set, font)) | |
79 | { | |
80 | FcPatternDestroy (font); | |
81 | font = 0; | |
82 | ret = FcFalse; | |
83 | } | |
84 | } | |
47b49bf1 KP |
85 | else if (font) |
86 | FcPatternDestroy (font); | |
24330d27 KP |
87 | id++; |
88 | } while (font && ret && id < count); | |
89 | return ret; | |
90 | } | |
91 | ||
d47c9d6e KP |
92 | FcBool |
93 | FcFileScan (FcFontSet *set, | |
94 | FcStrSet *dirs, | |
95 | FcGlobalCache *cache, | |
96 | FcBlanks *blanks, | |
97 | const FcChar8 *file, | |
98 | FcBool force) | |
99 | { | |
100 | return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0); | |
101 | } | |
102 | ||
e3c6d336 PL |
103 | /* |
104 | * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage | |
105 | */ | |
106 | ||
107 | static int | |
108 | cmpstringp(const void *p1, const void *p2) | |
109 | { | |
110 | return strcmp(* (char **) p1, * (char **) p2); | |
111 | } | |
112 | ||
c8d5753c KP |
113 | /* |
114 | * Scan 'dir', adding font files to 'set' and | |
115 | * subdirectories to 'dirs' | |
116 | */ | |
117 | ||
24330d27 | 118 | FcBool |
d47c9d6e KP |
119 | FcDirScanConfig (FcFontSet *set, |
120 | FcStrSet *dirs, | |
121 | FcGlobalCache *cache, | |
122 | FcBlanks *blanks, | |
123 | const FcChar8 *dir, | |
124 | FcBool force, | |
125 | FcConfig *config) | |
24330d27 | 126 | { |
327a7fd4 KP |
127 | DIR *d; |
128 | struct dirent *e; | |
e3c6d336 PL |
129 | FcChar8 **dirlist; |
130 | int dirlistlen, dirlistalloc; | |
327a7fd4 KP |
131 | FcChar8 *file; |
132 | FcChar8 *base; | |
133 | FcBool ret = FcTrue; | |
eb0cf671 PL |
134 | FcFontSet *tmpSet; |
135 | int i; | |
24330d27 | 136 | |
d47c9d6e KP |
137 | if (config && !FcConfigAcceptFilename (config, dir)) |
138 | return FcTrue; | |
139 | ||
327a7fd4 KP |
140 | if (!force) |
141 | { | |
327a7fd4 | 142 | /* |
f21f40f3 | 143 | * Check ~/.fonts.cache-<version> file |
327a7fd4 | 144 | */ |
8245771d | 145 | if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config)) |
327a7fd4 | 146 | return FcTrue; |
2eb84374 | 147 | |
7410e40b PL |
148 | if (FcDirCacheValid (dir, config) && |
149 | FcDirCacheHasCurrentArch (dir, config) && | |
530e66b0 | 150 | FcDirCacheRead (set, dirs, dir, config)) |
2eb84374 | 151 | return FcTrue; |
327a7fd4 KP |
152 | } |
153 | ||
9dac3c59 | 154 | /* freed below */ |
179c3995 | 155 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); |
24330d27 KP |
156 | if (!file) |
157 | return FcFalse; | |
158 | ||
ccb3e93b KP |
159 | strcpy ((char *) file, (char *) dir); |
160 | strcat ((char *) file, "/"); | |
161 | base = file + strlen ((char *) file); | |
24330d27 | 162 | |
a8386abc KP |
163 | if (FcDebug () & FC_DBG_SCAN) |
164 | printf ("\tScanning dir %s\n", dir); | |
165 | ||
ccb3e93b | 166 | d = opendir ((char *) dir); |
24330d27 KP |
167 | if (!d) |
168 | { | |
169 | free (file); | |
179c3995 KP |
170 | /* Don't complain about missing directories */ |
171 | if (errno == ENOENT) | |
172 | return FcTrue; | |
24330d27 KP |
173 | return FcFalse; |
174 | } | |
eb0cf671 PL |
175 | |
176 | tmpSet = FcFontSetCreate(); | |
65448e8b PL |
177 | if (!tmpSet) |
178 | { | |
179 | ret = FcFalse; | |
180 | goto bail0; | |
eb0cf671 PL |
181 | } |
182 | ||
e3c6d336 PL |
183 | dirlistlen = 0; |
184 | dirlistalloc = 8; | |
185 | dirlist = malloc(dirlistalloc * sizeof(FcChar8 *)); | |
65448e8b PL |
186 | if (!dirlist) |
187 | { | |
188 | ret = FcFalse; | |
189 | goto bail1; | |
190 | } | |
e3c6d336 | 191 | while ((e = readdir (d))) |
24330d27 | 192 | { |
179c3995 | 193 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) |
24330d27 | 194 | { |
e3c6d336 PL |
195 | if (dirlistlen == dirlistalloc) |
196 | { | |
65448e8b PL |
197 | FcChar8 **tmp_dirlist; |
198 | ||
e3c6d336 | 199 | dirlistalloc *= 2; |
65448e8b PL |
200 | tmp_dirlist = realloc(dirlist, |
201 | dirlistalloc * sizeof(FcChar8 *)); | |
202 | if (!tmp_dirlist) | |
203 | { | |
204 | ret = FcFalse; | |
205 | goto bail2; | |
206 | } | |
207 | dirlist = tmp_dirlist; | |
e3c6d336 PL |
208 | } |
209 | dirlist[dirlistlen] = malloc(strlen (e->d_name) + 1); | |
65448e8b PL |
210 | if (!dirlist[dirlistlen]) |
211 | { | |
212 | ret = FcFalse; | |
213 | goto bail2; | |
214 | } | |
3a342c5a | 215 | strcpy((char *)dirlist[dirlistlen], e->d_name); |
e3c6d336 | 216 | dirlistlen++; |
24330d27 KP |
217 | } |
218 | } | |
e3c6d336 PL |
219 | qsort(dirlist, dirlistlen, sizeof(FcChar8 *), cmpstringp); |
220 | i = 0; | |
221 | while (ret && i < dirlistlen) | |
222 | { | |
223 | strcpy ((char *) base, (char *) dirlist[i]); | |
224 | ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config); | |
225 | i++; | |
226 | } | |
c8d5753c KP |
227 | /* |
228 | * Now that the directory has been scanned, | |
229 | * add the cache entry | |
230 | */ | |
327a7fd4 | 231 | if (ret && cache) |
971cf180 | 232 | FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config); |
eb0cf671 PL |
233 | |
234 | for (i = 0; i < tmpSet->nfont; i++) | |
235 | FcFontSetAdd (set, tmpSet->fonts[i]); | |
236 | ||
237 | if (tmpSet->fonts) | |
238 | { | |
239 | FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *)); | |
240 | free (tmpSet->fonts); | |
241 | } | |
242 | FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet)); | |
65448e8b PL |
243 | |
244 | bail2: | |
245 | for (i = 0; i < dirlistlen; i++) | |
246 | free(dirlist[i]); | |
247 | ||
248 | free (dirlist); | |
249 | ||
250 | bail1: | |
eb0cf671 | 251 | free (tmpSet); |
65448e8b PL |
252 | |
253 | bail0: | |
254 | closedir (d); | |
255 | ||
256 | free (file); | |
24330d27 KP |
257 | return ret; |
258 | } | |
259 | ||
d47c9d6e KP |
260 | FcBool |
261 | FcDirScan (FcFontSet *set, | |
262 | FcStrSet *dirs, | |
263 | FcGlobalCache *cache, | |
264 | FcBlanks *blanks, | |
265 | const FcChar8 *dir, | |
266 | FcBool force) | |
267 | { | |
268 | return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0); | |
269 | } | |
270 | ||
24330d27 | 271 | FcBool |
2304e38f | 272 | FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) |
24330d27 | 273 | { |
7410e40b | 274 | return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ()); |
24330d27 | 275 | } |