info2html fixed



Hi,

I've finally fixed the bzip2 info file support in gnome-info2html 
in a not too ugly way..

I've attached the patch to bug 66334 but here is it again..

So, could someone review this patch so I could commit it ?

(and, no, I don't want to become maintainer of the helper converters..
:)
-- 
Frédéric Crozat
MandrakeSoft
? libgnome/help-converters/info/autoconf.info
? libgnome/help-converters/info/autoconf.info.bz2
? libgnome/help-converters/info/autoconf.info.gz
? libgnome/help-converters/info/bz.log
? libgnome/help-converters/info/filename
? libgnome/help-converters/info/gz.log
? libgnome/help-converters/info/info.patch
? libgnome/help-converters/info/t
? libgnome/help-converters/info/tt
? libgnome/help-converters/info/ttt
Index: libgnome/help-converters/info/data.h
===================================================================
RCS file: /cvs/gnome/libgnome/help-converters/info/data.h,v
retrieving revision 1.2
diff -u -r1.2 data.h
--- libgnome/help-converters/info/data.h	8 Mar 1998 21:24:42 -0000	1.2
+++ libgnome/help-converters/info/data.h	25 Apr 2002 16:50:27 -0000
@@ -1,5 +1,11 @@
 #ifndef DATA_H
 #define DATA_H
+#include <config.h>
+#include <zlib.h>
+#ifdef HAVE_LIBBZ2
+#include <bzlib.h>
+#endif
+#include <glib.h>
 
 /* data.h - first cut at data structures for info2html filter */
 /* many of these are motivated by the source code to the 'info' program */
@@ -44,6 +50,22 @@
   REFERENCE     *ref;
   struct info_menu_entry    *next;
 };
+
+
+enum file_type {GZIP, BZIP2 };
+#define READ_BUF_SIZE (32 * 1024)
+
+typedef struct{
+  enum file_type type;
+  gzFile      *gzfile;
+  char        *buffer;
+  int          eof;
+  gsize size;
+  gsize pos;
+#ifdef HAVE_LIBBZ2
+  BZFILE      *bzfile;
+#endif
+} ReadBuf;
 
 #define INFO_FF '\014'
 #define INFO_COOKIE '\037'
Index: libgnome/help-converters/info/html.c
===================================================================
RCS file: /cvs/gnome/libgnome/help-converters/info/html.c,v
retrieving revision 1.19
diff -u -r1.19 html.c
--- libgnome/help-converters/info/html.c	7 Apr 2002 18:10:02 -0000	1.19
+++ libgnome/help-converters/info/html.c	25 Apr 2002 16:50:27 -0000
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <ctype.h>
 
 #include <glib.h>
 
Index: libgnome/help-converters/info/main.c
===================================================================
RCS file: /cvs/gnome/libgnome/help-converters/info/main.c,v
retrieving revision 1.28
diff -u -r1.28 main.c
--- libgnome/help-converters/info/main.c	10 Jan 2002 00:00:20 -0000	1.28
+++ libgnome/help-converters/info/main.c	25 Apr 2002 16:50:28 -0000
@@ -5,16 +5,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <popt.h>
-#include <zlib.h>
-#ifdef HAVE_LIBBZ2
-#include <bzlib.h>
-#endif
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <limits.h>
 
-#include <glib.h>
 #include "data.h"
 #include "html.h"
 #include "parse.h"
