summaryrefslogtreecommitdiffstats
path: root/private/crt32/iostream/ostream.cxx
blob: 69eaefabe4f921f2aaaa82e4a0c8f9feb6144f1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/***
* ostream.cxx - definitions for ostream and ostream_withassign classes
*
*	Copyright (c) 1991-1992, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*	Contains the core member function definitions for ostream and
*	ostream_withassign classes.
*
*Revision History:
*	07-01-91   KRS	Created.
*	08-19-91   KRS	Corrected my interpretation of the spec. for negative
*			hex or octal integers.
*	08-20-91   KRS  Replace 'clear(x)' with 'state |= x'.
*			Skip text translation for write().
*	08-26-91   KRS	Modify to work with DLL's/MTHREAD.
*	09-05-91   KRS	Fix opfx() to flush tied ostream, not current one.
*	09-09-91   KRS	Remove sync_with_stdio() call from Iostream_init().
*			Reinstate text-translation (by default) for write().
*	09-19-91   KRS	Add opfx()/osfx() calls to put() and write().
*			Schedule destruction of predefined streams.
*	09-23-91   KRS	Split up for granularity.
*	10-04-91   KRS	Use bp->sputc, not put(), in writepad().
*	10-24-91   KRS	Added initialization of x_floatused.
*	11-04-91   KRS	Make constructors work with virtual base.
*	11-20-91   KRS	Add/fix copy constructor and assignment operators.
*
*******************************************************************************/

#include <cruntime.h>
#include <internal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#pragma hdrstop

int ostream::opfx()
{
    lock();
    if (state)
	{
	state |= ios::failbit;	// CONSIDER???
	unlock();
	return 0;
	}
    if (x_tie)
	{
	x_tie->flush();
	}
    lockbuf();
    return(1);	// return non-zero
}

void ostream::osfx()
{
    x_width = 0;
    if (x_flags & unitbuf)
	{
	if (bp->sync()==EOF)
	    state = failbit | badbit;
	}
#ifndef	_WINDLL
    if (x_flags & ios::stdio)
	{
	if (fflush(stdout)==EOF)
	    state |= failbit;
	if (fflush(stderr)==EOF)
	    state |= failbit;
	}
#endif
    unlockbuf();
    unlock();
}

// note: called inline by char and signed char versions:
#if 0
ostream&  ostream::operator<<(unsigned char c)
{
    if (opfx())
	{
	if (x_width)
	    {
	    _WINSTATIC char outc[2];
	    outc[0] = c;
	    outc[1] = '\0';
	    writepad("",outc);
	    }
	else if (bp->sputc(c)==EOF)
	    {
	    if (bp->overflow(c)==EOF)
		state |= (badbit|failbit);  // fatal error?
	    }
	osfx();
	}
    return *this;
}
#endif

// note: called inline by unsigned char * and signed char * versions:
ostream& ostream::operator<<(const char * s)
{
    if (opfx()) {
	writepad("",s);
	osfx();
    }
    return *this;
}

ostream& ostream::flush()
{
    lock();
    lockbuf();
    if (bp->sync()==EOF)
	state |= ios::failbit;
    unlockbuf();
    unlock();
    return(*this);
}

	ostream::ostream()
// : ios()
{
	x_floatused = 0;
	// CONSIDER: do anything else?
}

	ostream::ostream(streambuf* _inistbf)
// : ios()
{
	init(_inistbf);

	x_floatused = 0;
	// CONSIDER: do anything else?
}

	ostream::ostream(const ostream& _ostrm)
// : ios()
{
	init(_ostrm.rdbuf());

	x_floatused = 0;
	// CONSIDER: do anything else?
}

	ostream::~ostream()
// : ~ios()
{
	// CONSIDER: do anything else?
}

// used in ios::sync_with_stdio()
ostream& ostream::operator=(streambuf * _sbuf)
{
// consider: may be some redundency here, depending on spec.

	if (delbuf() && rdbuf())
	    delete rdbuf();
	bp = 0;

	this->ios::operator=(ios());	// initialize ios members
	delbuf(0);			// important!
	init(_sbuf);

//	x_floatused = 0;		// not necessary

	return *this;
}


	ostream_withassign::ostream_withassign()
: ostream()
{
	// CONSIDER: do anything else?
}

	ostream_withassign::ostream_withassign(streambuf* _os)
: ostream(_os)
{
	// CONSIDER: do anything else?
}

	ostream_withassign::~ostream_withassign()
// : ~ostream()
{
;	// CONSIDER: do anything else?
}

ostream& ostream::writepad(const char * leader, const char * value)

	{
unsigned int len, leadlen;
long padlen;
	leadlen = strlen(leader);
	len = strlen(value);
	padlen = (((unsigned)x_width) > (len+leadlen)) ? ((unsigned)x_width) - (len + leadlen) : 0;
	if (!(x_flags & (left|internal)))  // default is right-adjustment
	    {
	    while (padlen-- >0)
		{
		if (bp->sputc((unsigned char)x_fill)==EOF)
		    state |= (ios::failbit|ios::badbit);
		}
	    }
	if (leadlen)
	    {
	    if ((unsigned)bp->sputn(leader,leadlen)!=leadlen)
	        state |= (failbit|badbit);	// CONSIDER: right error?
	    }
	if (x_flags & internal) 
	    {
	    while (padlen-- >0)
		{
		if (bp->sputc((unsigned char)x_fill)==EOF)
		    state |= (ios::failbit|ios::badbit);
		}
	    }
	if ((unsigned)bp->sputn(value,len)!=len)
	    state |= (failbit|badbit);	// CONSIDER: right error?
	if (x_flags & left) 
	    {
	    while ((padlen--)>0)        // left-adjust if necessary
		{
		if (bp->sputc((unsigned char)x_fill)==EOF)
		    state |= (ios::failbit|ios::badbit);
		}
	    }
	return (*this);
	}