]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcdir.c
Regenerate x86 line in fcarch.tmpl.h to match change in cache data.
[fontconfig.git] / src / fcdir.c
index 9ce22daf660884af223ed91df92bac740ea687c0..013686789a60f8817266a876848ef56db9d9df61 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
  *
- * Copyright © 2000 Keith Packard
+ * Copyright Â© 2000 Keith Packard
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -25,7 +25,7 @@
 #include "fcint.h"
 #include <dirent.h>
 
-static FcBool
+FcBool
 FcFileIsDir (const FcChar8 *file)
 {
     struct stat            statb;
@@ -35,109 +35,32 @@ 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;
-    FcChar8            *name;
-    FcPattern          *font;
-    FcBool             ret = FcTrue;
-    FcBool             isDir;
-    int                        count = 0;
-    FcGlobalCacheFile  *cache_file;
-    FcGlobalCacheDir   *cache_dir;
-    FcBool             need_scan;
+    FcPattern  *font;
+    FcBool     ret = FcTrue;
+    int                id;
+    int                count = 0;
     
-    if (config && !FcConfigAcceptFilename (config, file))
-       return FcTrue;
-
-    if (force)
-       cache = 0;
     id = 0;
     do
     {
-       need_scan = FcTrue;
        font = 0;
-       /*
-        * Check the cache
-        */
-       if (cache)
-       {
-           if ((cache_file = FcGlobalCacheFileGet (cache, file, id, &count)))
-           {
-               /*
-                * Found a cache entry for the file
-                */
-               if (FcGlobalCacheCheckTime (file, &cache_file->info))
-               {
-                   name = cache_file->name;
-                   need_scan = FcFalse;
-                   FcGlobalCacheReferenced (cache, &cache_file->info);
-                   /* "." means the file doesn't contain a font */
-                   if (FcStrCmp (name, FC_FONT_FILE_INVALID) != 0)
-                   {
-                       font = FcNameParse (name);
-                       if (font)
-                           if (!FcPatternAddString (font, FC_FILE, file))
-                               ret = FcFalse;
-                   }
-               }
-           }
-           else if ((cache_dir = FcGlobalCacheDirGet (cache, file,
-                                                      strlen ((const char *) file),
-                                                      FcFalse)))
-           {
-               if (FcGlobalCacheCheckTime (cache_dir->info.file, 
-                                           &cache_dir->info))
-               {
-                   font = 0;
-                   need_scan = FcFalse;
-                   FcGlobalCacheReferenced (cache, &cache_dir->info);
-                   if (!FcStrSetAdd (dirs, file))
-                       ret = FcFalse;
-               }
-           }
-       }
        /*
         * Nothing in the cache, scan the file
         */
-       if (need_scan)
+       if (FcDebug () & FC_DBG_SCAN)
        {
-           if (FcDebug () & FC_DBG_SCAN)
-           {
-               printf ("\tScanning file %s...", file);
-               fflush (stdout);
-           }
-           font = FcFreeTypeQuery (file, id, blanks, &count);
-           if (FcDebug () & FC_DBG_SCAN)
-               printf ("done\n");
-           isDir = FcFalse;
-           if (!font && FcFileIsDir (file))
-           {
-               isDir = FcTrue;
-               ret = FcStrSetAdd (dirs, file);
-           }
-           /*
-            * Update the cache
-            */
-           if (cache && font)
-           {
-               FcChar8 *unparse;
-
-               unparse = FcNameUnparse (font);
-               if (unparse)
-               {
-                   (void) FcGlobalCacheUpdate (cache, file, id, unparse);
-                   FcStrFree (unparse);
-               }
-           }
+           printf ("\tScanning file %s...", file);
+           fflush (stdout);
        }
+       font = FcFreeTypeQuery (file, id, blanks, &count);
+       if (FcDebug () & FC_DBG_SCAN)
+           printf ("done\n");
        /*
         * Add the font
         */
@@ -150,23 +73,50 @@ FcFileScanConfig (FcFontSet        *set,
                ret = FcFalse;
            }
        }
+       else if (font)
+           FcPatternDestroy (font);
        id++;
     } while (font && ret && id < count);
     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);
 }
 
