summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrédéric Crozat <fcrozat@src.gnome.org>2002-05-14 10:10:30 +0000
committerFrédéric Crozat <fcrozat@src.gnome.org>2002-05-14 10:10:30 +0000
commit744a4c63682f194db3c75aaa6e1f8e830df57f35 (patch)
tree4febcbfbef39f262d493a5a95d1712f214312778
parent7fb5fa6380876bcb252262eb35db507d79497660 (diff)
downloadyelp-744a4c63682f194db3c75aaa6e1f8e830df57f35.tar.gz
Fix bug #66334 (bzip2 info files)LIBGNOME_1_117_1
-rw-r--r--src/info2html/data.h22
-rw-r--r--src/info2html/html.c1
-rw-r--r--src/info2html/main.c78
-rw-r--r--src/info2html/parse.c11
-rw-r--r--src/info2html/parse.h5
-rw-r--r--src/info2html/utils.c127
-rw-r--r--src/info2html/utils.h4
7 files changed, 178 insertions, 70 deletions
diff --git a/src/info2html/data.h b/src/info2html/data.h
index 60a2ce74..61e17e0c 100644
--- a/src/info2html/data.h
+++ b/src/info2html/data.h
@@ -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 */
@@ -45,6 +51,22 @@ struct info_menu_entry{
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'
diff --git a/src/info2html/html.c b/src/info2html/html.c
index 7852bf19..86062088 100644
--- a/src/info2html/html.c
+++ b/src/info2html/html.c
@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
+#include <ctype.h>
#include <glib.h>
diff --git a/src/info2html/main.c b/src/info2html/main.c
index feb1d582..33310570 100644
--- a/src/info2html/main.c
+++ b/src/info2html/main.c
@@ -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 @@ file_exists(const char *fn)
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 @@ main(int argc, const char **argv)
ext = ".bz2";
sprintf(buf, "%s/%s.info.bz2", dirs[i], args[0]);
if(file_exists(buf)) {
- bz = 1;
break;
}
#endif
@@ -172,61 +162,31 @@ main(int argc, const char **argv)
/* 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();
diff --git a/src/info2html/parse.c b/src/info2html/parse.c
index 0177368b..d0fc595b 100644
--- a/src/info2html/parse.c
+++ b/src/info2html/parse.c
@@ -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 @@ int num_files_left;
/* 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 @@ char *parse_node_label( char **line, char *label, int allow_eof )
#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 @@ int read_node_contents( gzFile f, NODE *node )
/* 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 @@ int read_node_contents( gzFile f, NODE *node )
memcpy(tmpcontents+nread, searchbuf, linelen);
nread += linelen;
- if (!gzeof(f) || num_files_left)
+ if (!readbuf_eof(f) || num_files_left)
*(tmpcontents+nread) = '\14';
}
diff --git a/src/info2html/parse.h b/src/info2html/parse.h
index 9f539a3c..6ff84246 100644
--- a/src/info2html/parse.h
+++ b/src/info2html/parse.h
@@ -1,7 +1,6 @@
#ifndef PARSE_H
#define PARSE_H
-#include <zlib.h>
#include "data.h"
#define READ_OK 1
@@ -18,8 +17,8 @@ int parse_note_ref( char *line, char **refname, char **reffile,
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;
diff --git a/src/info2html/utils.c b/src/info2html/utils.c
index 5ab276a5..fcee4bf5 100644
--- a/src/info2html/utils.c
+++ b/src/info2html/utils.c
@@ -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 @@ NODE *alloc_node()
tmp->next=NULL;
tmp->prev=NULL;
tmp->up=NULL;
- tmp->filename=NULL;
tmp->menu=NULL;
tmp->menu_start=NULL;
}
@@ -272,4 +276,123 @@ void fixup_info_filename( char *file )
*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;
+}
+
diff --git a/src/info2html/utils.h b/src/info2html/utils.h
index 25a964c8..7f77aa4d 100644
--- a/src/info2html/utils.h
+++ b/src/info2html/utils.h
@@ -14,4 +14,8 @@ void free_node( NODE * );
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