core_fstream.C 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 libraries 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. #include <stdlib.h>
  25. #include <string.h>
  26. void filebuf_exam_test1()
  27. {
  28. fprintf(stderr, "fstream test1 : read\n");
  29. fstream fb("stream_test.read", ios::in|ios::out);
  30. char buf[10];
  31. for ( int i=0; i<2; i++ ) {
  32. fb.read(buf, 4);
  33. fprintf(stderr, "buf[0]=%c\n", buf[0]);
  34. fprintf(stderr, "buf[1]=%c\n", buf[1]);
  35. fprintf(stderr, "buf[2]=%c\n", buf[2]);
  36. fprintf(stderr, "buf[3]=%c\n", buf[3]);
  37. fprintf(stderr, "gcount=%d\n", fb.gcount());
  38. }
  39. }
  40. void filebuf_exam_test2()
  41. {
  42. fprintf(stderr, "fstream test2 : write \n");
  43. fstream fb("/tmp/stream_test.write", ios::in|ios::out|ios::trunc);
  44. char* buf = "abcdefghijklmnopq";
  45. for ( int i=0; i<2; i++ ) {
  46. fb.write(buf+i*4, 4);
  47. }
  48. system("cmp stream_test.write /tmp/stream_test.write");
  49. system("rm /tmp/stream_test.write");
  50. }
  51. void filebuf_exam_test3()
  52. {
  53. fprintf(stderr, "fstream test3 : getline\n");
  54. fstream fb("stream_test.getline", ios::in|ios::out);
  55. char buf[100];
  56. while ( fb.getline(buf, 100) ) {
  57. fprintf(stderr, "buf=%s\n", buf);
  58. fprintf(stderr, "gcount=%d\n", fb.gcount());
  59. }
  60. }
  61. void filebuf_exam_test4()
  62. {
  63. fprintf(stderr, "fstream test4 : word counting\n");
  64. fstream fb("stream_test.mobydick", ios::in);
  65. char buf[50];
  66. char largest[50];
  67. int curlen, max = -1, cnt = 0;
  68. while ( fb >> buf ) {
  69. curlen = strlen(buf);
  70. ++cnt;
  71. if ( curlen > max ) {
  72. max = curlen;
  73. memcpy(largest, buf, strlen(buf));
  74. }
  75. }
  76. fprintf(stderr, "# of words read is = %d\n", cnt);
  77. fprintf(stderr, "the longest word has a length of = %d\n", max);
  78. fprintf(stderr, "the longest word is = %s\n", largest);
  79. }
  80. void filebuf_exam_test5()
  81. {
  82. fprintf(stderr, "fstream test 5: (extraction)\n");
  83. fstream fb("stream_test.extraction", ios::in|ios::out);
  84. int x;
  85. long y;
  86. char c;
  87. unsigned short u;
  88. unsigned int v;
  89. // read
  90. // int long char (u)short (u)int
  91. fb >> x; fprintf(stderr, "x=%d\n", x);
  92. fprintf(stderr, "gcount=%d\n", fb.gcount());
  93. fb >> y; fprintf(stderr, "y=%ld\n", y);
  94. fprintf(stderr, "gcount=%d\n", fb.gcount());
  95. fb >> c; fprintf(stderr, "c=%c\n", c);
  96. fprintf(stderr, "gcount=%d\n", fb.gcount());
  97. fb >> u; fprintf(stderr, "u=%d\n", u);
  98. fprintf(stderr, "gcount=%d\n", fb.gcount());
  99. fb >> v; fprintf(stderr, "v=%d\n", v);
  100. fprintf(stderr, "gcount=%d\n", fb.gcount());
  101. }
  102. void filebuf_exam_test6()
  103. {
  104. fprintf(stderr, "fstream test 6: (mixed insertion and extraction [1]) \n");
  105. system("cp stream_test.mixed_ins_extr /tmp/stream_test.mixed_ins_extr");
  106. fstream fb("/tmp/stream_test.mixed_ins_extr", ios::in|ios::out);
  107. int x;
  108. long y;
  109. char c;
  110. unsigned short u;
  111. unsigned int v;
  112. // read
  113. // int long char (u)short (u)int
  114. fb >> x; fprintf(stderr, "x=%d\n", x);
  115. fb >> y; fprintf(stderr, "y=%ld\n", y);
  116. c = '0'; fb << c;
  117. c = '1'; fb << c;
  118. system("diff stream_test.mixed_ins_extr /tmp/stream_test.mixed_ins_extr");
  119. }
  120. void filebuf_exam_test7()
  121. {
  122. fprintf(stderr, "fstream test 7: (mixed insertion and extraction [2]) \n");
  123. system("cp stream_test.mixed_ins_extr.2 /tmp/stream_test.mixed_ins_extr.2");
  124. fstream fb("/tmp/stream_test.mixed_ins_extr.2", ios::in|ios::out|ios::trunc);
  125. int x = 1;
  126. long y = 2;
  127. char c;
  128. fb << x << y << "abcdefghijk";
  129. fb.seekg(9, ios::beg);
  130. fb >> c; fprintf(stderr, "c=%c\n", c);
  131. fb >> c; fprintf(stderr, "c=%c\n", c);
  132. system("diff /tmp/stream_test.mixed_ins_extr.2 stream_test.mixed_ins_extr.2");
  133. system("rm /tmp/stream_test.mixed_ins_extr.2");
  134. }
  135. void filebuf_exam_test8()
  136. {
  137. fprintf(stderr, "fstream test 8: (seek and read) \n");
  138. fstream fb("stream_test.seek_and_read", ios::in|ios::out);
  139. fb.seekg(10, ios::beg);
  140. char c;
  141. fb >> c; fprintf(stderr, "c=%c\n", c);
  142. fb >> c; fprintf(stderr, "c=%c\n", c);
  143. fb.seekg(20, ios::beg);
  144. fb >> c; fprintf(stderr, "c=%c\n", c);
  145. fb >> c; fprintf(stderr, "c=%c\n", c);
  146. }
  147. void filebuf_exam_test9()
  148. {
  149. fprintf(stderr, "fstream test 9: (append) \n");
  150. system("rm -f /tmp/stream_test.append");
  151. fstream fb("/tmp/stream_test.append", ios::app);
  152. fb << "lseek() sets the seek";
  153. fb << " pointer associated with the open";
  154. system("diff /tmp/stream_test.append stream_test.append");
  155. system("rm /tmp/stream_test.append");
  156. }
  157. void filebuf_exam_test10()
  158. {
  159. fprintf(stderr, "fstream test 10: (cerr) \n");
  160. fstream fb(2);
  161. fb << "cerr: lseek() sets the seek\n";
  162. fb << "cerr: pointer associated with the open\n";
  163. fstream fb1(1);
  164. fb1 << "cout::lseek() sets the seek\n";
  165. fb1 << "cout:: pointer associated with the open\n";
  166. }
  167. void filebuf_exam_test11(char* nm)
  168. {
  169. fprintf(stderr, "fstream test11 : getline (2)\n");
  170. fstream fb(nm, ios::in);
  171. char buf[100];
  172. while ( fb.getline(buf, 100) ) {
  173. fprintf(stderr, "buf=%s\n", buf);
  174. fprintf(stderr, "gcount=%d\n", fb.gcount());
  175. }
  176. }
  177. void filebuf_exam_test12()
  178. {
  179. fprintf(stderr, "fstream test12 : write (2)\n");
  180. fstream fb("/tmp/stream_test.write", ios::out);
  181. char* buf = "abcdefghijklmnopq";
  182. for ( int i=0; i<2; i++ ) {
  183. fb.write(buf+i*4, 4);
  184. }
  185. system("diff stream_test.write /tmp/stream_test.write");
  186. system("rm /tmp/stream_test.write");
  187. }
  188. void filebuf_exam_test13(char* nm, int pos)
  189. {
  190. fprintf(stderr, "fstream test13 : read (2)\n");
  191. fstream fb(nm, ios::in);
  192. char buf[8192];
  193. fb.seekg(pos, ios::beg);
  194. int len = 8192;
  195. fb.read(buf, len);
  196. fprintf(stderr, "len=%d\n", len);
  197. fprintf(stderr, "gcount=%d\n", fb.gcount());
  198. }
  199. void filebuf_exam_test14(char* nm)
  200. {
  201. fprintf(stderr, "fstream test14 : write (3)\n");
  202. fstream fb(nm, ios::out);
  203. char* buf = "abcdefghijklmnopq";
  204. for ( int i=0; i<2; i++ ) {
  205. fb.write(buf+i*4, 4);
  206. }
  207. }
  208. void usage(char** argv)
  209. {
  210. fprintf(stderr, "Usage: %s option\n", argv[0]);
  211. fprintf(stderr, "option = 1: read\n");
  212. fprintf(stderr, " 2: write\n");
  213. fprintf(stderr, " 3: getline\n");
  214. fprintf(stderr, " 4: word counting\n");
  215. fprintf(stderr, " 5: extraction\n");
  216. fprintf(stderr, " 6: mixed insertion/extraction [1]\n");
  217. fprintf(stderr, " 7: mixed insertion/extraction [2]\n");
  218. fprintf(stderr, " 8: seek and read\n");
  219. fprintf(stderr, " 9: append\n");
  220. fprintf(stderr, " 10: cerr\n");
  221. fprintf(stderr, " 11: getline (2). arguments: 11 filename\n");
  222. fprintf(stderr, " 12: write (2)\n");
  223. fprintf(stderr, " 13: read (2). arg: 13 filename offset\n");
  224. fprintf(stderr, " 14: write (3). arg: 14 filename\n");
  225. }
  226. int main(int argc, char** argv)
  227. {
  228. if ( argc <= 1 ) {
  229. usage(argv);
  230. return 1;
  231. }
  232. int i = atoi(argv[1]);
  233. switch (i) {
  234. case 1:
  235. filebuf_exam_test1();
  236. break;
  237. case 2:
  238. filebuf_exam_test2();
  239. break;
  240. case 3:
  241. filebuf_exam_test3();
  242. break;
  243. case 4:
  244. filebuf_exam_test4();
  245. break;
  246. case 5:
  247. filebuf_exam_test5();
  248. break;
  249. case 6:
  250. filebuf_exam_test6();
  251. break;
  252. case 7:
  253. filebuf_exam_test7();
  254. break;
  255. case 8:
  256. filebuf_exam_test8();
  257. break;
  258. case 9:
  259. filebuf_exam_test9();
  260. break;
  261. case 10:
  262. filebuf_exam_test10();
  263. break;
  264. case 11:
  265. if ( argc == 3 )
  266. filebuf_exam_test11(argv[2]);
  267. else
  268. usage(argv);
  269. break;
  270. case 12:
  271. filebuf_exam_test12();
  272. break;
  273. case 13:
  274. if ( argc == 4 )
  275. filebuf_exam_test13(argv[2], atoi(argv[3]));
  276. break;
  277. case 14:
  278. if ( argc == 3 )
  279. filebuf_exam_test14(argv[2]);
  280. break;
  281. default:
  282. filebuf_exam_test1();
  283. filebuf_exam_test2();
  284. filebuf_exam_test3();
  285. filebuf_exam_test4();
  286. filebuf_exam_test5();
  287. filebuf_exam_test6();
  288. filebuf_exam_test7();
  289. filebuf_exam_test8();
  290. filebuf_exam_test9();
  291. filebuf_exam_test10();
  292. filebuf_exam_test12();
  293. }
  294. return 0;
  295. }