-#define FC_MAX_FILE_LEN            4096
+/*
+ * 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);
+}
 
 /*
  * Scan 'dir', adding font files to 'set' and
@@ -176,95 +126,167 @@ FcFileScan (FcFontSet        *set,
 FcBool
 FcDirScanConfig (FcFontSet     *set,
                 FcStrSet       *dirs,
-                FcGlobalCache  *cache,
                 FcBlanks       *blanks,
                 const FcChar8  *dir,
                 FcBool         force,
                 FcConfig       *config)
 {
     DIR                        *d;
+    FcChar8            *canon_dir;
     struct dirent      *e;
+    FcStrSet           *dirlist, *filelist;
     FcChar8            *file;
     FcChar8            *base;
     FcBool             ret = FcTrue;
+    FcFontSet          *tmpSet;
+    int                        i;
 
-    if (config && !FcConfigAcceptFilename (config, dir))
-       return FcTrue;
+    canon_dir = FcStrCanonFilename (dir);
+    if (!canon_dir) canon_dir = (FcChar8 *) dir;
+    
+    if (config && !FcConfigAcceptFilename (config, canon_dir)) {
+       ret = FcTrue;
+       goto bail;
+    }
 
     if (!force)
     {
-       /*
-        * Check fonts.cache-<version> file
-        */
-       if (FcDirCacheReadDir (set, dirs, dir, config))
-       {
-           if (cache)
-               FcGlobalCacheReferenceSubdir (cache, dir);
-           return FcTrue;
+       if (FcDirCacheRead (set, dirs, canon_dir, config)) {
+           ret = FcTrue;
+           goto bail;
        }
-    
-       /*
-        * Check ~/.fonts.cache-<version> file
-        */
-       if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir, config))
-           return FcTrue;
     }
     
+    if (FcDebug () & FC_DBG_FONTSET)
+       printf ("cache scan dir %s\n", canon_dir);
+
     /* freed below */
-    file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
-    if (!file)
-       return FcFalse;
+    file = (FcChar8 *) malloc (strlen ((char *) canon_dir) + 1 + FC_MAX_FILE_LEN + 1);
+    if (!file) {
+       ret = FcFalse;
+       goto bail;
+    }
 
-    strcpy ((char *) file, (char *) dir);
+    strcpy ((char *) file, (char *) canon_dir);
     strcat ((char *) file, "/");
     base = file + strlen ((char *) file);
     
     if (FcDebug () & FC_DBG_SCAN)
-       printf ("\tScanning dir %s\n", dir);
+       printf ("\tScanning dir %s\n", canon_dir);
        
-    d = opendir ((char *) dir);
-    
+    d = opendir ((char *) canon_dir);
     if (!d)
     {
-       free (file);
        /* Don't complain about missing directories */
        if (errno == ENOENT)
-           return FcTrue;
-       return FcFalse;
+           ret = FcTrue;
+       else
+           ret = FcFalse;
+       goto bail_1;
+    }
+
+    tmpSet = FcFontSetCreate();
+    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 (set, 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);
+    /*
+     * Sort files and dirs to make things prettier
+     */
+    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);
+    
     /*
      * Now that the directory has been scanned,
-     * add the cache entry 
+     * write out the cache file
      */
-    if (ret && cache)
-       FcGlobalCacheUpdate (cache, dir, 0, 0);
-       
+    FcDirCacheWrite (tmpSet, dirlist, canon_dir, config);
+
+    /*
+     * Add the discovered fonts to our internal non-cache list
+     */
+    for (i = 0; i < tmpSet->nfont; i++)
+       FcFontSetAdd (set, tmpSet->fonts[i]);
+
+    /*
+     * 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);
+    
+ bail_1:
+    free (file);
+ bail:
+    if (canon_dir != dir) free (canon_dir);
     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)
+FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
 {
-    return FcDirCacheWriteDir (set, dirs, dir);
+    return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ());
 }