]> git.wh0rd.org - fontconfig.git/blobdiff - fc-cat/fc-cat.c
Add self to AUTHORS list.
[fontconfig.git] / fc-cat / fc-cat.c
index 4f5b71d5e13a18b69fac7785d989b9040b8a1df8..14e5c164f8eb0ec86ce8fd74c48925ef63fc2981 100644 (file)
@@ -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)
 {
@@ -164,46 +167,114 @@ 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 FcBool 
+FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file)
 {
+    char               name_buf[8192];
     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;
+       goto bail;
 
     current_arch_machine_name = FcCacheMachineSignature();
     fd = open(cache_file, O_RDONLY);
     if (fd == -1)
-        goto bail;
+       goto bail;
 
     current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
     if (current_arch_start < 0)
-        goto bail1;
+       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) 
+    {
+       char * dir, * ls;
+       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, name_buf, set))
+           goto bail1;
+
+       dir = strdup(name_buf);
+       strcat (dir, "/");
+
+       FcCachePrintSet (set, dirs, dir);
+       free (dir);
+
+       FcFontSetDestroy (set);
+       set = FcFontSetCreate();
+    }
+
+ bail1:
+    close (fd);
+ bail:
+    return FcFalse;
+}
+
+/* read serialized state from the cache file */
+static char *
+FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_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], *dir;
+    FcChar8 * ls;
+
+    if (!cache_file)
+        goto bail;
+
+    current_arch_machine_name = FcCacheMachineSignature();
+    fd = open(cache_file, O_RDONLY);
+    if (fd == -1)
+        goto bail;
+
+    FcCacheReadString (fd, name_buf, sizeof (name_buf));
+    if (!strlen (name_buf))
+       goto bail;
+    if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0)
+       goto bail;
+    printf ("fc-cat: printing directory cache for cache which would be named %s\n", 
+           name_buf);
+
+    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
+    if (current_arch_start < 0)
+        goto bail1;
+
     while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
         FcStrSetAdd (dirs, (FcChar8 *)subdirName);
 
+    dir = strdup(name_buf);
+    ls = FcStrLastSlash ((FcChar8 *)dir);
+    if (ls)
+       *ls = 0;
+
     if (!FcDirCacheConsume (fd, dir, set))
-       goto bail1;
-       
+       goto bail2;
+    free (dir);
+
     close(fd);
-    return FcTrue;
+    return name_buf;
+
+ bail2:
+    free (dir);
 
  bail1:
     close (fd);
  bail:
-    return FcFalse;
+    return 0;
 }
 
 /*
@@ -223,14 +294,14 @@ 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;
-    FcBool         ret;
     FcStrList      *list;
 
     list = FcStrListCreate (dirs);
@@ -239,7 +310,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)
@@ -259,7 +330,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)
@@ -290,8 +361,6 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
 bail3:
     FcStrListDone (list);
 bail2:
-bail1:
-bail0:
     return FcFalse;
 }
 
@@ -299,11 +368,11 @@ 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;
 
 #if HAVE_GETOPT_LONG
     while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
@@ -325,8 +394,18 @@ main (int argc, char **argv)
     i = 1;
 #endif
 
-    if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
-       FcCachePrintSet (fs, dirs, argv[i]);
+    if (i >= argc)
+        usage (argv[0]);
+
+    if (name_buf = FcCacheFileRead (fs, dirs, argv[i]))
+       FcCachePrintSet (fs, dirs, name_buf);
+    else
+    {
+        FcStrSetDestroy (dirs);
+        dirs = FcStrSetCreate ();
+        if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
+            ;
+    }
 
     FcStrSetDestroy (dirs);
     FcFontSetDestroy (fs);