Include <sys/types.h> and "config.h".
authorhanda <handa>
Fri, 28 May 2004 00:25:19 +0000 (00:25 +0000)
committerhanda <handa>
Fri, 28 May 2004 00:25:19 +0000 (00:25 +0000)
(next_file): New function.
(main): Call next_file.

example/otflist.c

index 78c0678..200ab1e 100644 (file)
@@ -24,11 +24,13 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
+#include <sys/types.h>
 #include <dirent.h>
 
 #include <ft2build.h>
 #include FT_FREETYPE_H
 
+#include "config.h"
 #include <otf.h>
 
 /* Format MSG by FMT and print the result to the stderr, and exit.  */
@@ -125,13 +127,58 @@ filter (const struct dirent *direntry)
              || ! strncmp (ext, ".pfb", 4)));
 }
 
+int dir_index = 0;
+
+#ifdef HAVE_SCANDIR
+
+struct dirent **namelist = NULL;
+int num_files = 0;
+
+char *
+next_file (char *dirname)
+{
+  if (dir_index == 0)
+    {
+#ifdef HAVE_ALPHASORT
+      num_files = scandir (".", &namelist, filter, alphasort);
+#else
+      num_files = scandir (".", &namelist, filter, NULL);
+#endif
+    }
+  if (dir_index == num_files)
+    return NULL;
+  return namelist[dir_index++]->d_name;
+}
+
+#else  /* not HAVE_SCANDIR */
+
+DIR *dirp;
+
+char *
+next_file (char *dirname)
+{
+  struct dirent *dirent;
+
+  if (dir_index == 0)
+    dirp = opendir (dirname);
+  while ((dirent = readdir (dirp))
+        && (strcmp (dirent->d_name, ".") == 0
+            || strcmp (dirent->d_name, "..") == 0));
+  if (! dirent)
+    return NULL;
+  dir_index++;
+  return dirent->d_name;
+}
+
+#endif /* not HAVE_SCANDIR */
+
 int
 main (int argc, char **argv)
 { 
   FT_Library ft_library;
   FT_Face face;
+  char *filename;
   int long_format = 0;
-  struct dirent **namelist;
   int n;
   int i, j;
 
@@ -150,48 +197,49 @@ main (int argc, char **argv)
       if (chdir (argv[1]) < 0)
        FATAL_ERROR ("Can't change directory to %s\n", argv[1]);
     }
-  n = scandir (".", &namelist, filter, alphasort);
-
-  for (i = 0; i < n; i++)
-    if (! FT_New_Face (ft_library, namelist[i]->d_name, 0, &face))
-      {
-       OTF *otf = OTF_open (namelist[i]->d_name);
-       char *name, *family = NULL, *style = NULL;
-
-       if (otf)
-         {
-           OTF_get_table (otf, "name");
-
-           if (! (family = otf->name->name[16]))
-             family = otf->name->name[1];
-           if (! (style = otf->name->name[17]))
-             style = otf->name->name[2];
-         }
-       if (! family)
-         family = face->family_name;
-       if (! style)
-         style = face->style_name;
-
-       name = alloca (strlen (namelist[i]->d_name)
-                      + strlen (family)
-                      + 4);
-       sprintf (name, "%s (%s)", namelist[i]->d_name, family);
-       printf ("%-40s %s", name, style);
-       for (j = 0; j < face->num_charmaps; j++)
-         printf (" %d-%d", face->charmaps[j]->platform_id,
-                 face->charmaps[j]->encoding_id);
-       printf ("\n");
-       if (otf && long_format)
-         {
-           print_gsub_gpos_info (otf, "GSUB");
-           print_gsub_gpos_info (otf, "GPOS");
-         }
-       if (otf)
-         OTF_close (otf);
-      }
-    else
-      {
-       printf ("%s fail to open\n", namelist[i]->d_name);
-      }
+
+  while ((filename = next_file (".")) != NULL)
+    {
+      if (! FT_New_Face (ft_library, filename, 0, &face))
+       {
+         OTF *otf = OTF_open (filename);
+         char *name, *family = NULL, *style = NULL;
+
+         if (otf)
+           {
+             OTF_get_table (otf, "name");
+
+             if (! (family = otf->name->name[16]))
+               family = otf->name->name[1];
+             if (! (style = otf->name->name[17]))
+               style = otf->name->name[2];
+           }
+         if (! family)
+           family = face->family_name;
+         if (! style)
+           style = face->style_name;
+
+         name = alloca (strlen (filename)
+                        + strlen (family)
+                        + 4);
+         sprintf (name, "%s (%s)", filename, family);
+         printf ("%-40s %s", name, style);
+         for (j = 0; j < face->num_charmaps; j++)
+           printf (" %d-%d", face->charmaps[j]->platform_id,
+                   face->charmaps[j]->encoding_id);
+         printf ("\n");
+         if (otf && long_format)
+           {
+             print_gsub_gpos_info (otf, "GSUB");
+             print_gsub_gpos_info (otf, "GPOS");
+           }
+         if (otf)
+           OTF_close (otf);
+       }
+      else
+       {
+         printf ("%s fail to open\n", filename);
+       }
+    }
   exit (0);
 }