1 // Copyright (c) 1997 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident "%Z%%M% %I% %E% SMI"
6 #include "OutputByteStream.h"
10 #ifdef SP_INCLUDE_IO_H
11 #include <io.h> // for open, fstat, lseek, read prototypes
14 #ifdef SP_INCLUDE_UNISTD_H
18 #ifdef SP_INCLUDE_OSFCN_H
34 #define O_CREAT _O_CREAT
40 #define O_WRONLY _O_WRONLY
46 #define O_TRUNC _O_TRUNC
52 #define O_BINARY _O_BINARY
60 #define S_IRUSR S_IREAD
61 #elif defined(_S_IREAD)
62 #define S_IRUSR _S_IREAD
70 #define S_IWUSR S_IWRITE
71 #elif defined(_S_IWRITE)
72 #define S_IWUSR _S_IWRITE
80 #define S_IRGRP S_IREAD
81 #elif defined(_S_IREAD)
82 #define S_IRGRP _S_IREAD
90 #define S_IWGRP S_IWRITE
91 #elif defined(_S_IWRITE)
92 #define S_IWGRP _S_IWRITE
100 #define S_IROTH S_IREAD
101 #elif defined(_S_IREAD)
102 #define S_IROTH _S_IREAD
109 #if defined(S_IWRITE)
110 #define S_IWOTH S_IWRITE
111 #elif defined(_S_IWRITE)
112 #define S_IWOTH _S_IWRITE
119 namespace SP_NAMESPACE
{
122 const int openFlags
= O_CREAT
|O_WRONLY
|O_TRUNC
|O_BINARY
;
123 const int protMode
= S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
;
124 const int bufSize
= 8192;
126 OutputByteStream::OutputByteStream()
131 OutputByteStream::~OutputByteStream()
135 void OutputByteStream::sputn(const char *s
, size_t n
)
137 for (; n
> 0; n
--, s
++)
141 OutputByteStream
&OutputByteStream::operator<<(long n
)
144 sprintf(buf
, "%ld", n
);
148 OutputByteStream
&OutputByteStream::operator<<(unsigned long n
)
151 sprintf(buf
, "%lu", n
);
155 OutputByteStream
&OutputByteStream::operator<<(const char *s
)
162 StrOutputByteStream::StrOutputByteStream()
166 void StrOutputByteStream::extractString(String
<char> &str
)
169 buf_
.resize(ptr_
- &buf_
[0]);
175 void StrOutputByteStream::flush()
179 void StrOutputByteStream::flushBuf(char c
)
186 size_t i
= ptr_
- &buf_
[0];
187 buf_
.resize(buf_
.size()*2);
190 end_
= &buf_
[0] + buf_
.size();
194 FileOutputByteStream::FileOutputByteStream()
199 FileOutputByteStream::FileOutputByteStream(int fd
, Boolean closeFd
)
205 FileOutputByteStream::~FileOutputByteStream()
210 #ifdef SP_WIDE_SYSTEM
212 Boolean
FileOutputByteStream::open(const wchar_t *filename
)
214 int fd
= _wopen(filename
, openFlags
, protMode
);
217 // _wopen will always fail on Windows 95
219 int len
= WideCharToMultiByte(CP_ACP
, 0, filename
, -1, 0, 0, 0, 0);
221 WideCharToMultiByte(CP_ACP
, 0, filename
, -1, buf
.begin(), len
, 0, 0);
223 return attach(::open(buf
.data(), openFlags
, protMode
));
226 #else /* not SP_WIDE_SYSTEM */
228 Boolean
FileOutputByteStream::open(const char *filename
)
230 return attach(::open(filename
, openFlags
, protMode
));
233 #endif /* not SP_WIDE_SYSTEM */
235 Boolean
FileOutputByteStream::attach(int fd
, Boolean closeFd
)
243 Boolean
FileOutputByteStream::close()
252 return ::close(fd
) == 0;
255 void FileOutputByteStream::flush()
260 buf_
.resize(bufSize
);
262 end_
= ptr_
+ buf_
.size();
264 size_t n
= ptr_
- &buf_
[0];
265 const char *s
= buf_
.data();
267 int nw
= ::write(fd_
, s
, n
);
276 void FileOutputByteStream::flushBuf(char c
)