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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// Copyright (c) 1997 James Clark
// See the file COPYING for copying permission.
#include "splib.h"
#include "OutputByteStream.h"
#include <sys/types.h>
#ifdef SP_INCLUDE_IO_H
#include <io.h> // for open, fstat, lseek, read prototypes
#endif
#ifdef SP_INCLUDE_UNISTD_H
#include <unistd.h>
#endif
#ifdef SP_INCLUDE_OSFCN_H
#include <osfcn.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#ifdef SP_WIDE_SYSTEM
#define STRICT
#include <windows.h>
#endif
#ifndef O_CREAT
#ifdef _O_CREAT
#define O_CREAT _O_CREAT
#endif
#endif
#ifndef O_WRONLY
#ifdef _O_WRONLY
#define O_WRONLY _O_WRONLY
#endif
#endif
#ifndef O_TRUNC
#ifdef _O_TRUNC
#define O_TRUNC _O_TRUNC
#endif
#endif
#ifndef O_BINARY
#ifdef _O_BINARY
#define O_BINARY _O_BINARY
#else
#define O_BINARY 0
#endif
#endif
#ifndef S_IRUSR
#if defined(S_IREAD)
#define S_IRUSR S_IREAD
#elif defined(_S_IREAD)
#define S_IRUSR _S_IREAD
#else
#define S_IRUSR 0400
#endif
#endif
#ifndef S_IWUSR
#if defined(S_IWRITE)
#define S_IWUSR S_IWRITE
#elif defined(_S_IWRITE)
#define S_IWUSR _S_IWRITE
#else
#define S_IWUSR 0200
#endif
#endif
#ifndef S_IRGRP
#if defined(S_IREAD)
#define S_IRGRP S_IREAD
#elif defined(_S_IREAD)
#define S_IRGRP _S_IREAD
#else
#define S_IRGRP 0040
#endif
#endif
#ifndef S_IWGRP
#if defined(S_IWRITE)
#define S_IWGRP S_IWRITE
#elif defined(_S_IWRITE)
#define S_IWGRP _S_IWRITE
#else
#define S_IWGRP 0020
#endif
#endif
#ifndef S_IROTH
#if defined(S_IREAD)
#define S_IROTH S_IREAD
#elif defined(_S_IREAD)
#define S_IROTH _S_IREAD
#else
#define S_IROTH 0004
#endif
#endif
#ifndef S_IWOTH
#if defined(S_IWRITE)
#define S_IWOTH S_IWRITE
#elif defined(_S_IWRITE)
#define S_IWOTH _S_IWRITE
#else
#define S_IWOTH 0002
#endif
#endif
#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif
const int openFlags = O_CREAT|O_WRONLY|O_TRUNC|O_BINARY;
const int protMode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
const int bufSize = 8192;
OutputByteStream::OutputByteStream()
: ptr_(0), end_(0)
{
}
OutputByteStream::~OutputByteStream()
{
}
void OutputByteStream::sputn(const char *s, size_t n)
{
for (; n > 0; n--, s++)
sputc(*s);
}
OutputByteStream &OutputByteStream::operator<<(long n)
{
char buf[32];
sprintf(buf, "%ld", n);
return *this << buf;
}
OutputByteStream &OutputByteStream::operator<<(unsigned long n)
{
char buf[32];
sprintf(buf, "%lu", n);
return *this << buf;
}
OutputByteStream &OutputByteStream::operator<<(const char *s)
{
while (*s)
sputc(*s++);
return *this;
}
StrOutputByteStream::StrOutputByteStream()
{
}
void StrOutputByteStream::extractString(String<char> &str)
{
if (ptr_)
buf_.resize(ptr_ - &buf_[0]);
str.resize(0);
buf_.swap(str);
ptr_ = end_ = 0;
}
void StrOutputByteStream::flush()
{
}
void StrOutputByteStream::flushBuf(char c)
{
if (!ptr_) {
buf_.resize(16);
ptr_ = &buf_[0];
}
else {
size_t i = ptr_ - &buf_[0];
buf_.resize(buf_.size()*2);
ptr_ = &buf_[0] + i;
}
end_ = &buf_[0] + buf_.size();
*ptr_++ = c;
}
FileOutputByteStream::FileOutputByteStream()
: fd_(-1)
{
}
FileOutputByteStream::FileOutputByteStream(int fd, Boolean closeFd)
: fd_(-1)
{
attach(fd, closeFd);
}
FileOutputByteStream::~FileOutputByteStream()
{
close();
}
#ifdef SP_WIDE_SYSTEM
Boolean FileOutputByteStream::open(const wchar_t *filename)
{
int fd = _wopen(filename, openFlags, protMode);
if (fd >= 0)
return attach(fd);
// _wopen will always fail on Windows 95
String<char> buf;
int len = WideCharToMultiByte(CP_ACP, 0, filename, -1, 0, 0, 0, 0);
buf.resize(len + 1);
WideCharToMultiByte(CP_ACP, 0, filename, -1, buf.begin(), len, 0, 0);
buf[len] = '\0';
return attach(::open(buf.data(), openFlags, protMode));
}
#else /* not SP_WIDE_SYSTEM */
Boolean FileOutputByteStream::open(const char *filename)
{
return attach(::open(filename, openFlags, protMode));
}
#endif /* not SP_WIDE_SYSTEM */
Boolean FileOutputByteStream::attach(int fd, Boolean closeFd)
{
close();
fd_ = fd;
closeFd_ = closeFd;
return fd_ >= 0;
}
Boolean FileOutputByteStream::close()
{
if (fd_ < 0)
return 0;
flush();
int fd = fd_;
fd_ = -1;
if (!closeFd_)
return 1;
return ::close(fd) == 0;
}
void FileOutputByteStream::flush()
{
if (!buf_.size()) {
if (fd_ < 0)
return;
buf_.resize(bufSize);
ptr_ = &buf_[0];
end_ = ptr_ + buf_.size();
}
size_t n = ptr_ - &buf_[0];
const char *s = buf_.data();
while (n > 0) {
int nw = ::write(fd_, s, n);
if (nw < 0)
break;
n -= nw;
s += nw;
}
ptr_ = &buf_[0];
}
void FileOutputByteStream::flushBuf(char c)
{
flush();
*ptr_++ = c;
}
#ifdef SP_NAMESPACE
}
#endif
|