1 /***************************************
2 $Header$
3
4 File IO test program
5 ******************/ /******************
6 Written by Andrew M. Bishop
7
8 This file Copyright 2003 Andrew M. Bishop
9 It may be distributed under the GNU Public License, version 2, or
10 any higher version. See section COPYING of the GNU Public license
11 for conditions under which this file may be redistributed.
12 ***************************************/
13
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20
21 #include "autoconfig.h"
22 #include "io.h"
23 #include "errors.h"
24
25
26 /*+ Need this for Win32 to use binary mode +*/
27 #ifndef O_BINARY
28 #define O_BINARY 0
29 #endif
30
31
main(int argc,char ** argv)32 int main(int argc,char **argv)
33 {
34 char infile[32],outfile[32];
35 char buffer[IO_BUFFER_SIZE];
36 int n;
37 int count;
38
39 /* Writing test */
40
41 strcpy(infile,"file.txt");
42
43 for(count=0;count<4;count++)
44 {
45 int zlib,chunk;
46 int read_fd,write_fd;
47
48 zlib =count&1;
49 chunk=count&2;
50
51 strcpy(outfile,"write");
52 if(zlib) strcat(outfile,"-zlib");
53 if(chunk) strcat(outfile,"-chunk");
54 strcat(outfile,".txt");
55
56 printf("Writing");
57 if(zlib) printf(" with compression");
58 if(chunk) printf(" with chunked encoding");
59 printf(" %s -> %s\n",infile,outfile);
60
61 read_fd=open(infile,O_RDONLY|O_BINARY);
62 if(read_fd==-1)
63 PrintMessage(Fatal,"Cannot open '%s' for reading [%!s]",infile);
64
65 write_fd=open(outfile,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0644);
66 if(write_fd==-1)
67 PrintMessage(Fatal,"Cannot open '%s' for writing [%!s]",outfile);
68
69 init_io(write_fd);
70
71 #if USE_ZLIB
72 if(zlib)
73 configure_io_zlib(write_fd,-1,zlib?2:0);
74 #endif
75 if(chunk)
76 configure_io_chunked(write_fd,-1,chunk?1:0);
77
78 do
79 {
80 int size=1+rand()%128;
81
82 n=read(read_fd,buffer,size);
83 if(n>0)
84 write_data(write_fd,buffer,n);
85 }
86 while(n>0);
87
88 finish_io(write_fd);
89 close(write_fd);
90
91 close(read_fd);
92 }
93
94 /* Reading test */
95
96 for(count=0;count<4;count++)
97 {
98 int zlib,chunk;
99 int read_fd,write_fd;
100
101 zlib =count&1;
102 chunk=count&2;
103
104 strcpy(infile,"write");
105 if(zlib) strcat(infile,"-zlib");
106 if(chunk) strcat(infile,"-chunk");
107 strcat(infile,".txt");
108
109 strcpy(outfile,"read");
110 if(zlib) strcat(outfile,"-zlib");
111 if(chunk) strcat(outfile,"-chunk");
112 strcat(outfile,".txt");
113
114 printf("Reading");
115 if(zlib) printf(" with compression");
116 if(chunk) printf(" with chunked encoding");
117 printf(" %s -> %s\n",infile,outfile);
118
119 read_fd=open(infile,O_RDONLY|O_BINARY);
120 if(read_fd==-1)
121 PrintMessage(Fatal,"Cannot open '%s' for reading [%!s]",infile);
122
123 write_fd=open(outfile,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0644);
124 if(write_fd==-1)
125 PrintMessage(Fatal,"Cannot open '%s' for writing [%!s]",outfile);
126
127 init_io(read_fd);
128
129 #if USE_ZLIB
130 if(zlib)
131 configure_io_zlib(read_fd,zlib?2:0,-1);
132 #endif
133 if(chunk)
134 configure_io_chunked(read_fd,chunk?1:0,-1);
135
136 do
137 {
138 int size=1+rand()%128;
139
140 n=read_data(read_fd,buffer,size);
141 if(n>0)
142 write(write_fd,buffer,n);
143 }
144 while(n>0);
145
146 finish_io(read_fd);
147 close(read_fd);
148
149 close(write_fd);
150 }
151
152 /* Reading lines test */
153
154 for(count=0;count<4;count++)
155 {
156 int zlib,chunk,lineno;
157 int read_fd,write_fd;
158 char *line=NULL;
159
160 zlib =count&1;
161 chunk=count&2;
162
163 /* Create the files with headers */
164
165 strcpy(infile,"write");
166 if(zlib) strcat(infile,"-zlib");
167 if(chunk) strcat(infile,"-chunk");
168 strcat(infile,".txt");
169
170 strcpy(outfile,"line");
171 if(zlib) strcat(outfile,"-zlib");
172 if(chunk) strcat(outfile,"-chunk");
173 strcat(outfile,".txt");
174
175 read_fd=open(infile,O_RDONLY|O_BINARY);
176 if(read_fd==-1)
177 PrintMessage(Fatal,"Cannot open '%s' for reading [%!s]",infile);
178
179 write_fd=open(outfile,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0644);
180 if(write_fd==-1)
181 PrintMessage(Fatal,"Cannot open '%s' for writing [%!s]",outfile);
182
183 for(lineno=0;lineno<26;lineno++)
184 {
185 int size=1+rand()%256;
186
187 buffer[size+3]=0;
188 buffer[size+2]='\n';
189 buffer[size+1]='\r';
190 while(size>=0)
191 buffer[size--]='A'+lineno;
192
193 write(write_fd,buffer,strlen(buffer));
194 }
195
196 write(write_fd,"\r\n",2);
197
198 while((n=read(read_fd,buffer,1024))>0)
199 write(write_fd,buffer,n);
200
201 close(read_fd);
202 close(write_fd);
203
204 /* Read in the headers */
205
206 strcpy(infile,"line");
207 if(zlib) strcat(infile,"-zlib");
208 if(chunk) strcat(infile,"-chunk");
209 strcat(infile,".txt");
210
211 strcpy(outfile,"head");
212 if(zlib) strcat(outfile,"-zlib");
213 if(chunk) strcat(outfile,"-chunk");
214 strcat(outfile,".txt");
215
216 printf("Reading Lines (header)");
217 printf(" %s -> %s\n",infile,outfile);
218
219 read_fd=open(infile,O_RDONLY|O_BINARY);
220 if(read_fd==-1)
221 PrintMessage(Fatal,"Cannot open '%s' for reading [%!s]",infile);
222
223 write_fd=open(outfile,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0644);
224 if(write_fd==-1)
225 PrintMessage(Fatal,"Cannot open '%s' for writing [%!s]",outfile);
226
227 init_io(read_fd);
228
229 while((line=read_line(read_fd,line)))
230 {
231 write(write_fd,line,strlen(line));
232 if(*line=='\r' || *line=='\n')
233 break;
234 }
235
236 close(write_fd);
237
238 /* Read in the body */
239
240 strcpy(outfile,"body");
241 if(zlib) strcat(outfile,"-zlib");
242 if(chunk) strcat(outfile,"-chunk");
243 strcat(outfile,".txt");
244
245 printf("Reading Lines (body)");
246 if(zlib) printf(" with compression");
247 if(chunk) printf(" with chunked encoding");
248 printf(" %s -> %s\n",infile,outfile);
249
250 write_fd=open(outfile,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0644);
251 if(write_fd==-1)
252 PrintMessage(Fatal,"Cannot open '%s' for writing [%!s]",outfile);
253
254 #if USE_ZLIB
255 if(zlib)
256 configure_io_zlib(read_fd,zlib?2:0,-1);
257 #endif
258 if(chunk)
259 configure_io_chunked(read_fd,chunk?1:0,-1);
260
261 do
262 {
263 int size=1+rand()%1024;
264
265 n=read_data(read_fd,buffer,size);
266 if(n>0)
267 write(write_fd,buffer,n);
268 }
269 while(n>0);
270
271 finish_io(read_fd);
272 close(read_fd);
273
274 close(write_fd);
275 }
276
277 return(0);
278 }
279