1 /* $Id: bstream.cc,v 1.2 2004/12/28 00:23:14 atterer Exp $ -*- C++ -*-
2 __ _
3 |_) /| Copyright (C) 2001-2004 | richard@
4 | \/�| Richard Atterer | atterer.net
5 � '` �
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2. See
8 the file COPYING for details.
9
10 I/O streams for bytes (byte is unsigned char, not regular char)
11
12 */
13
14 #include <config.h>
15
16 #include <bstream.hh>
17
18 #include <stdio.h>
19
20 #if !HAVE_WORKING_FSTREAM
21
22 bistream bcin(stdin);
23 bostream bcout(stdout);
24
seekp(off_t off,ios::seekdir dir)25 bostream& bostream::seekp(off_t off, ios::seekdir dir) {
26 if (fail()) return *this;
27 int whence;
28 if (dir == ios::beg)
29 whence = SEEK_SET;
30 else if (dir == ios::end)
31 whence = SEEK_END;
32 else if (dir == ios::cur)
33 whence = SEEK_CUR;
34 else
35 { Assert(false); whence = SEEK_SET; }
36 /*int r =*/ fseeko(f, off, whence);
37 // Fails: Assert((r == -1) == (ferror(f) != 0));
38 return *this;
39 }
40
seekg(off_t off,ios::seekdir dir)41 bistream& bistream::seekg(off_t off, ios::seekdir dir) {
42 if (fail()) return *this;
43 int whence;
44 if (dir == ios::beg)
45 whence = SEEK_SET;
46 else if (dir == ios::end)
47 whence = SEEK_END;
48 else if (dir == ios::cur)
49 whence = SEEK_CUR;
50 else
51 { Assert(false); whence = SEEK_SET; }
52 /*int r =*/ fseeko(f, off, whence);
53 // Fails: Assert((r == -1) == (ferror(f) != 0));
54 return *this;
55 }
56
getline(string & l)57 void bistream::getline(string& l) {
58 gcountVal = 0;
59 l.clear();
60 while (true) {
61 int c = fgetc(f);
62 if (c >= 0) ++gcountVal;
63 if (c == EOF || c == '\n') break;
64 l += static_cast<char>(c);
65 }
66 }
67
open(const char * name,ios::openmode m)68 void bofstream::open(const char* name, ios::openmode m) {
69 Paranoid((m & ios::binary) != 0 && f == 0);
70 Paranoid((m & ios::ate) == 0);
71 if ((m & ios::trunc) != 0) {
72 f = fopen(name, "w+b");
73 return;
74 }
75 // Open existing file for reading and writing
76 f = fopen(name, "r+b");
77 if (f == NULL && errno == ENOENT)
78 f = fopen(name, "w+b"); // ...or create new, empty file
79 }
80
bfstream(const char * name,ios::openmode m)81 bfstream::bfstream(const char* name, ios::openmode m) : biostream() {
82 Paranoid((m & ios::binary) != 0);
83 Paranoid((m & ios::ate) == 0);
84 if ((m & ios::out) == 0)
85 f = fopen(name, "rb");
86 else if ((m & ios::trunc) != 0)
87 f = fopen(name, "w+b");
88 else
89 f = fopen(name, "r+b");
90 }
91
92 #endif
93