Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | // This may look like C code, but it is really -*- C++ -*- |
2 | /* | |
3 | Copyright (C) 1989 Free Software Foundation | |
4 | written by Doug Lea (dl@rocky.oswego.edu) | |
5 | ||
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is distributed in the hope that it will be useful, | |
9 | but WITHOUT ANY WARRANTY. No author or distributor | |
10 | accepts responsibility to anyone for the consequences of using it | |
11 | or for whether it serves any particular purpose or works at all, | |
12 | unless he says so in writing. Refer to the GNU CC General Public | |
13 | License for full details. | |
14 | ||
15 | Everyone is granted permission to copy, modify and redistribute | |
16 | GNU CC, but only under the conditions described in the | |
17 | GNU CC General Public License. A copy of this license is | |
18 | supposed to have been given to you along with GNU CC so you | |
19 | can know your rights and responsibilities. It should be in a | |
20 | file named COPYING. Among other things, the copyright notice | |
21 | and this notice must be preserved on all copies. | |
22 | */ | |
23 | ||
24 | /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */ | |
25 | ||
26 | #ifdef __GNUG__ | |
27 | #pragma implementation | |
28 | #endif | |
29 | #include <stream.h> | |
30 | #include <stdarg.h> | |
31 | #include <values.h> | |
32 | #include <ctype.h> | |
33 | #include <Obstack.h> | |
34 | ||
35 | ostream::ostream(streambuf* s) | |
36 | : bp(s), state(_good), ownbuf(0) {} | |
37 | ||
38 | ostream::ostream(int sz, char* buf) | |
39 | : state(_good), ownbuf(1) | |
40 | { | |
41 | if (buf == 0) | |
42 | { | |
43 | buf = new char[sz]; | |
44 | bp = new streambuf(buf, sz); | |
45 | bp->setbuf(buf, sz); | |
46 | bp->alloc = 1; | |
47 | } | |
48 | else | |
49 | { | |
50 | bp = new streambuf(buf, sz); | |
51 | bp->alloc = 0; | |
52 | } | |
53 | } | |
54 | ||
55 | ||
56 | ostream::ostream(const char* filename, io_mode m, access_mode a) | |
57 | : state(_good), ownbuf(1) | |
58 | { | |
59 | bp = new filebuf(filename, m, a); | |
60 | } | |
61 | ||
62 | ostream::ostream(const char* filename, const char* m) | |
63 | : state(_good), ownbuf(1) | |
64 | { | |
65 | bp = new filebuf(filename, m); | |
66 | } | |
67 | ||
68 | ostream::ostream(int filedesc, io_mode m) | |
69 | : state(_good), ownbuf(1) | |
70 | { | |
71 | bp = new filebuf(filedesc, m); | |
72 | } | |
73 | ||
74 | ostream::ostream(FILE* fileptr) | |
75 | : state(_good), ownbuf(1) | |
76 | { | |
77 | bp = new filebuf(fileptr); | |
78 | } | |
79 | ||
80 | ostream::ostream(int filedesc) | |
81 | : state(_good), ownbuf(1) | |
82 | { | |
83 | bp = new filebuf(filedesc); | |
84 | } | |
85 | ||
86 | ostream::ostream(int filedesc, char* buf, int buflen) | |
87 | : state(_good), ownbuf(1) | |
88 | { | |
89 | bp = new filebuf(filedesc, buf, buflen); | |
90 | } | |
91 | ||
92 | ostream::~ostream() | |
93 | { | |
94 | if (ownbuf) delete bp; | |
95 | } | |
96 | ||
97 | ostream& ostream::open(const char* filename, io_mode m, access_mode a) | |
98 | { | |
99 | return failif(bp->open(filename, m, a) == 0); | |
100 | } | |
101 | ||
102 | ostream& ostream::open(const char* filename, const char* m) | |
103 | { | |
104 | return failif(bp->open(filename, m) == 0); | |
105 | } | |
106 | ||
107 | ostream& ostream::open(int filedesc, io_mode m) | |
108 | { | |
109 | return failif(bp->open(filedesc, m) == 0); | |
110 | } | |
111 | ||
112 | ostream& ostream::open(FILE* fileptr) | |
113 | { | |
114 | return failif(bp->open(fileptr) == 0); | |
115 | } | |
116 | ||
117 | ostream& ostream::open(const char* filenam, open_mode m) | |
118 | { | |
119 | return failif(bp->open(filenam, m) == 0); | |
120 | } | |
121 | ||
122 | ostream& ostream::form(const char* fmt...) | |
123 | { | |
124 | va_list args; | |
125 | va_start(args, fmt); | |
126 | char buf[BUFSIZ]; | |
127 | #ifndef HAVE_VPRINTF | |
128 | FILE b; | |
129 | b._flag = _IOWRT|_IOSTRG; | |
130 | b._ptr = buf; | |
131 | b._cnt = BUFSIZ; | |
132 | _doprnt(fmt, args, &b); | |
133 | putc('\0', &b); | |
134 | #else | |
135 | vsprintf(buf, fmt, args); | |
136 | #endif | |
137 | va_end(args); | |
138 | return put(buf); | |
139 | } | |
140 | ||
141 | ostream& ostream::operator<<(short n) | |
142 | { | |
143 | return put(itoa(long(n))); | |
144 | } | |
145 | ||
146 | ostream& ostream::operator<<(unsigned short n) | |
147 | { | |
148 | return put(itoa((unsigned long)(n))); | |
149 | } | |
150 | ||
151 | ostream& ostream::operator<<(int n) | |
152 | { | |
153 | return put(itoa(long(n))); | |
154 | } | |
155 | ||
156 | ostream& ostream::operator<<(unsigned int n) | |
157 | { | |
158 | return put(itoa((unsigned long)(n))); | |
159 | } | |
160 | ||
161 | ostream& ostream::operator<<(long n) | |
162 | { | |
163 | return put(itoa(n)); | |
164 | } | |
165 | ||
166 | ostream& ostream::operator<<(unsigned long n) | |
167 | { | |
168 | return put(itoa(n)); | |
169 | } | |
170 | ||
171 | #ifdef __GNUG__ | |
172 | #ifndef VMS | |
173 | ostream& ostream::operator<<(long long n) | |
174 | { | |
175 | return put(itoa(n)); | |
176 | } | |
177 | ||
178 | ostream& ostream::operator<<(unsigned long long n) | |
179 | { | |
180 | return put(itoa(n)); | |
181 | } | |
182 | #endif | |
183 | #endif | |
184 | ostream& ostream::operator<<(float n) | |
185 | { | |
186 | return put(dtoa(double(n))); | |
187 | } | |
188 | ||
189 | ostream& ostream::operator<<(double n) | |
190 | { | |
191 | return put(dtoa(n)); | |
192 | } | |
193 | ||
194 | ostream& ostream::operator<<(const void* p) | |
195 | { | |
196 | put("0x"); | |
197 | return put(itoa((unsigned long)(p), 16)); | |
198 | } | |
199 | ||
200 | ||
201 | const char* ostream::name() | |
202 | { | |
203 | return bp->name(); | |
204 | } | |
205 | ||
206 | void ostream::error() | |
207 | { | |
208 | bp->error(); | |
209 | } | |
210 | ||
211 | #if 0 /* BSD 4.4 */ | |
212 | ||
213 | ostream cerr(stderr); | |
214 | ostream cout(stdout); | |
215 | ||
216 | #else | |
217 | ||
218 | static char cerrbuf[1]; | |
219 | static char coutbuf[BUFSIZE]; | |
220 | ||
221 | ostream cerr(2, cerrbuf, 1); | |
222 | ostream cout(1, coutbuf, BUFSIZE); | |
223 | ||
224 | #endif |