]> git.wh0rd.org - fontconfig.git/blob - src/fcdir.c
Eliminate redundancies.
[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 id = 0;
59 do
60 {
61 font = 0;
62 /*
63 * Nothing in the cache, scan the file
64 */
65 if (FcDebug () & FC_DBG_SCAN)
66 {
67 printf ("\tScanning file %s...", file);
68 fflush (stdout);
69 }
70 font = FcFreeTypeQuery (file, id, blanks, &count);
71 if (FcDebug () & FC_DBG_SCAN)
72 printf ("done\n");
73 /*
74 * Add the font
75 */
76 if (font && (!config || FcConfigAcceptFont (config, font)))
77 {
78 if (!FcFontSetAdd (set, font))
79 {
80 FcPatternDestroy (font);
81 font = 0;
82 ret = FcFalse;
83 }
84 }
85 else if (font)
86 FcPatternDestroy (font);
87 id++;
88 } while (font && ret && id < count);
89 return ret;
90 }
91
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
103 /*
104 * Scan 'dir', adding font files to 'set' and
105 * subdirectories to 'dirs'
106 */
107
108 FcBool
109 FcDirScanConfig (FcFontSet *set,
110 FcStrSet *dirs,
111 FcGlobalCache *cache,
112 FcBlanks *blanks,
113 const FcChar8 *dir,
114 FcBool force,
115 FcConfig *config)
116 {
117 DIR *d;
118 struct dirent *e;
119 FcChar8 *file;
120 const FcChar8 *d_can = 0;
121 FcChar8 *base;
122 FcBool ret = FcTrue;
123 FcFontSet *tmpSet;
124 int i;
125
126 if (config && !FcConfigAcceptFilename (config, dir))
127 return FcTrue;
128
129 if (config)
130 d_can = FcConfigNormalizeFontDir (config, dir);
131 if (d_can)
132 dir = d_can;
133
134 if (!force)
135 {
136 /*
137 * Check ~/.fonts.cache-<version> file
138 */
139 if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
140 return FcTrue;
141
142 if (FcDirCacheValid (dir) &&
143 FcDirCacheHasCurrentArch (dir) &&
144 FcDirCacheRead (set, dirs, dir, config))
145 return FcTrue;
146 }
147
148 /* freed below */
149 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
150 if (!file)
151 return FcFalse;
152
153 strcpy ((char *) file, (char *) dir);
154 strcat ((char *) file, "/");
155 base = file + strlen ((char *) file);
156
157 if (FcDebug () & FC_DBG_SCAN)
158 printf ("\tScanning dir %s\n", dir);
159
160 d = opendir ((char *) dir);
161 if (!d)
162 {
163 free (file);
164 /* Don't complain about missing directories */
165 if (errno == ENOENT)
166 return FcTrue;
167 return FcFalse;
168 }
169
170 tmpSet = FcFontSetCreate();
171 if (!tmpSet)
172 {
173 free (file);
174 return FcFalse;
175 }
176
177 while (ret && (e = readdir (d)))
178 {
179 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
180 {
181 strcpy ((char *) base, (char *) e->d_name);
182 ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
183 }
184 }
185 free (file);
186 closedir (d);
187 /*
188 * Now that the directory has been scanned,
189 * add the cache entry
190 */
191 if (ret && cache)
192 FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config);
193
194 for (i = 0; i < tmpSet->nfont; i++)
195 FcFontSetAdd (set, tmpSet->fonts[i]);
196
197 if (tmpSet->fonts)
198 {
199 FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
200 free (tmpSet->fonts);
201 }
202 FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
203 free (tmpSet);
204
205 return ret;
206 }
207
208 FcBool
209 FcDirScan (FcFontSet *set,
210 FcStrSet *dirs,
211 FcGlobalCache *cache,
212 FcBlanks *blanks,
213 const FcChar8 *dir,
214 FcBool force)
215 {
216 return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
217 }
218
219 FcBool
220 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
221 {
222 return FcDirCacheWrite (set, dirs, dir);
223 }