File: zsockr.c

package info (click to toggle)
r-cran-seqinr 3.4-5-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,876 kB
  • sloc: ansic: 1,987; makefile: 14
file content (169 lines) | stat: -rw-r--r-- 2,869 bytes parent folder | download | duplicates (4)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* functions to handle zlib-compressed data read from socket
*/
#ifndef WIN32
#ifdef _WIN32
#define WIN32 1
#endif
#endif
#ifndef WIN32
#include "zlib.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <winsock.h>
#else
#include <sys/select.h>
#endif


/* included functions */
void *prepare_sock_gz_r(int sockr);
int z_getc_R(void *v);
char *z_gets(void *v, char *line, size_t len);
char *z_read_sock(void *v);
int close_sock_gz_r(void *v);



#define ZBSIZE 100000
typedef struct {
	z_stream stream;
	char z_buffer[ZBSIZE]; /* compressed input buffer */
	char text_buffer[4 * ZBSIZE]; /* decompressed buffer */
	char *pos, *endbuf;
#ifdef WIN32
	SOCKET fd;
#else
	int fd;
#endif
	} sock_gz_r;



void *prepare_sock_gz_r(int sockr)
{
int err;
sock_gz_r *big;
static sock_gz_r s_big;

big = &s_big;
if(big == NULL) return NULL;
big->stream.next_in = Z_NULL;
big->stream.avail_in = 0;
big->stream.avail_out = 0;
big->stream.zalloc = Z_NULL;
big->stream.zfree = Z_NULL;
big->stream.opaque = NULL;
big->pos = big->text_buffer;
big->endbuf = big->pos;
#ifdef WIN32
big->fd = (SOCKET)sockr;
#else
big->fd = sockr;
#endif
err = inflateInit(&big->stream);
return err == Z_OK ? (void *)big : NULL;
}


int z_getc_R(void *v)
{
int q, lu;
sock_gz_r *big = (sock_gz_r *)v;
z_streamp zs;
#ifndef WIN32
int err;
fd_set readfds;
#endif

if(big->pos < big->endbuf) {
	return *(big->pos++);
	}
zs = &(big->stream);
zs->next_out = (Bytef *)big->text_buffer;
zs->avail_out = sizeof(big->text_buffer);
big->pos = (char *)zs->next_out;
do	{
	if(zs->avail_in == 0) {
#ifdef WIN32
		do
		lu = recv( big->fd , big->z_buffer, ZBSIZE, 0 );
		while (lu <=0);
#else
		FD_ZERO(&readfds);
		FD_SET(big->fd, &readfds);
		err = select(big->fd + 1, &readfds, NULL, NULL, NULL);
		if(err > 0 ) {
			lu = read( big->fd , big->z_buffer, ZBSIZE );
			}
		else lu = -1;
#endif
		if(lu == -1) return EOF;
		zs->next_in = (Bytef *)big->z_buffer;
		zs->avail_in = lu;
		}
	q = inflate(zs, Z_NO_FLUSH);	
	if(q == Z_STREAM_END) break;	
	if(q != Z_OK) {
		break;
		}
	}
while ( (char *)zs->next_out == big->pos);
big->endbuf = (char *)zs->next_out;
if(big->pos < big->endbuf) return *(big->pos++);
else 
	return EOF;
}


char *z_gets(void *v, char *line, size_t len)
{
int c;
char *p;

p = line;
while(len > 1) {
	c = z_getc_R( v );
	if(c == EOF) {
		if(p == line) return NULL;
		break;
		}
	*(p++) = c;
	if(c == '\n') break;
	len--;
	}
*p = 0;
return line;
}


char *z_read_sock(void *v)
{
static char line[500];
char *p;
int l;

p = z_gets(v, line, sizeof(line));
if(p == NULL) return NULL;
l = strlen(line);
if(l > 0 && line[l-1] == '\n') line[l-1] = 0;
return line;
}


int close_sock_gz_r(void *v)
{
sock_gz_r *big = (sock_gz_r *)v;
int val;

val = inflateEnd(&(big->stream));
return val;
}
#else
void *prepare_sock_gz_r(int sockr) {
return 0;
}
#endif