File: gzbuffered.h

package info (click to toggle)
hspell 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,556 kB
  • sloc: ansic: 2,808; perl: 1,989; makefile: 209; sh: 109; awk: 15
file content (117 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright (C) 2004 Nadav Har'El and Dan Kenigsberg */

/* The implementation of gzgetc() in the Zlib library, which gets the next
   uncompressed character when reading a gzip file, is extremely slow.
   When we tried using gzgetc() to read the gzipped dictionary file one
   character at a time, the result was 7 times slower start-up than when
   we read from a pipe to the "gzcat" program.

   It turns out that this can easily be solved, by buffering the reads:
   we can read, using gzread() a whole chunk (say, of 4 Kbytes) of
   uncompressed characters, and then dispense them one character at a time,
   much like the stdio library's getc() buffers calls to read().

   This implementation provides a new type, "gzbFile *", routines to open
   and close such a file, gzb_open, gzb_dopen (uses an already open file
   descriptor) and gzb_close, and most importantly, a gzb_getc() routine
   from getting, in a buffered manner, the next uncompressed character
   from the file.

   The semantics implemented is "close enough" to that of zlib to fit
   our needs, but not identical. Also, many other facilities offered
   by zlib and stdio are not given a buffered version here because Hspell
   doesn't need them. Such facilities, like ungetc, scanf, tell/seek,
   and of course writing, can be implemented in the future if needed.
*/

#define GZBUFFERED_SIZE 4096 /* empirical testing showed this to be fine */

#include <stdlib.h>
#include <zlib.h>

typedef struct {
        gzFile gz;
        char buf[GZBUFFERED_SIZE]; /* buffer of preread characters */
        char *b; /* next character to read from b */
        int n; /* number of character left to read in buffer */
} gzbFile;

static inline gzbFile *
gzb_open(const char *path, const char *mode)
{
	gzbFile *ret = (gzbFile *)malloc(sizeof(gzbFile));
	if(!ret)
		return NULL;
	ret->n = 0;
	ret->gz = gzopen(path,mode);
	if(!ret->gz){
		free(ret);
		return NULL;
	}

	return ret;
}

static inline gzbFile *
gzb_dopen(int fd, const char *mode)
{
	gzbFile *ret = (gzbFile *)malloc(sizeof(gzbFile));
	if(!ret)
		return NULL;
	ret->n = 0;
	ret->gz = gzdopen(fd,mode);
	if(!ret->gz){
		free(ret);
		return NULL;
	}
	return ret;
}

static inline int
gzb_close(gzbFile *f)
{
	int ret;
	ret=gzclose(f->gz);
	free(f);
	return ret;
}

static inline int
gzb_getc(gzbFile *gzbp){
	if(!gzbp->n){
		/* No more characters buffered. Refill buffer with gzread() */
		gzbp->n = gzread(gzbp->gz, gzbp->buf, sizeof(gzbp->buf));
		if(gzbp->n <= 0){
			gzbp->n=0;
			return EOF;
		}
		gzbp->b=gzbp->buf;
	}
	/* Return the next available character in the buffer */
	gzbp->n--;
	return *(gzbp->b++);
}

/* We need an implementation of this function for use in linginfo.c
   (which used fgets). This might not be the most efficient implementation -
   we could have browsed the buffer directly, rather than calling gzb_getc
   per character. But I think this will be quick enough.
*/
static inline char *
gzb_gets(char *s, int size, gzbFile *stream)
{
	int c;
	char *ret=s;
	while(--size){ /* stop after at most size-1 characters */
		c=gzb_getc(stream);
		if(c==EOF)
			break;
		else {
			*(s++)=c;
			if(c=='\n')
				break;
		}
	}
	*s='\0';
	return s==ret ? NULL : ret;
}