Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / core_fstream.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: core_fstream.C /main/4 1996/10/04 10:47:25 drk $
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 void filebuf_exam_test1()
29 {
30    fprintf(stderr, "fstream test1 : read\n");
31    fstream fb("stream_test.read", ios::in|ios::out);
32    char buf[10];
33
34    for ( int i=0; i<2; i++ ) {
35       fb.read(buf, 4);
36       fprintf(stderr, "buf[0]=%c\n", buf[0]);
37       fprintf(stderr, "buf[1]=%c\n", buf[1]);
38       fprintf(stderr, "buf[2]=%c\n", buf[2]);
39       fprintf(stderr, "buf[3]=%c\n", buf[3]);
40
41       fprintf(stderr, "gcount=%d\n", fb.gcount());
42    }
43 }
44
45 void filebuf_exam_test2()
46 {
47    fprintf(stderr, "fstream test2 : write \n");
48    fstream fb("/tmp/stream_test.write", ios::in|ios::out|ios::trunc);
49    char* buf = "abcdefghijklmnopq";
50
51    for ( int i=0; i<2; i++ ) {
52       fb.write(buf+i*4, 4);
53    }
54
55    system("cmp stream_test.write /tmp/stream_test.write");
56    system("rm /tmp/stream_test.write");
57 }
58
59 void filebuf_exam_test3()
60 {
61    fprintf(stderr, "fstream test3 : getline\n");
62    fstream fb("stream_test.getline", ios::in|ios::out);
63    char buf[100];
64
65    while ( fb.getline(buf, 100) ) {
66       fprintf(stderr, "buf=%s\n", buf);
67       fprintf(stderr, "gcount=%d\n", fb.gcount());
68    }
69 }
70
71 void filebuf_exam_test4()
72 {
73    fprintf(stderr, "fstream test4 : word counting\n");
74    fstream fb("stream_test.mobydick", ios::in);
75
76    char buf[50];
77    char largest[50];
78    int curlen, max = -1, cnt = 0;
79    while ( fb >> buf ) {
80      curlen = strlen(buf);
81      ++cnt;
82      if ( curlen > max ) {
83        max = curlen;
84        memcpy(largest, buf, strlen(buf));
85      }
86    }
87    fprintf(stderr, "# of words read is = %d\n", cnt);
88    fprintf(stderr, "the longest word has a length of = %d\n", max);
89    fprintf(stderr, "the longest word is = %s\n", largest);
90 }
91
92 void filebuf_exam_test5()
93 {
94    fprintf(stderr, "fstream test 5: (extraction)\n");
95    fstream fb("stream_test.extraction", ios::in|ios::out);
96
97    int x;
98    long y;
99    char c;
100    unsigned short u;
101    unsigned int v;
102 // read 
103 // int long char (u)short (u)int
104
105    fb >> x; fprintf(stderr, "x=%d\n", x);
106    fprintf(stderr, "gcount=%d\n", fb.gcount());
107
108    fb >> y; fprintf(stderr, "y=%ld\n", y);
109    fprintf(stderr, "gcount=%d\n", fb.gcount());
110
111    fb >> c; fprintf(stderr, "c=%c\n", c);
112    fprintf(stderr, "gcount=%d\n", fb.gcount());
113
114    fb >> u; fprintf(stderr, "u=%d\n", u);
115    fprintf(stderr, "gcount=%d\n", fb.gcount());
116
117    fb >> v; fprintf(stderr, "v=%d\n", v);
118    fprintf(stderr, "gcount=%d\n", fb.gcount());
119
120 }
121
122 void filebuf_exam_test6()
123 {
124    fprintf(stderr, "fstream test 6: (mixed insertion and extraction [1]) \n");
125
126    system("cp stream_test.mixed_ins_extr /tmp/stream_test.mixed_ins_extr");
127
128    fstream fb("/tmp/stream_test.mixed_ins_extr", ios::in|ios::out);
129
130    int x;
131    long y;
132    char c;
133    unsigned short u;
134    unsigned int v;
135 // read
136 // int long char (u)short (u)int
137
138    fb >> x; fprintf(stderr, "x=%d\n", x);
139    fb >> y; fprintf(stderr, "y=%ld\n", y);
140
141    c = '0'; fb << c; 
142    c = '1'; fb << c; 
143
144    system("diff stream_test.mixed_ins_extr /tmp/stream_test.mixed_ins_extr");
145 }
146
147 void filebuf_exam_test7()
148 {
149    fprintf(stderr, "fstream test 7: (mixed insertion and extraction [2]) \n");
150
151    system("cp stream_test.mixed_ins_extr.2 /tmp/stream_test.mixed_ins_extr.2");
152    fstream fb("/tmp/stream_test.mixed_ins_extr.2", ios::in|ios::out|ios::trunc);
153
154    int x = 1;
155    long y = 2;
156    char c;
157
158    fb << x << y << "abcdefghijk";
159
160    fb.seekg(9, ios::beg);
161
162    fb >> c; fprintf(stderr, "c=%c\n", c);
163    fb >> c; fprintf(stderr, "c=%c\n", c);
164
165    system("diff /tmp/stream_test.mixed_ins_extr.2 stream_test.mixed_ins_extr.2");
166    system("rm /tmp/stream_test.mixed_ins_extr.2");
167 }
168
169 void filebuf_exam_test8()
170 {
171    fprintf(stderr, "fstream test 8: (seek and read) \n");
172
173    fstream fb("stream_test.seek_and_read", ios::in|ios::out);
174
175    fb.seekg(10, ios::beg);
176
177    char c;
178    fb >> c; fprintf(stderr, "c=%c\n", c);
179    fb >> c; fprintf(stderr, "c=%c\n", c);
180
181    fb.seekg(20, ios::beg);
182    fb >> c; fprintf(stderr, "c=%c\n", c);
183    fb >> c; fprintf(stderr, "c=%c\n", c);
184 }
185
186 void filebuf_exam_test9()
187 {
188    fprintf(stderr, "fstream test 9: (append) \n");
189
190    system("rm -f /tmp/stream_test.append");
191    fstream fb("/tmp/stream_test.append", ios::app);
192
193    fb << "lseek() sets the seek"; 
194    fb << " pointer associated with the open"; 
195
196    system("diff /tmp/stream_test.append stream_test.append");
197    system("rm /tmp/stream_test.append");
198 }
199
200 void filebuf_exam_test10()
201 {
202    fprintf(stderr, "fstream test 10: (cerr) \n");
203
204    fstream fb(2);
205
206    fb << "cerr: lseek() sets the seek\n"; 
207    fb << "cerr:  pointer associated with the open\n"; 
208
209    fstream fb1(1);
210    fb1 << "cout::lseek() sets the seek\n"; 
211    fb1 << "cout:: pointer associated with the open\n"; 
212 }
213
214 void filebuf_exam_test11(char* nm)
215 {
216    fprintf(stderr, "fstream test11 : getline (2)\n");
217    fstream fb(nm, ios::in);
218    char buf[100];
219
220    while ( fb.getline(buf, 100) ) {
221       fprintf(stderr, "buf=%s\n", buf);
222       fprintf(stderr, "gcount=%d\n", fb.gcount());
223    }
224 }
225
226 void filebuf_exam_test12()
227 {
228    fprintf(stderr, "fstream test12 : write (2)\n");
229    fstream fb("/tmp/stream_test.write", ios::out);
230    char* buf = "abcdefghijklmnopq";
231
232    for ( int i=0; i<2; i++ ) {
233       fb.write(buf+i*4, 4);
234    }
235    system("diff stream_test.write /tmp/stream_test.write");
236    system("rm /tmp/stream_test.write");
237 }
238
239 void filebuf_exam_test13(char* nm, int pos)
240 {
241    fprintf(stderr, "fstream test13 : read (2)\n");
242    fstream fb(nm, ios::in);
243    char buf[8192];
244
245    fb.seekg(pos, ios::beg);
246
247    int len = 8192;
248    fb.read(buf, len);
249
250    fprintf(stderr, "len=%d\n", len);
251    fprintf(stderr, "gcount=%d\n", fb.gcount());
252 }
253
254 void filebuf_exam_test14(char* nm)
255 {
256    fprintf(stderr, "fstream test14 : write (3)\n");
257    fstream fb(nm, ios::out);
258    char* buf = "abcdefghijklmnopq";
259
260    for ( int i=0; i<2; i++ ) {
261       fb.write(buf+i*4, 4);
262    }
263 }
264
265
266 void usage(char** argv)
267 {
268       fprintf(stderr, "Usage: %s option\n", argv[0]);
269       fprintf(stderr, "option = 1: read\n");
270       fprintf(stderr, "         2: write\n");
271       fprintf(stderr, "         3: getline\n");
272       fprintf(stderr, "         4: word counting\n");
273       fprintf(stderr, "         5: extraction\n");
274       fprintf(stderr, "         6: mixed insertion/extraction [1]\n");
275       fprintf(stderr, "         7: mixed insertion/extraction [2]\n");
276       fprintf(stderr, "         8: seek and read\n");
277       fprintf(stderr, "         9: append\n");
278       fprintf(stderr, "         10: cerr\n");
279       fprintf(stderr, "         11: getline (2). arguments: 11 filename\n");
280       fprintf(stderr, "         12: write (2)\n");
281       fprintf(stderr, "         13: read (2). arg: 13 filename offset\n");
282       fprintf(stderr, "         14: write (3). arg: 14 filename\n");
283 }
284
285 int main(int argc, char** argv)
286 {
287    if ( argc <= 1 ) {
288       usage(argv);
289       return 1;
290    }
291
292    int i = atoi(argv[1]);
293    switch (i) {
294      case 1:
295       filebuf_exam_test1();
296       break;
297      case 2:
298       filebuf_exam_test2();
299       break;
300      case 3:
301       filebuf_exam_test3();
302       break;
303      case 4:
304       filebuf_exam_test4();
305       break;
306      case 5:
307       filebuf_exam_test5();
308       break;
309      case 6:
310       filebuf_exam_test6();
311       break;
312      case 7:
313       filebuf_exam_test7();
314       break;
315      case 8:
316       filebuf_exam_test8();
317       break;
318      case 9:
319       filebuf_exam_test9();
320       break;
321      case 10:
322       filebuf_exam_test10();
323       break;
324      case 11:
325       if ( argc == 3 )
326          filebuf_exam_test11(argv[2]);
327       else
328          usage(argv);
329       break;
330      case 12:
331       filebuf_exam_test12();
332       break;
333      case 13:
334       if ( argc == 4 )
335          filebuf_exam_test13(argv[2], atoi(argv[3]));
336       break;
337      case 14:
338       if ( argc == 3 )
339          filebuf_exam_test14(argv[2]);
340       break;
341      default:
342       filebuf_exam_test1();
343       filebuf_exam_test2();
344       filebuf_exam_test3();
345       filebuf_exam_test4();
346       filebuf_exam_test5();
347       filebuf_exam_test6();
348       filebuf_exam_test7();
349       filebuf_exam_test8();
350       filebuf_exam_test9();
351       filebuf_exam_test10();
352       filebuf_exam_test12();
353    }
354    return 0;
355 }