Re: info2html fixed



Le mar 30/04/2002 à 15:57, Mikael Hallendal a écrit :
> tis 2002-04-30 klockan 15.52 skrev Frederic Crozat:
> > Hi,
> > 
> > I've finally fixed the bzip2 info file support in gnome-info2html 
> > in a not too ugly way..
> 
> That entire code is ugly anyway :

:))
 
> I wouldn't know if this patch is good or not, just that I noticed you
> did a g_print ("out") somewhere, it's bad to write things on standard
> out since that gets outputted in the view.
> 
> > +		  if(!f) {
> > +		    g_print("out\n");
> >  		    break;
> > +		  }

Oops, sorry, I forgot to remove some debug stuff..

I also fixed the eof handling to be consistent between bzip2 and gzip;
and missing #ifdef when libbz2 is not installed on the system..

New patch attached..

> > (and, no, I don't want to become maintainer of the helper converters..
> > :)
> 
> Of course you do, GO GO GO GO !! :9

Noooooooooooooooooooooo

/me runs..

-- 
Frédéric Crozat
MandrakeSoft
--- libgnome-1.116.0/help-converters/info/utils.c.bzipped	Tue May  2 02:49:34 2000
+++ libgnome-1.116.0/help-converters/info/utils.c	Tue Apr 30 16:21:27 2002
@@ -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,123 @@
     *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 = FALSE;
+      }
+    }
+  return f;
+}
+
+void readbuf_close(ReadBuf *f) 
+{
+  switch (f->type) {
+  case GZIP:
+    gzclose(f->gzfile);
+    break;
+#ifdef HAVE_LIBBZ2
+  case BZIP2:
+    bzclose(f->bzfile);
+    break;
+#endif
+  }
+  g_free (f->buffer);
+  g_free (f);
+  }
+
+int readbuf_eof(ReadBuf *f)
+{
+  return f->eof;
+}
+
+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;
+#ifdef HAVE_LIBBZ2
+		case BZIP2:
+		  bytes_read = bzread(rb->bzfile,rb->buffer,READ_BUF_SIZE);
+		  break;
+#endif
+		}
+
+		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;
+}
+
--- libgnome-1.116.0/help-converters/info/html.c.bzipped	Sun Apr  7 20:10:02 2002
+++ libgnome-1.116.0/help-converters/info/html.c	Tue Apr 30 16:13:53 2002
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <ctype.h>
 
 #include <glib.h>
 
--- libgnome-1.116.0/help-converters/info/main.c.bzipped	Thu Jan 10 01:00:20 2002
+++ libgnome-1.116.0/help-converters/info/main.c	Tue Apr 30 16:14:52 2002
@@ -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,31 @@
 	/* 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) {
 		    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();
--- libgnome-1.116.0/help-converters/info/parse.h.bzipped	Fri Dec  3 18:02:49 1999
+++ libgnome-1.116.0/help-converters/info/parse.h	Tue Apr 30 16:13:53 2002
@@ -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;
--- libgnome-1.116.0/help-converters/info/data.h.bzipped	Sun Mar  8 22:24:42 1998
+++ libgnome-1.116.0/help-converters/info/data.h	Tue Apr 30 16:13:53 2002
@@ -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'
--- libgnome-1.116.0/help-converters/info/parse.c.bzipped	Tue May  2 02:49:34 2000
+++ libgnome-1.116.0/help-converters/info/parse.c	Tue Apr 30 16:13:53 2002
@@ -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';
     }
 
--- libgnome-1.116.0/help-converters/info/utils.h.bzipped	Mon Feb 23 20:01:36 1998
+++ libgnome-1.116.0/help-converters/info/utils.h	Tue Apr 30 16:13:53 2002
@@ -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]