]> git.wh0rd.org - fontconfig.git/blobdiff - fc-cat/fc-cat.c
Add codepath for reading global cache files as well.
[fontconfig.git] / fc-cat / fc-cat.c
index a39232712d511017990700a2312f40d414783d2e..9fb930bb81883499cf58dca30070ffc329d550c4 100644 (file)
@@ -164,6 +164,59 @@ usage (char *program)
     exit (1);
 }
 
+static FcBool 
+FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
+{
+    char               name_buf[8192];
+    int fd;
+    FcGlobalCacheDir   *d, *next;
+    char * current_arch_machine_name;
+    char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
+    off_t current_arch_start = 0;
+    char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
+
+    if (!cache_file)
+       goto bail;
+
+    current_arch_machine_name = FcCacheMachineSignature();
+    fd = open(cache_file, O_RDONLY);
+    if (fd == -1)
+       goto bail;
+
+    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
+    if (current_arch_start < 0)
+       goto bail1;
+
+    lseek (fd, current_arch_start, SEEK_SET);
+    if (FcCacheReadString (fd, candidate_arch_machine_name, 
+                          sizeof (candidate_arch_machine_name)) == 0)
+       goto bail1;
+
+    while (1) 
+    {
+       off_t targ;
+
+       FcCacheReadString (fd, name_buf, sizeof (name_buf));
+       if (!strlen(name_buf))
+           break;
+       printf ("fc-cat: printing global cache contents for dir %s\n", 
+               name_buf);
+
+       if (!FcDirCacheConsume (fd, dir, set))
+           goto bail1;
+
+       FcCachePrintSet (set, dirs, name_buf);
+
+       FcFontSetDestroy (set);
+       set = FcFontSetCreate();
+    }
+
+ bail1:
+    close (fd);
+ bail:
+    return FcFalse;
+}
+
 /* read serialized state from the cache file */
 static FcBool
 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
@@ -198,13 +251,11 @@ FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
        goto bail1;
        
     close(fd);
-    free (cache_file);
     return FcTrue;
 
  bail1:
     close (fd);
  bail:
-    free (cache_file);
     return FcFalse;
 }
 
@@ -230,9 +281,9 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
     FcPattern      *font;
     FcChar8        *name, *dir;
     const FcChar8   *file, *base;
+    int                    ret;
     int                    n;
     int                    id;
-    FcBool         ret;
     FcStrList      *list;
 
     list = FcStrListCreate (dirs);
@@ -301,7 +352,6 @@ int
 main (int argc, char **argv)
 {
     int                i;
-    int                ret;
 #if HAVE_GETOPT_LONG || HAVE_GETOPT
     int                c;
     FcFontSet  *fs = FcFontSetCreate();
@@ -327,8 +377,19 @@ main (int argc, char **argv)
     i = 1;
 #endif
 
-    if (FcCacheFileRead (fs, dirs, dirname (argv[i]), argv[i]))
+    if (i >= argc)
+        usage (argv[0]);
+
+    if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
        FcCachePrintSet (fs, dirs, argv[i]);
+    else
+    {
+        FcStrSetDestroy (dirs);
+        dirs = FcStrSetCreate ();
+        if (FcCacheGlobalFileReadAndPrint (fs, dirs, dirname (strdup (argv[i])),
+                                           argv[i]))
+            ;
+    }
 
     FcStrSetDestroy (dirs);
     FcFontSetDestroy (fs);