Oh GACK! src-clean doesn't quite work that easily since cleandist rebuilds the
[unix-history] / gnu / lib / libg++ / libg++ / filebuf.cc
CommitLineData
15637ed4
RG
1/*
2Copyright (C) 1990 Free Software Foundation
3 written by Doug Lea (dl@rocky.oswego.edu)
4
5This file is part of GNU CC.
6
7GNU CC is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY. No author or distributor
9accepts responsibility to anyone for the consequences of using it
10or for whether it serves any particular purpose or works at all,
11unless he says so in writing. Refer to the GNU CC General Public
12License for full details.
13
14Everyone is granted permission to copy, modify and redistribute
15GNU CC, but only under the conditions described in the
16GNU CC General Public License. A copy of this license is
17supposed to have been given to you along with GNU CC so you
18can know your rights and responsibilities. It should be in a
19file named COPYING. Among other things, the copyright notice
20and this notice must be preserved on all copies.
21*/
22
23#if 1
24#ifdef __GNUG__
25#pragma implementation
26#endif
27#endif
28
29#include <std.h>
30#include <sys/file.h> // needed to determine values of O_RDONLY...
31#include <filebuf.h>
32#include <open.h>
33
34filebuf::filebuf()
35 :streambuf(), fd(-1), opened(0) {}
36
37filebuf::filebuf(int newfd)
38 : streambuf(), fd(newfd), opened(1) {}
39
40filebuf::filebuf(int newfd, char* buf, int buflen)
41 : streambuf(buf, buflen), fd(newfd), opened(1) {}
42
43filebuf::filebuf(const char* filename, io_mode m, access_mode a)
44 : streambuf()
45{
46 open(filename, m, a);
47}
48
49filebuf::filebuf(const char* filename, const char* m)
50 : streambuf()
51{
52 open(filename, m);
53}
54
55filebuf::filebuf(int filedesc, io_mode m)
56 : streambuf()
57{
58 open(filedesc, m);
59}
60
61filebuf::filebuf(FILE* fileptr)
62 : streambuf()
63{
64 open(fileptr);
65}
66
67int filebuf::is_open()
68{
69 return opened;
70}
71
72int filebuf::close()
73{
74 int was = opened;
75 if (was) ::close(fd);
76 opened = 0;
77 return was;
78}
79
80streambuf* filebuf::open(const char* filename, open_mode m)
81{
82 if (opened) return 0;
83 int mode = -1; // any illegal value
84 switch (m)
85 {
86 case input: mode = O_RDONLY;
87 break;
88 case output: mode = O_WRONLY | O_CREAT | O_TRUNC;
89 break;
90 case append: mode = O_APPEND | O_CREAT | O_WRONLY;
91 break;
92 }
93 fd = ::open(filename, mode, 0666);
94 if (opened = (fd >= 0))
95 {
96 allocate();
97 return this;
98 }
99 else
100 return 0;
101}
102
103
104streambuf* filebuf::open(const char* filename, io_mode m, access_mode a)
105{
106 int open_arg = open_cmd_arg(m, a);
107 if (open_arg == -1) return 0;
108 fd = ::open(filename, open_arg, 0666);
109 if (opened = (fd >= 0))
110 {
111 allocate();
112 return this;
113 }
114 else
115 return 0;
116
117}
118
119streambuf* filebuf::open(const char* filename, const char* m)
120{
121 int open_arg = open_cmd_arg(m);
122 if (open_arg == -1) return 0;
123 fd = ::open(filename, open_arg, 0666);
124 if (opened = (fd >= 0))
125 {
126 allocate();
127 return this;
128 }
129 else
130 return 0;
131}
132
133streambuf* filebuf::open(int filedesc, io_mode m)
134{
135 int open_arg = open_cmd_arg(m, a_use);
136 if (open_arg == -1) return 0;
137 fd = ::open(name, open_arg, 0666);
138 if (opened = (fd >= 0))
139 {
140 allocate();
141 return this;
142 }
143 else
144 return 0;
145}
146
147streambuf* filebuf::open(FILE* fileptr)
148{
149 opened = fileptr != 0;
150 fd = fileno(fileptr);
151 return this;
152}
153
154int filebuf::underflow()
155{
156 if (!opened) return EOF;
157 if (base == 0) allocate();
158 int nwanted = eptr - base + 1;
159 int nread = ::read(fd, base, nwanted);
160 if (nread >= 0)
161 {
162 gptr = base;
163 pptr = base + nread;
164 }
165 return (nread <= 0)? EOF : int(*gptr);
166}
167
168int filebuf::overflow(int ch)
169{
170 if (!opened) return EOF;
171 if (base == 0) allocate();
172 if (ch != EOF) // overflow *must* be called before really full
173 *pptr++ = (char)(ch);
174
175 // loop, in case write can't handle full request
176 // From: Rene' Seindal <seindal@diku.dk>
177
178 int w, n, t;
179 for (w = t = 0, n = pptr - base; n > 0; n -= w, t += w)
180 {
181 if ((w = ::write(fd, base + t, n)) < 0)
182 break;
183 }
184
185 pptr = base;
186 return (n == 0 && w >= 0)? 0 : EOF;
187}
188
189filebuf::~filebuf()
190{
191 close();
192}