X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffcdir.c;h=62314a325984452c437d9987e6aad0b2d2c3a4ff;hb=00f059e930f12ca7c66cf2ffbc6c4ae789912af7;hp=18cb6cc90e5f12923f085973fea3be82b78f5120;hpb=530e66b008c0d5b972b54046a5b15e76c8e989b6;p=fontconfig.git diff --git a/src/fcdir.c b/src/fcdir.c index 18cb6cc..62314a3 100644 --- a/src/fcdir.c +++ b/src/fcdir.c @@ -35,29 +35,17 @@ FcFileIsDir (const FcChar8 *file) return S_ISDIR(statb.st_mode); } -FcBool -FcFileScanConfig (FcFontSet *set, - FcStrSet *dirs, - FcGlobalCache *cache, - FcBlanks *blanks, - const FcChar8 *file, - FcBool force, - FcConfig *config) +static FcBool +FcFileScanFontConfig (FcFontSet *set, + FcBlanks *blanks, + const FcChar8 *file, + FcConfig *config) { - int id; - FcPattern *font; - FcBool ret = FcTrue; - int count = 0; + FcPattern *font; + FcBool ret = FcTrue; + int id; + int count = 0; - if (config && !FcConfigAcceptFilename (config, file)) - return FcTrue; - - if (FcFileIsDir (file)) - return FcStrSetAdd (dirs, file); - - if (force) - cache = 0; - id = 0; do { @@ -92,15 +80,42 @@ FcFileScanConfig (FcFontSet *set, return ret; } +FcBool +FcFileScanConfig (FcFontSet *set, + FcStrSet *dirs, + FcBlanks *blanks, + const FcChar8 *file, + FcBool force, + FcConfig *config) +{ + if (config && !FcConfigAcceptFilename (config, file)) + return FcTrue; + + if (FcFileIsDir (file)) + return FcStrSetAdd (dirs, file); + else + return FcFileScanFontConfig (set, blanks, file, config); +} + FcBool FcFileScan (FcFontSet *set, FcStrSet *dirs, - FcGlobalCache *cache, + FcFileCache *cache, /* XXX unused */ FcBlanks *blanks, const FcChar8 *file, FcBool force) { - return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0); + return FcFileScanConfig (set, dirs, blanks, file, force, 0); +} + +/* + * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage + */ + +static int +cmpstringp(const void *p1, const void *p2) +{ + return strcmp(* (char **) p1, * (char **) p2); } /* @@ -111,7 +126,6 @@ FcFileScan (FcFontSet *set, FcBool FcDirScanConfig (FcFontSet *set, FcStrSet *dirs, - FcGlobalCache *cache, FcBlanks *blanks, const FcChar8 *dir, FcBool force, @@ -119,8 +133,8 @@ FcDirScanConfig (FcFontSet *set, { DIR *d; struct dirent *e; + FcStrSet *dirlist, *filelist; FcChar8 *file; - const FcChar8 *d_can = 0; FcChar8 *base; FcBool ret = FcTrue; FcFontSet *tmpSet; @@ -129,25 +143,15 @@ FcDirScanConfig (FcFontSet *set, if (config && !FcConfigAcceptFilename (config, dir)) return FcTrue; - if (config) - d_can = FcConfigNormalizeFontDir (config, dir); - if (d_can) - dir = d_can; - if (!force) { - /* - * Check ~/.fonts.cache- file - */ - if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config)) - return FcTrue; - - if (FcDirCacheValid (dir) && - FcDirCacheHasCurrentArch (dir) && - FcDirCacheRead (set, dirs, dir, config)) + if (FcDirCacheRead (set, dirs, dir, config)) return FcTrue; } + if (FcDebug () & FC_DBG_FONTSET) + printf ("cache scan dir %s\n", dir); + /* freed below */ file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); if (!file) @@ -171,56 +175,107 @@ FcDirScanConfig (FcFontSet *set, } tmpSet = FcFontSetCreate(); - if (!tmpSet) - { - free (file); - return FcFalse; + if (!tmpSet) + { + ret = FcFalse; + goto bail0; } - while (ret && (e = readdir (d))) + dirlist = FcStrSetCreate (); + if (!dirlist) + { + ret = FcFalse; + goto bail1; + } + filelist = FcStrSetCreate (); + if (!filelist) + { + ret = FcFalse; + goto bail2; + } + while ((e = readdir (d))) { if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) { strcpy ((char *) base, (char *) e->d_name); - ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config); + if (FcFileIsDir (file)) { + if (!FcStrSetAdd (dirlist, file)) { + ret = FcFalse; + goto bail3; + } + } else { + if (!FcStrSetAdd (filelist, file)) { + ret = FcFalse; + goto bail3; + } + } } } - free (file); - closedir (d); /* - * Now that the directory has been scanned, - * add the cache entry + * Sort files and dirs to make things prettier */ - if (ret && cache) - FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config); + qsort(dirlist->strs, dirlist->num, sizeof(FcChar8 *), cmpstringp); + qsort(filelist->strs, filelist->num, sizeof(FcChar8 *), cmpstringp); + + for (i = 0; i < filelist->num; i++) + FcFileScanFontConfig (tmpSet, blanks, filelist->strs[i], config); + + if (FcShouldWriteFiles ()) + { + /* + * Now that the directory has been scanned, + * write out the cache file + */ + FcDirCacheWrite (tmpSet, dirlist, dir, config); + } + /* + * Add the discovered fonts to our internal non-cache list + */ for (i = 0; i < tmpSet->nfont; i++) FcFontSetAdd (set, tmpSet->fonts[i]); - if (tmpSet->fonts) - { - FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *)); - free (tmpSet->fonts); - } - FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet)); - free (tmpSet); - + /* + * the patterns in tmpset now belong to set; don't free them + */ + tmpSet->nfont = 0; + + /* + * Add the discovered directories to the list to be scanned + */ + for (i = 0; i < dirlist->num; i++) + if (!FcStrSetAdd (dirs, dirlist->strs[i])) { + ret = FcFalse; + goto bail3; + } + + bail3: + FcStrSetDestroy (filelist); + bail2: + FcStrSetDestroy (dirlist); + bail1: + FcFontSetDestroy (tmpSet); + + bail0: + closedir (d); + + free (file); return ret; } FcBool FcDirScan (FcFontSet *set, FcStrSet *dirs, - FcGlobalCache *cache, + FcFileCache *cache, /* XXX unused */ FcBlanks *blanks, const FcChar8 *dir, FcBool force) { - return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0); + return FcDirScanConfig (set, dirs, blanks, dir, force, 0); } FcBool FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) { - return FcDirCacheWrite (set, dirs, dir); + return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ()); }