fix type in testcase too.
[citadel] / libcitadel / tests / stringbuf_stream.c
1 /*
2  *  CUnit - A Unit testing framework library for C.
3  *  Copyright (C) 2001  Anil Kumar
4  *  
5  *  This library is open source software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <sys/select.h>
20
21 #include <ctype.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <limits.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <sys/un.h>
34 #include <netdb.h>
35 #include <sys/poll.h>
36 #include <string.h>
37 #include <pwd.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <pthread.h>
42 #include <signal.h>
43 #include <sys/utsname.h>
44 #include <string.h>
45
46
47 typedef void testfunc(int Sock);
48
49 #include <string.h>
50
51 #include "../lib/libcitadel.h"
52
53
54 int DeZip = -1;
55 int DeBase64 = -1;
56
57 int msock;                      /* master listening socket */
58 int BindPort;
59 int time_to_die=0;
60 int n_Lines_to_read = 0;
61 int blobsize = 0;
62 int timeout = 5;
63 int selres = 1;
64 char ip_addr[256]="0.0.0.0";
65
66
67
68 static void StreamEncode(void)
69 {
70         IOBuffer ReadBuffer;
71         IOBuffer WriteBuffer;
72         int err;
73         const char *Err = NULL;
74         int ret = 0;
75         int done = 0;
76         vStreamT *vStream;
77         
78         memset(&ReadBuffer, 0, sizeof(IOBuffer));
79         memset(&WriteBuffer, 0, sizeof(IOBuffer));
80         ReadBuffer.fd = fileno(stdin);
81         WriteBuffer.fd = fileno(stdout);
82         ReadBuffer.Buf = NewStrBufPlain(NULL, SIZ*2);
83         WriteBuffer.Buf = NewStrBufPlain(NULL, SIZ*2);
84
85         int fdin = fileno(stdin);
86         int fdout = fileno(stdout);
87         eStreamType ST;
88
89                                 
90         if (DeZip == 0)
91                 ST = eZLibEncode;
92         else if (DeZip == 1)
93                 ST = eZLibDecode;
94         else if (DeBase64 == 0)
95                 ST = eBase64Encode;
96         else if (DeBase64 == 1)
97                 ST = eBase64Decode;
98         else
99                 ST = eEmtyCodec;
100         vStream = StrBufNewStreamContext(ST, &Err);
101
102         while (!done && (fdin >= 0) && (fdout >= 0) && (!feof(stdin)))
103         {
104
105                 done = StrBuf_read_one_chunk_callback(fdin,
106                                                       0,
107                                                       &ReadBuffer) < (SIZ * 4) -1 ;
108                 if (IOBufferStrLength(&ReadBuffer) == 0)
109                 {
110                         done = 1;
111                 }
112                 do
113                 {
114                         do {
115                                 ret = StrBufStreamTranscode(ST, &WriteBuffer, &ReadBuffer, NULL, -1, vStream, done, &Err);
116                                 
117                                 while (IOBufferStrLength(&WriteBuffer) > 0)
118                                 {
119                                         err = StrBuf_write_one_chunk_callback(fdout,
120                                                                               0,
121                                                                               &WriteBuffer);
122                                 }
123                         } while (ret > 0);
124                 } while (IOBufferStrLength(&ReadBuffer) > 0);
125         }
126
127         
128         StrBufDestroyStreamContext(ST, &vStream, &Err);
129         
130         FreeStrBuf(&ReadBuffer.Buf);
131         FreeStrBuf(&WriteBuffer.Buf);
132         time_to_die = 1;
133 }
134
135
136
137
138 int main(int argc, char* argv[])
139 {
140         char a;
141         setvbuf(stdout, NULL, _IONBF, 0);
142
143
144         while ((a = getopt(argc, argv, "z:b")) != EOF)
145         {
146                 switch (a) {
147
148                 case 'z':
149                         // zip...
150                         DeZip = *optarg == 'x';
151                         break;
152                 case 'b':
153                         // base64
154                         DeBase64 = *optarg == 'x';
155
156                 }
157         }
158
159
160         StartLibCitadel(8);
161
162         StreamEncode();
163         return 0;
164 }
165
166
167 /// run -z 1 < ~/testfile1.txt > /tmp/blarg.z