]> git.wh0rd.org - fontconfig.git/blobdiff - fc-cat/fc-cat.c
Fix fc-cat again. Sigh.
[fontconfig.git] / fc-cat / fc-cat.c
index 4f5b71d5e13a18b69fac7785d989b9040b8a1df8..c033bdc776359ec1886ddacb39612d21750220f3 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
@@ -53,6 +53,7 @@
 #include <getopt.h>
 const struct option longopts[] = {
     {"version", 0, 0, 'V'},
+    {"verbose", 0, 0, 'v'},
     {"help", 0, 0, '?'},
     {NULL,0,0,0},
 };
@@ -78,6 +79,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)
 {
@@ -164,46 +168,22 @@ usage (char *program)
     exit (1);
 }
 
-/* read serialized state from the cache file */
-static FcBool
-FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
+static int
+FcCacheFileOpen (char *cache_file, off_t *size)
 {
     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];
-
-    if (!cache_file)
-        goto bail;
-
-    current_arch_machine_name = FcCacheMachineSignature();
-    fd = open(cache_file, O_RDONLY);
-    if (fd == -1)
-        goto bail;
+    struct stat file_stat;
 
-    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
-    if (current_arch_start < 0)
-        goto bail1;
+    fd = open(cache_file, O_RDONLY | O_BINARY);
+    if (fd < 0)
+        return -1;
 
-    lseek (fd, current_arch_start, SEEK_SET);
-    if (FcCacheReadString (fd, candidate_arch_machine_name, 
-                          sizeof (candidate_arch_machine_name)) == 0)
-       goto bail1;
-
-    while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
-        FcStrSetAdd (dirs, (FcChar8 *)subdirName);
-
-    if (!FcDirCacheConsume (fd, dir, set))
-       goto bail1;
-       
-    close(fd);
-    return FcTrue;
-
- bail1:
-    close (fd);
- bail:
-    return FcFalse;
+    if (fstat (fd, &file_stat) < 0) {
+       close (fd); 
+       return -1;
+    }
+    *size = file_stat.st_size;
+    return fd;
 }
 
 /*
@@ -214,23 +194,22 @@ static const FcChar8 *
 FcFileBaseName (const char *cache, const FcChar8 *file)
 {
     const FcChar8   *cache_slash;
+    int                    cache_len = strlen (cache);
 
-    cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
-    if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
-                                (cache_slash + 1) - (const FcChar8 *)cache))
-       return file + ((cache_slash + 1) - (const FcChar8 *)cache);
+    if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
+       return file + cache_len + 1;
     return 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;
-    FcBool         ret;
     FcStrList      *list;
 
     list = FcStrListCreate (dirs);
@@ -239,7 +218,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)
@@ -256,14 +235,15 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
     
     for (n = 0; n < set->nfont; n++)
     {
-       font = set->fonts[n];
+       FcPattern   **fonts = FcFontSetFonts (set);
+       FcPattern   *encoded_font = fonts[n];
+       FcPattern   *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
+
        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)
-           printf (" write file \"%s\"\n", base);
        if (!FcCacheWriteStringOld (stdout, base))
            goto bail3;
        if (PUTC (' ', stdout) == EOF)
@@ -290,25 +270,48 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
 bail3:
     FcStrListDone (list);
 bail2:
-bail1:
-bail0:
     return FcFalse;
 }
 
+FcCache *
+FcCacheFileMap (const FcChar8 *file)
+{
+    FcCache *cache;
+    int            fd;
+    struct stat file_stat;
+
+    fd = open (file, O_RDONLY | O_BINARY);
+    if (fd < 0)
+       return NULL;
+    if (fstat (fd, &file_stat) < 0) {
+       close (fd);
+       return NULL;
+    }
+    if (FcCacheLoad (fd, file_stat.st_size, &cache)) {
+       close (fd);
+       return cache;
+    }
+    close (fd);
+    return NULL;
+}
+
 int
 main (int argc, char **argv)
 {
     int                i;
-    int                ret;
+    int                ret = 0;
+    FcFontSet  *fs;
+    FcStrSet    *dirs;
+    FcCache    *cache;
+    FcConfig   *config;
+    int                verbose = 0;
 #if HAVE_GETOPT_LONG || HAVE_GETOPT
     int                c;
-    FcFontSet  *fs = FcFontSetCreate();
-    FcStrSet    *dirs = FcStrSetCreate();
 
 #if HAVE_GETOPT_LONG
-    while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
+    while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1)
 #else
-    while ((c = getopt (argc, argv, "fsVv?")) != -1)
+    while ((c = getopt (argc, argv, "Vv?")) != -1)
 #endif
     {
        switch (c) {
@@ -316,6 +319,9 @@ main (int argc, char **argv)
            fprintf (stderr, "fontconfig version %d.%d.%d\n", 
                     FC_MAJOR, FC_MINOR, FC_REVISION);
            exit (0);
+       case 'v':
+           verbose++;
+           break;
        default:
            usage (argv[0]);
        }
@@ -325,11 +331,50 @@ main (int argc, char **argv)
     i = 1;
 #endif
 
-    if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
-       FcCachePrintSet (fs, dirs, argv[i]);
+    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]);
+
+    for (; i < argc; i++)
+    {
+       int     j;
+       off_t   size;
+       intptr_t        *cache_dirs;
+       
+       if (FcFileIsDir ((const FcChar8 *)argv[i]))
+           cache = FcDirCacheMap ((const FcChar8 *) argv[i], config);
+       else
+           cache = FcCacheFileMap (argv[i]);
+       if (!cache)
+       {
+           perror (argv[i]);
+           ret++;
+           continue;
+       }
+       
+       dirs = FcStrSetCreate ();
+       fs = FcCacheSet (cache);
+       cache_dirs = FcCacheDirs (cache);
+       for (j = 0; j < cache->dirs_count; j++) 
+           FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
+                                             cache_dirs[j],
+                                             FcChar8));
+
+       if (verbose)
+           printf ("Name: %s\nDirectory: %s\n", argv[i], FcCacheDir(cache));
+        FcCachePrintSet (fs, dirs, FcCacheDir (cache));
+
+       FcStrSetDestroy (dirs);
 
-    FcStrSetDestroy (dirs);
-    FcFontSetDestroy (fs);
+       FcDirCacheUnmap (cache);
+    }
 
     return 0;
 }