]> git.wh0rd.org - fontconfig.git/blobdiff - fc-cat/fc-cat.c
Fix up fc-cache and fc-cat for no global cache changes.
[fontconfig.git] / fc-cat / fc-cat.c
index 64746538bcd13dd893a6ffcc5c9c6efb6979401b..36f8bd9865047549fd1399cd969ffaa0b2a3dc9c 100644 (file)
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <fontconfig/fontconfig.h>
-#include <../src/fccache.c>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <libgen.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #else
 #define HAVE_GETOPT 1
 #endif
 
+#include <fontconfig/fontconfig.h>
+#include <../src/fccache.c>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
 #ifndef HAVE_GETOPT
 #define HAVE_GETOPT 0
 #endif
@@ -78,6 +78,9 @@ extern int optind, opterr, optopt;
 #define PUTC(c,f) putc(c,f)
 #endif
 
+FcBool
+FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
+
 static FcBool
 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
 {
@@ -165,45 +168,38 @@ usage (char *program)
 }
 
 /* read serialized state from the cache file */
-static FcBool
-FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
+static char *
+FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
 {
+    FILE *file;
     int fd;
-    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];
+    static char name_buf[8192];
+    FcChar8 * ls;
+    char * buf;
+    int i;
 
     if (!cache_file)
         goto bail;
 
-    current_arch_machine_name = FcCacheMachineSignature();
-    fd = open(cache_file, O_RDONLY);
-    if (fd == -1)
+    file = fopen(cache_file, "rb");
+    if (file == NULL)
         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)
+    if (!FcDirCacheConsume (file, set, dirs, NULL, name_buf))
        goto bail1;
+    
+    fclose (file);
+    
+    printf ("fc-cat: printing directory cache for cache which would be named %s\n", 
+           name_buf);
 
-    while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
-        FcStrSetAdd (dirs, (FcChar8 *)subdirName);
-
-    if (!FcDirCacheConsume (fd, dir, set))
-       goto bail1;
-       
-    close(fd);
-    return FcTrue;
+    return name_buf;
 
  bail1:
-    close (fd);
+    fclose (file);
  bail:
-    return FcFalse;
+    return 0;
 }
 
 /*
@@ -223,11 +219,12 @@ FcFileBaseName (const char *cache, const FcChar8 *file)
 }
 
 FcBool
-FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
+FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
 {
     FcPattern      *font;
     FcChar8        *name, *dir;
     const FcChar8   *file, *base;
+    int                    ret;
     int                    n;
     int                    id;
     FcStrList      *list;
@@ -238,7 +235,7 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
     
     while ((dir = FcStrListNext (list)))
     {
-       base = FcFileBaseName (cache_file, dir);
+       base = FcFileBaseName (base_name, dir);
        if (!FcCacheWriteStringOld (stdout, base))
            goto bail3;
        if (PUTC (' ', stdout) == EOF)
@@ -258,7 +255,7 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
        font = set->fonts[n];
        if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
            goto bail3;
-       base = FcFileBaseName (cache_file, file);
+       base = FcFileBaseName (base_name, file);
        if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
            goto bail3;
        if (FcDebug () & FC_DBG_CACHEV)
@@ -289,8 +286,6 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
 bail3:
     FcStrListDone (list);
 bail2:
-bail1:
-bail0:
     return FcFalse;
 }
 
@@ -298,11 +293,12 @@ int
 main (int argc, char **argv)
 {
     int                i;
-    int                ret;
 #if HAVE_GETOPT_LONG || HAVE_GETOPT
     int                c;
     FcFontSet  *fs = FcFontSetCreate();
     FcStrSet    *dirs = FcStrSetCreate();
+    char       *name_buf;
+    FcConfig   *config;
 
 #if HAVE_GETOPT_LONG
     while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
@@ -324,11 +320,37 @@ main (int argc, char **argv)
     i = 1;
 #endif
 
+    config = FcInitLoadConfig ();
+    if (!config)
+    {
+       fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
+       return 1;
+    }
+    FcConfigSetCurrent (config);
+    
     if (i >= argc)
         usage (argv[0]);
 
-    if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
-       FcCachePrintSet (fs, dirs, argv[i]);
+    if (FcFileIsDir ((const FcChar8 *)argv[i]))
+    {
+        char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i], 
+                                               (FcChar8 *)"/dummy");
+        if (!FcDirScanConfig (fs, dirs, 0, 
+                              (const FcChar8 *)argv[i], FcFalse, config))
+            fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
+        else
+        {
+            /* sorry, we can't tell you where the cache file is. */
+            FcCachePrintSet (fs, dirs, dummy_name);
+            FcStrFree ((FcChar8 *)dummy_name);
+        }
+    }
+    else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0)
+       FcCachePrintSet (fs, dirs, name_buf);
+    else
+    {
+       printf ("nothing to do\n");
+    }
 
     FcStrSetDestroy (dirs);
     FcFontSetDestroy (fs);