]> 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 f466e3ecbc62f3af09f1dd8feef1b698d5e44391..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
@@ -167,125 +167,37 @@ usage (char *program)
     exit (1);
 }
 
-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];
-    char               subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-    off_t current_arch_start = 0;
-
-    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) 
-    {
-       char * dir;
-       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);
-
-       do
-       {
-           if (!FcCacheReadString (fd, subdirName, 
-                                   sizeof (subdirName)) ||
-               !strlen (subdirName))
-               break;
-           /* then don't do anything with subdirName. */
-       } while (1);
-
-       if (!FcDirCacheConsume (fd, name_buf, set, 0))
-           goto bail1;
-
-       dir = malloc (strlen (name_buf) + 2);
-       if (!dir)
-           goto bail1;
-
-       strcpy (dir, 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)
 {
+    FILE *file;
     int fd;
-    char * current_arch_machine_name;
-    off_t current_arch_start = 0;
     char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-    static char name_buf[8192], *dir;
+    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;
 
-    FcCacheReadString (fd, name_buf, sizeof (name_buf));
-    if (!strlen (name_buf))
-       goto bail;
-    if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0)
-       goto bail;
+    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);
 
-    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, 0))
-       goto bail2;
-    free (dir);
-
-    close(fd);
     return name_buf;
 
- bail2:
-    free (dir);
-
  bail1:
-    close (fd);
+    fclose (file);
  bail:
     return 0;
 }
@@ -377,16 +289,6 @@ bail2:
     return FcFalse;
 }
 
-FcBool
-FcFileIsDir (const FcChar8 *file)
-{
-    struct stat            statb;
-
-    if (stat ((const char *) file, &statb) != 0)
-       return FcFalse;
-    return S_ISDIR(statb.st_mode);
-}
-
 int
 main (int argc, char **argv)
 {
@@ -396,6 +298,7 @@ main (int argc, char **argv)
     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)
@@ -417,6 +320,14 @@ 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]);
 
@@ -424,8 +335,8 @@ main (int argc, char **argv)
     {
         char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i], 
                                                (FcChar8 *)"/dummy");
-        if (!FcDirScanConfig (fs, dirs, 0, 0, 
-                              (const FcChar8 *)argv[i], FcFalse, 0))
+        if (!FcDirScanConfig (fs, dirs, 0, 
+                              (const FcChar8 *)argv[i], FcFalse, config))
             fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
         else
         {
@@ -438,10 +349,7 @@ main (int argc, char **argv)
        FcCachePrintSet (fs, dirs, name_buf);
     else
     {
-        FcStrSetDestroy (dirs);
-        dirs = FcStrSetCreate ();
-        if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
-            ;
+       printf ("nothing to do\n");
     }
 
     FcStrSetDestroy (dirs);