File: getzlibsock.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 (243 lines) | stat: -rw-r--r-- 5,746 bytes parent folder | download | duplicates (2)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <R.h>
#include <Rdefines.h>
#include <Rinternals.h>
#ifndef WIN32
#ifdef _WIN32
#define WIN32 1
#endif
#endif
#ifndef WIN32
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>

void *prepare_sock_gz_r(int ncon );
char *z_read_sock(void *v);
int close_sock_gz_r(void *v);
static void *extract_opaque = NULL;

#define R_EOF	-1	
#define MAXESSAIS 2	/*#define MAXESSAIS 1000000*/

SEXP getzlibsock(SEXP sock, SEXP nmax, SEXP debug)
{

/* Variable de types SEXP :entree et sorties*/

  SEXP ans = R_NilValue;
  SEXP ans2 = R_NilValue;

/* Quelles variable faut il proteger : s'appliqe uniquement aux objets R, cad SEXP : ans et ans2*/  
  
  int nprotect = 0;
  int debugon;
  int testc;
  int numsoc;


  int i, n, nn, nnn, nread;
  int itest,itestd;
  
  char *res;
  
  int flagend =0;
  int nbseq =0;
  
  
  debugon = INTEGER_VALUE(debug);
  n=INTEGER_VALUE(nmax);
  if (debugon)
  	Rprintf("Running getzlibsock... \n");
		  
  if(!inherits(sock, "connection")) {
  	Rprintf("Error!\n\n'con' is not a connection");
	ans2 = PROTECT(allocVector(STRSXP, 1));
	nprotect ++;
	SET_STRING_ELT(ans2, 0,mkChar("Socket is not defined."));
	PROTECT(ans = ans2);
	nprotect ++;
    	UNPROTECT(nprotect);
	nprotect=0;
	return ans ;
	}
  if (debugon)
	Rprintf("'con' is a connection...\n");
  numsoc = asInteger(sock);
/* Pour  UNIX ( pbil):
  numsoc = asInteger(sock) + 1;	
  con=getConnection(numsoc);
  scon= (Rsockconn)con->private;
  numsoc = scon->fd;*/
  
  
  numsoc ++;
  
  if (debugon)
   	Rprintf("Socket number is %d....\n",numsoc);
  extract_opaque=prepare_sock_gz_r(numsoc);
  if (extract_opaque == NULL) {
	Rprintf("Erreur dans prepare_sock_gz_r\n");
	ans2 = PROTECT(allocVector(STRSXP, 1));
	nprotect ++;
	SET_STRING_ELT(ans2, 0,mkChar("Socket is not defined."));
	PROTECT(ans = ans2);
	nprotect ++;
    	UNPROTECT(nprotect);
	nprotect=0;
	return ans ;
	} 
	
   if (debugon)
   	Rprintf("Trying to get answer from socket...\n");
 
   res=z_read_sock(extract_opaque);
   
   /*AJOUT PATCHE CRADO ( devrait etre inutile)*/
   itest=0;
   itestd=0;
   while ( res == NULL){ 
   	res=z_read_sock(extract_opaque);
	itest++;
	itestd++;
	if (debugon){	
		if (itestd>10) {
			Rprintf("*");
			itestd=0;
			}
		}
			
	if (itest> MAXESSAIS) {
		Rprintf("Socket error!\n");
		Rprintf("No answer from socket after %d trials!\n",itest);
		ans2 = PROTECT(allocVector(STRSXP, 1));
		nprotect++;
		SET_STRING_ELT(ans2, 0,mkChar("No answer from socket."));
		PROTECT(ans = ans2);
		nprotect++;
    		UNPROTECT(nprotect);
		nprotect=0;
		testc=close_sock_gz_r(extract_opaque);
		if (debugon)
			Rprintf("Closing socket close_sock_gz_r  status = %d\n",testc);
		return ans ;
		}
	}

  if (debugon)
  	Rprintf("\n-->[%s]\n",res);
  if (strncmp(res,"code=0",6) != 0) {
  	Rprintf("extractseqs error!\n");
	Rprintf("[%s]\n",res);
	ans2 = PROTECT(allocVector(STRSXP, 1));
	nprotect++;
	SET_STRING_ELT(ans2, 0,mkChar("Wrong answer from socket."));
	PROTECT(ans = ans2);
	nprotect++;
    	UNPROTECT(nprotect);
	nprotect=0;
	testc=close_sock_gz_r(extract_opaque);
	if (debugon)
		Rprintf("Closing socket close_sock_gz_r  status = %d\n",testc);
	return ans ;
	}
  if (debugon)
  	Rprintf("Socket answer is ok %s(%d)\n",res, strlen(res));		 
  nn = (n < 0) ? 1000 : n; /* initially allocate space for 1000 lines */
  nnn = (n < 0) ? INT_MAX : n;
  PROTECT(ans = allocVector(STRSXP, nn));
  nprotect++;
  nread=0;
  if (debugon)
   	Rprintf("n=%d, nn=%d,nnn=%d\n",n,nn, nnn);
  res=z_read_sock(extract_opaque);	
  while ((res != NULL) ) {
  
  	if (nread >=nnn) {
	  	if (debugon)
			Rprintf("Increasing memory...\n");
	    	PROTECT(ans2 = (allocVector(STRSXP, 2*nn)));
		nprotect++;
	    	for(i = 0; i < nn; i++)
		SET_STRING_ELT(ans2, i, STRING_ELT(ans, i));
	    	nn *= 2;
	    	nnn=nn;
	    	UNPROTECT(nprotect); /* old ans et ans2 */
	    	PROTECT(ans = ans2);
		nprotect=1;
		};
	if  (strncmp(res,"extractseqs END.",16) == 0){
		if (debugon)
			Rprintf("extractseqs successfully ended ...\n");
		flagend=1;
		break;
		}
	if  ((strncmp(res,"code=0",6) == 0) && (nread >0)) {
		Rprintf("-->[%s]\n",res);
		Rprintf("WARNING!\nextractseqs unsuccessfully ended ...\n");
		flagend=1;
		break;
		}				
	 if  (strncmp(res,"\033count=", 7) == 0){
	 	nbseq++;
	 	} else {
		SET_STRING_ELT(ans, nread, mkChar(res));
		nread++;
		}
		
	res=z_read_sock(extract_opaque);
    	}
  if (debugon)
    Rprintf("Number of lines     : %d\n",nread-1);
  if (debugon) 
    Rprintf("Number of sequences : %d\n",nbseq);
  if (flagend) {
  	if (debugon)
  		Rprintf("extractseqs OK, program carry on...\n");
	if (debugon)
	 	Rprintf("Ok, everything is fine!\n");
	 } 
	 else{
	Rprintf("extractseqs error!\n");
	ans2 = PROTECT(allocVector(STRSXP, 1));
	nprotect++;
	SET_STRING_ELT(ans2, 0,mkChar("Wrong answer from socket."));
	PROTECT(ans = ans2);
	nprotect++;
	}
		
  testc=close_sock_gz_r(extract_opaque);
  if (debugon)
	Rprintf("Closing socket close_sock_gz_r  status = %d\n",testc);
  UNPROTECT(nprotect);
  return ans ;
}
#else
SEXP getzlibsock(SEXP sock, SEXP nmax, SEXP debug)
{

/* Variable de types SEXP :entree et sorties*/

  SEXP ans = R_NilValue;
  SEXP ans2 = R_NilValue;

/* Quelles variable faut il proteger : s'appliqe uniquement aux objets R, cad SEXP : ans et ans2*/  
  
  int nprotect = 0;
  int debugon;
  
  
  debugon = INTEGER_VALUE(debug);
  if (debugon)
  	Rprintf("Running getzlibsock... \n");	
  Rprintf("Warning!\n\nCompressed sockets are not yet available on Windows.\n");
  ans2 = PROTECT(allocVector(STRSXP, 1));
  nprotect ++;
  SET_STRING_ELT(ans2, 0,mkChar("Compressed sockets are not yet available on Windows."));  
  PROTECT(ans = ans2);
  nprotect ++;
  UNPROTECT(nprotect);
  nprotect=0;
  return ans ;
	}
#endif