@@ -45,11 +40,7 @@
 int
 main(int argc, const char **argv)
 {
-	gzFile f = NULL;
-	int bz = 0;
-#ifdef HAVE_LIBBZ2
-	BZFILE *bf=NULL;
-#endif
+        ReadBuf *f=NULL;
 	char line[250];
 	poptContext ctx;
 	int result;
@@ -110,7 +101,6 @@
 		ext = ".bz2";
 		sprintf(buf, "%s/%s.info.bz2", dirs[i], args[0]);		
 		if(file_exists(buf)) {
-		  bz = 1;
 		  break;
 		}
 #endif
@@ -172,61 +162,32 @@
 	/* No need to store all nodes, etc since we let web server */
 	/* handle resolving tags!                                  */
 	for (;1 || !foundit || !requested_nodename;) {
-	  if(bz)
+	  if(!f)
 	    {
-#ifdef HAVE_LIBBZ2
-	      if(!bf)
+	      if(args && args[curarg])
 		{
-		  if(args && args[curarg])
-		    {
-		      bf = bzopen(args[curarg++], "r");
-		      if(!f)
-			break;
-		      num_files_left = args[curarg]?1:0;
-		      for(work_line_number = 0, bzread(bf, line, sizeof(line)); *line != INFO_COOKIE;
-			  bzread(bf, line, sizeof(line)), work_line_number++)
-			/**/ ;
-		    }
-		  else
+		  f = readbuf_open (args[curarg++]);
+		  if(!f) {
+		    g_print("out\n");
 		    break;
+		  }
+		  num_files_left = args[curarg]?1:0;
+		  for(work_line_number = 0, readbuf_gets(f,line,sizeof(line)); *line != INFO_COOKIE;
+		      readbuf_gets(f,line,sizeof(line)), work_line_number++)
+		    /**/ ;
 		}
-	      if(!bzread(bf, line, sizeof(line)))
-		{
-		  bzclose(bf);
-		  bf = NULL;
-		  continue;
-		}
-#else
-	      g_assert_not_reached();
-#endif
+	      else
+		break;
 	    }
-	  else
+	  if(!readbuf_gets(f,line,sizeof(line)))
 	    {
-	      if(!f)
-		{
-		  if(args && args[curarg])
-		    {
-		      f = gzopen(args[curarg++], "r");
-		      if(!f)
-			break;
-		      num_files_left = args[curarg]?1:0;
-		      for(work_line_number = 0, gzgets(f, line, sizeof(line)); *line != INFO_COOKIE;
-			  gzgets(f, line, sizeof(line)), work_line_number++)
-			/**/ ;
-		    }
-		  else
-		    break;
-		}
-	      if(!gzgets(f, line, sizeof(line)))
-		{
-		  gzclose(f);
-		  f = NULL;
-		  continue;
-		}
+	      readbuf_close(f);
+	      f = NULL;
+	      continue;
 	    }
-		
-	  work_line_number++;
-		
+	
+	work_line_number++;
+
 		/* found a node definition line */
 	  if (!strncmp(line, "File:", 5)) {
 	    node = alloc_node();
Index: libgnome/help-converters/info/parse.c
===================================================================
RCS file: /cvs/gnome/libgnome/help-converters/info/parse.c,v
retrieving revision 1.4
diff -u -r1.4 parse.c
--- libgnome/help-converters/info/parse.c	2 May 2000 00:49:34 -0000	1.4
+++ libgnome/help-converters/info/parse.c	25 Apr 2002 16:50:28 -0000
@@ -6,7 +6,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <zlib.h>
 
 #include "parse.h"
 #include "data.h"
@@ -16,7 +15,7 @@
 
 /* main routine to read in a node once we found its start in file f */
 /* assumes NODE has already been allocated */
-int read_node (gzFile f, char *line, NODE *node)
+int read_node (ReadBuf *f, char *line, NODE *node)
 {
   /* we found a node line */
   if (!parse_node_line( node, line ))
@@ -116,7 +115,7 @@
 #define SEARCH_BUF_SIZE 1024
 #define CONTENTS_BUF_INCR 1024
 
-int read_node_contents( gzFile f, NODE *node )
+int read_node_contents( ReadBuf *f, NODE *node )
 {
   int nread;
   int found;
@@ -139,14 +138,14 @@
   /* and save contents as we go along                 */
   for ( found=0 ; !found ; )
     {
-      status=gzgets(f,  searchbuf, SEARCH_BUF_SIZE);
+      status=readbuf_gets(f,searchbuf, SEARCH_BUF_SIZE);
       linelen = strlen( searchbuf );
       for (found=0, ptr = searchbuf; *ptr && !found; ptr++)
 	if (*ptr == INFO_FF || *ptr == INFO_COOKIE)
 	    found=1;
 
       /* if we didn't find the magic character, but hit eof, same deal */
-      if (!found && gzeof(f))
+      if (!found && readbuf_eof(f))
 	{
 	  found = 1;
 	  continue;
@@ -160,7 +159,7 @@
 
       memcpy(tmpcontents+nread, searchbuf, linelen);
       nread += linelen;
-      if (!gzeof(f) || num_files_left)
+      if (!readbuf_eof(f) || num_files_left)
 	*(tmpcontents+nread) = '\14';
     }
 
Index: libgnome/help-converters/info/parse.h
===================================================================
RCS file: /cvs/gnome/libgnome/help-converters/info/parse.h,v
retrieving revision 1.2
diff -u -r1.2 parse.h
--- libgnome/help-converters/info/parse.h	3 Dec 1999 17:02:49 -0000	1.2
+++ libgnome/help-converters/info/parse.h	25 Apr 2002 16:50:28 -0000
@@ -1,7 +1,6 @@
 #ifndef PARSE_H
 #define PARSE_H
 
-#include <zlib.h>
 #include "data.h"
 
 #define READ_OK    1
@@ -18,8 +17,8 @@
                      char **refnode, char **end_of_link,
 		     int span_lines);
 
-int read_node_contents( gzFile f, NODE *node );
-int read_node (gzFile f, char *line, NODE *node);
+int read_node_contents(ReadBuf *f, NODE *node );
+int read_node (ReadBuf *f, char *line, NODE *node);
 
 int is_a_hdr_line (char *r);
 extern int num_files_left;
Index: libgnome/help-converters/info/utils.c
===================================================================
RCS file: /cvs/gnome/libgnome/help-converters/info/utils.c,v
retrieving revision 1.4
diff -u -r1.4 utils.c
--- libgnome/help-converters/info/utils.c	2 May 2000 00:49:34 -0000	1.4
+++ libgnome/help-converters/info/utils.c	25 Apr 2002 16:50:28 -0000
@@ -3,9 +3,14 @@
 #include <config.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <regex.h>
+#include <zlib.h>
 
+#ifdef HAVE_LIBBZ2
+#include <bzlib.h>
+#endif
 #include <glib.h>
 
 #include "data.h"
@@ -198,7 +203,6 @@
       tmp->next=NULL;
       tmp->prev=NULL;
       tmp->up=NULL;
-      tmp->filename=NULL;
       tmp->menu=NULL;
       tmp->menu_start=NULL;
     }
@@ -272,4 +276,126 @@
     *ptr1 = '\000';
 
 }
-  
+
+ReadBuf * readbuf_open(const char *name) {
+  ReadBuf *f;
+
+#ifdef HAVE_LIBBZ2  
+  if (strlen(name) > 4 && strcmp(name+strlen(name)-4,".bz2") == 0) {
+    f = g_new0 (ReadBuf, 1);
+    f->buffer = g_malloc(READ_BUF_SIZE);
+    f->bzfile = bzopen(name, "r");
+    if(!f->bzfile) {
+      free(f);
+      return NULL;
+    }
+    
+    f->type = BZIP2;
+    f->eof = FALSE;
+  }
+  else 
+#endif
+    {
+      f = g_new0 (ReadBuf, 1);
+      f->buffer = g_malloc(READ_BUF_SIZE);
+      f->gzfile = gzopen(name, "r");     
+      if (f->gzfile == NULL) {
+	free(f);
+	return NULL;
+      }
+      else {
+	f->type = GZIP;
+	f->eof = 0;
+      }
+    }
+  return f;
+}
+
+void readbuf_close(ReadBuf *f) 
+{
+  switch (f->type) {
+  case GZIP:
+    gzclose(f->gzfile);
+    break;
+  case BZIP2:
+    bzclose(f->bzfile);
+    break;
+  }
+  g_free (f->buffer);
+  g_free (f);
+  }
+
+int readbuf_eof(ReadBuf *f)
+{
+  switch (f->type) {
+  case GZIP:
+    return gzeof(f->gzfile);
+  case BZIP2:
+    return f->eof;
+  default:
+    return -1;
+  }
+}
+
+static int
+readbuf_getc (ReadBuf *rb)
+{
+	if (rb->eof)
+		return EOF;
+
+	if (rb->size == 0 ||
+	    rb->pos == rb->size) {
+		int bytes_read;
+		
+		switch (rb->type) {
+		case GZIP:
+		  bytes_read = gzread(rb->gzfile,rb->buffer,READ_BUF_SIZE);
+		  break;
+		case BZIP2:
+		  bytes_read = bzread(rb->bzfile,rb->buffer,READ_BUF_SIZE);
+		  break;
+		}
+
+		if (bytes_read == 0) {
+			rb->eof = TRUE;
+			return EOF;
+		}
+
+		rb->size = bytes_read;
+		rb->pos = 0;
+
+	}
+
+	return (guchar) rb->buffer[rb->pos++];
+}
+
+char *
+readbuf_gets (ReadBuf *rb, char *buf, gsize bufsize)
+{
+	int c;
+	gsize pos;
+
+	g_return_val_if_fail (buf != NULL, NULL);
+	g_return_val_if_fail (rb != NULL, NULL);
+
+	pos = 0;
+	buf[0] = '\0';
+
+	do {
+		c = readbuf_getc (rb);
+		if (c == EOF || c == '\n')
+			break;
+		buf[pos++] = c;
+	} while (pos < bufsize-1);
+
+	if (c == EOF && pos == 0)
+		return NULL;
+
+	if (c == '\n')
+	  buf[pos++] = '\n';
+
+	buf[pos++] = '\0';
+
+	return buf;
+}
+
Index: libgnome/help-converters/info/utils.h
===================================================================
RCS file: /cvs/gnome/libgnome/help-converters/info/utils.h,v
retrieving revision 1.1
diff -u -r1.1 utils.h
--- libgnome/help-converters/info/utils.h	23 Feb 1998 19:01:36 -0000	1.1
+++ libgnome/help-converters/info/utils.h	25 Apr 2002 16:50:28 -0000
@@ -14,4 +14,8 @@
 void map_spaces_to_underscores( char *str );
 void fixup_info_filename( char *file );
 char *escape_html_chars( char *str );
+ReadBuf * readbuf_open (const char *name);
+int  readbuf_eof (ReadBuf *rb);
+char * readbuf_gets (ReadBuf *rb, char *buf, gsize bufsize);
+void readbuf_close (ReadBuf *rb);
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]