Blob Blame History Raw
Description: implement zlib support to blisstog utility
 zlib is implemented to handle directly flate and compress files
 for the blisstog nauty tool.
Origin: debian
Author: Jerome Benoit <calculus@rezozer.net>
Last-Update: 2016-03-18

--- a/blisstog.c
+++ b/blisstog.c
@@ -9,45 +9,37 @@
   -n#:#  Specify a range of n values for output\n\
   Input files with name *.gz are ungzipped\n"
 
-#define ZCAT "gunzip -c"  /* name of zcat command (might be "gunzip -c") */
-
 /*************************************************************************/
 
+#include <zlib.h>
 #include "gtools.h" 
 
+#define BUFSIZE 256
+
 typedef struct 
 {
    int v,w;
 } vpair;
 
-static int
-nextchar(FILE *f)
-{
-    char s[2];
-
-    if (fscanf(f,"%1s",s) != 1) return EOF;
-    else                        return s[0];
-}
-
 static boolean
-readblissgraph(FILE *f, sparsegraph *g)
+readblissgraph(gzFile f, sparsegraph *g)
 /* Reads a graph from Bliss format into a sparse graph */
 {
-    int n,c;
+    int n;
     unsigned long ne,j;
     int haven;
     int i,v,w;
-    int haveptn;
     DYNALLSTAT(vpair,elist,elist_sz);
+    char buffer[BUFSIZE];
 
+    memset(buffer, '\0', BUFSIZE);
     haven = 0;
     j = 0;
-    while ((c = nextchar(f)) >= 0)
+    while (gzgets(f, buffer, BUFSIZE) != NULL && strlen(buffer) < BUFSIZE - 1)
     {
-	switch (c)
+	switch (*buffer)
 	{
 	case 'c':
-	    while ((c = getc(f)) != '\n' && c != EOF) {}
 	    break;
 
 	case 'p':
@@ -56,7 +48,7 @@
 		fprintf(stderr,"Duplicate p line\n");
 		exit(1);
 	    }
-	    if (fscanf(f," edge %d %lu",&n,&ne) != 2)
+	    if (sscanf(buffer,"p edge %d %lu",&n,&ne) != 2)
 	    {
 		fprintf(stderr,"Bad p line\n");
 		return FALSE;
@@ -71,7 +63,7 @@
                 fprintf(stderr,"Missing p line\n");
                 return FALSE;
             }  
-            if (fscanf(f,"%d%d",&w,&v) != 2 || w < 1 || w > n)
+            if (sscanf(buffer,"n %d%d",&w,&v) != 2 || w < 1 || w > n)
             {
                 fprintf(stderr,"Bad n line\n");
                 return FALSE;
@@ -84,7 +76,7 @@
 		fprintf(stderr,"Missing p line or too many e lines\n");
 		return FALSE;
 	    }
-	    if (fscanf(f,"%d%d",&v,&w) != 2 || v < 1 || w < 1 || v > n || w > n)
+	    if (sscanf(buffer,"e %d%d",&v,&w) != 2 || v < 1 || w < 1 || v > n || w > n)
 	    {
 		fprintf(stderr,"Bad e line\n");
 		return FALSE;
@@ -94,11 +86,22 @@
 	    break;
 
 	default:
-	    fprintf(stderr,"Unknown line %c\n",c);
+	    fprintf(stderr,"Unknown line\n");
 	    return FALSE;
 	}
     }
 
+    if (errno)
+    {
+	fputs("Corrupted data file\n", stderr);
+	return FALSE;
+    }
+    else if (strlen(buffer) == BUFSIZE - 1)
+    {
+	fputs("Corruped data line\n", stderr);
+	return FALSE;
+    }
+
     if (j != ne)
     {
         fprintf(stderr,"Wrong number of e lines\n");
@@ -135,13 +138,10 @@
 int
 main(int argc, char *argv[])
 {
-    FILE *infile;
+    gzFile infile;
     int j,firstarg;
     SG_DECL(g);
-    size_t flen;
-    boolean ispipe;
     int nmin,nmax;
-    char zcmd[515];
 
     HELP; PUTVERSION;
 
@@ -160,53 +160,46 @@
 
     if (argc == firstarg)
     {
-	if (!readblissgraph(stdin,&g))
+	if ((infile = gzdopen(STDIN_FILENO,"r")) == NULL)
 	{
-	    fprintf(stderr,">E Bliss error in file %s\n","stdin");
+	    fputs(">E Can't open stdin\n", stderr);
 	    gt_abort(NULL);
 	}
 	else
-	    writes6_sg(stdout,&g);
+	{
+	    if (!readblissgraph(stdin,&g))
+	    {
+		fprintf(stderr,">E Bliss error in file %s\n","stdin");
+		gt_abort(NULL);
+	    }
+	    else
+		writes6_sg(stdout,&g);
+	    gzclose(infile);
+	}
     }
     else
     {
         for (j = firstarg; j < argc; ++j)
 	{
-	    flen = strlen(argv[j]);
-            if (flen >= 3 && strcmp(argv[j]+flen-3,".gz") == 0)
-            {
-                sprintf(zcmd,"%s \"%s\"",ZCAT,argv[j]);
-                if ((infile = popen(zcmd,"r")) == NULL)
-                {
-                    fprintf(stderr,
-                         ">E blisstog: cannot open zcat pipe for \"%s\"\n",
-                         argv[j]);
-                    gt_abort(NULL);
-                }
-		ispipe = TRUE;
-            }
-            else
-            {
-	        if ((infile = fopen(argv[j],"r")) == NULL)
-	        {
-	            fprintf(stderr,">E Can't open file %s\n",argv[j]);
-		    gt_abort(NULL);
-	        }
-		ispipe = FALSE;
-	    }
-
-	    if (!readblissgraph(infile,&g))
+	    if ((infile = gzopen(argv[j],"r")) == NULL)
 	    {
-	        fprintf(stderr,">E Bliss error in file %s\n",argv[j]);
+		fprintf(stderr,">E Can't open file %s\n",argv[j]);
 		gt_abort(NULL);
 	    }
-	    else if (nmax < 0 || (g.nv >= nmin && g.nv <= nmax))
-            {
-		sortlists_sg(&g);
-	        writes6_sg(stdout,&g);
+	    else
+	    {
+		if (!readblissgraph(infile,&g))
+		{
+		    fprintf(stderr,">E Bliss error in file %s\n",argv[j]);
+		    gt_abort(NULL);
+		}
+		else if (nmax < 0 || (g.nv >= nmin && g.nv <= nmax))
+		{
+		    sortlists_sg(&g);
+		    writes6_sg(stdout,&g);
+		}
+		gzclose(infile);
 	    }
-
-	    if (ispipe) pclose(infile); else fclose(infile);
         }
     }