adding GNU dc ("desk calculator")
[unix-history] / gnu / lib / libg++ / libg++ / ostream.cc
CommitLineData
15637ed4
RG
1// This may look like C code, but it is really -*- C++ -*-
2/*
3Copyright (C) 1989 Free Software Foundation
4 written by Doug Lea (dl@rocky.oswego.edu)
5
6This file is part of GNU CC.
7
8GNU CC is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY. No author or distributor
10accepts responsibility to anyone for the consequences of using it
11or for whether it serves any particular purpose or works at all,
12unless he says so in writing. Refer to the GNU CC General Public
13License for full details.
14
15Everyone is granted permission to copy, modify and redistribute
16GNU CC, but only under the conditions described in the
17GNU CC General Public License. A copy of this license is
18supposed to have been given to you along with GNU CC so you
19can know your rights and responsibilities. It should be in a
20file named COPYING. Among other things, the copyright notice
21and 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
35ostream::ostream(streambuf* s)
36 : bp(s), state(_good), ownbuf(0) {}
37
38ostream::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
56ostream::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
62ostream::ostream(const char* filename, const char* m)
63 : state(_good), ownbuf(1)
64{
65 bp = new filebuf(filename, m);
66}
67
68ostream::ostream(int filedesc, io_mode m)
69 : state(_good), ownbuf(1)
70{
71 bp = new filebuf(filedesc, m);
72}
73
74ostream::ostream(FILE* fileptr)
75 : state(_good), ownbuf(1)
76{
77 bp = new filebuf(fileptr);
78}
79
80ostream::ostream(int filedesc)
81 : state(_good), ownbuf(1)
82{
83 bp = new filebuf(filedesc);
84}
85
86ostream::ostream(int filedesc, char* buf, int buflen)
87 : state(_good), ownbuf(1)
88{
89 bp = new filebuf(filedesc, buf, buflen);
90}
91
92ostream::~ostream()
93{
94 if (ownbuf) delete bp;
95}
96
97ostream& ostream::open(const char* filename, io_mode m, access_mode a)
98{
99 return failif(bp->open(filename, m, a) == 0);
100}
101
102ostream& ostream::open(const char* filename, const char* m)
103{
104 return failif(bp->open(filename, m) == 0);
105}
106
107ostream& ostream::open(int filedesc, io_mode m)
108{
109 return failif(bp->open(filedesc, m) == 0);
110}
111
112ostream& ostream::open(FILE* fileptr)
113{
114 return failif(bp->open(fileptr) == 0);
115}
116
117ostream& ostream::open(const char* filenam, open_mode m)
118{
119 return failif(bp->open(filenam, m) == 0);
120}
121
122ostream& 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
141ostream& ostream::operator<<(short n)
142{
143 return put(itoa(long(n)));
144}
145
146ostream& ostream::operator<<(unsigned short n)
147{
148 return put(itoa((unsigned long)(n)));
149}
150
151ostream& ostream::operator<<(int n)
152{
153 return put(itoa(long(n)));
154}
155
156ostream& ostream::operator<<(unsigned int n)
157{
158 return put(itoa((unsigned long)(n)));
159}
160
161ostream& ostream::operator<<(long n)
162{
163 return put(itoa(n));
164}
165
166ostream& ostream::operator<<(unsigned long n)
167{
168 return put(itoa(n));
169}
170
171#ifdef __GNUG__
172#ifndef VMS
173ostream& ostream::operator<<(long long n)
174{
175 return put(itoa(n));
176}
177
178ostream& ostream::operator<<(unsigned long long n)
179{
180 return put(itoa(n));
181}
182#endif
183#endif
184ostream& ostream::operator<<(float n)
185{
186 return put(dtoa(double(n)));
187}
188
189ostream& ostream::operator<<(double n)
190{
191 return put(dtoa(n));
192}
193
194ostream& ostream::operator<<(const void* p)
195{
196 put("0x");
197 return put(itoa((unsigned long)(p), 16));
198}
199
200
201const char* ostream::name()
202{
203 return bp->name();
204}
205
206void ostream::error()
207{
208 bp->error();
209}
210
211#if 0 /* BSD 4.4 */
212
213ostream cerr(stderr);
214ostream cout(stdout);
215
216#else
217
218static char cerrbuf[1];
219static char coutbuf[BUFSIZE];
220
221ostream cerr(2, cerrbuf, 1);
222ostream cout(1, coutbuf, BUFSIZE);
223
224#endif