"Fossies" - the Fresh Open Source Software Archive

Member "OpenSP-1.5.2/lib/OutputByteStream.cxx" (21 Jul 2005, 4701 Bytes) of package /linux/misc/old/OpenSP-1.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "OutputByteStream.cxx" see the Fossies "Dox" file reference documentation.

    1 // Copyright (c) 1997 James Clark
    2 // See the file COPYING for copying permission.
    3 
    4 #include "splib.h"
    5 #include "OutputByteStream.h"
    6 
    7 #include <sys/types.h>
    8 
    9 #ifdef SP_INCLUDE_IO_H
   10 #include <io.h>     // for open, fstat, lseek, read prototypes
   11 #endif
   12 
   13 #ifdef SP_INCLUDE_UNISTD_H
   14 #include <unistd.h>
   15 #endif
   16 
   17 #ifdef SP_INCLUDE_OSFCN_H
   18 #include <osfcn.h>
   19 #endif
   20 
   21 #include <fcntl.h>
   22 #include <sys/stat.h>
   23 #include <errno.h>
   24 #include <stdio.h>
   25 
   26 #ifdef SP_WIDE_SYSTEM
   27 #define STRICT
   28 #include <windows.h>
   29 #endif
   30 
   31 #ifndef O_CREAT
   32 #ifdef _O_CREAT
   33 #define O_CREAT _O_CREAT
   34 #endif
   35 #endif
   36 
   37 #ifndef O_WRONLY
   38 #ifdef _O_WRONLY
   39 #define O_WRONLY _O_WRONLY
   40 #endif
   41 #endif
   42 
   43 #ifndef O_TRUNC
   44 #ifdef _O_TRUNC
   45 #define O_TRUNC _O_TRUNC
   46 #endif
   47 #endif
   48 
   49 #ifndef O_BINARY
   50 #ifdef _O_BINARY
   51 #define O_BINARY _O_BINARY
   52 #else
   53 #define O_BINARY 0
   54 #endif
   55 #endif
   56 
   57 #ifndef S_IRUSR
   58 #if defined(S_IREAD)
   59 #define S_IRUSR S_IREAD
   60 #elif defined(_S_IREAD)
   61 #define S_IRUSR _S_IREAD
   62 #else
   63 #define S_IRUSR 0400
   64 #endif
   65 #endif
   66 
   67 #ifndef S_IWUSR
   68 #if defined(S_IWRITE)
   69 #define S_IWUSR S_IWRITE
   70 #elif defined(_S_IWRITE)
   71 #define S_IWUSR _S_IWRITE
   72 #else
   73 #define S_IWUSR 0200
   74 #endif
   75 #endif
   76 
   77 #ifndef S_IRGRP
   78 #if defined(S_IREAD)
   79 #define S_IRGRP S_IREAD
   80 #elif defined(_S_IREAD)
   81 #define S_IRGRP _S_IREAD
   82 #else
   83 #define S_IRGRP 0040
   84 #endif
   85 #endif
   86 
   87 #ifndef S_IWGRP
   88 #if defined(S_IWRITE)
   89 #define S_IWGRP S_IWRITE
   90 #elif defined(_S_IWRITE)
   91 #define S_IWGRP _S_IWRITE
   92 #else
   93 #define S_IWGRP 0020
   94 #endif
   95 #endif
   96 
   97 #ifndef S_IROTH
   98 #if defined(S_IREAD)
   99 #define S_IROTH S_IREAD
  100 #elif defined(_S_IREAD)
  101 #define S_IROTH _S_IREAD
  102 #else
  103 #define S_IROTH 0004
  104 #endif
  105 #endif
  106 
  107 #ifndef S_IWOTH
  108 #if defined(S_IWRITE)
  109 #define S_IWOTH S_IWRITE
  110 #elif defined(_S_IWRITE)
  111 #define S_IWOTH _S_IWRITE
  112 #else
  113 #define S_IWOTH 0002
  114 #endif
  115 #endif
  116 
  117 #ifdef SP_NAMESPACE
  118 namespace SP_NAMESPACE {
  119 #endif
  120 
  121 const int openFlags = O_CREAT|O_WRONLY|O_TRUNC|O_BINARY;
  122 const int protMode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
  123 const int bufSize = 8192;
  124 
  125 OutputByteStream::OutputByteStream()
  126 : ptr_(0), end_(0)
  127 {
  128 }
  129 
  130 OutputByteStream::~OutputByteStream()
  131 {
  132 }
  133 
  134 void OutputByteStream::sputn(const char *s, size_t n)
  135 {
  136   for (; n > 0; n--, s++)
  137     sputc(*s);
  138 }
  139 
  140 OutputByteStream &OutputByteStream::operator<<(long n)
  141 {
  142   char buf[32];
  143   sprintf(buf, "%ld", n);
  144   return *this << buf;
  145 }
  146 
  147 OutputByteStream &OutputByteStream::operator<<(unsigned long n)
  148 {
  149   char buf[32];
  150   sprintf(buf, "%lu", n);
  151   return *this << buf;
  152 }
  153 
  154 OutputByteStream &OutputByteStream::operator<<(const char *s)
  155 {
  156   while (*s)
  157     sputc(*s++);
  158   return *this;
  159 }
  160 
  161 StrOutputByteStream::StrOutputByteStream()
  162 {
  163 }
  164 
  165 void StrOutputByteStream::extractString(String<char> &str)
  166 {
  167   if (ptr_)
  168     buf_.resize(ptr_ - &buf_[0]);
  169   str.resize(0);
  170   buf_.swap(str);
  171   ptr_ = end_ = 0;
  172 }
  173 
  174 void StrOutputByteStream::flush()
  175 {
  176 }
  177 
  178 void StrOutputByteStream::flushBuf(char c)
  179 {
  180   if (!ptr_) {
  181     buf_.resize(16);
  182     ptr_ = &buf_[0];
  183   }
  184   else {
  185     size_t i = ptr_ - &buf_[0];
  186     buf_.resize(buf_.size()*2);
  187     ptr_ = &buf_[0] + i;
  188   }
  189   end_ = &buf_[0] + buf_.size();
  190   *ptr_++ = c;
  191 }
  192 
  193 FileOutputByteStream::FileOutputByteStream()
  194 : fd_(-1)
  195 {
  196 }
  197 
  198 FileOutputByteStream::FileOutputByteStream(int fd, Boolean closeFd)
  199 : fd_(-1)
  200 {
  201   attach(fd, closeFd);
  202 }
  203 
  204 FileOutputByteStream::~FileOutputByteStream()
  205 {
  206   close();
  207 }
  208 
  209 #ifdef SP_WIDE_SYSTEM
  210 
  211 Boolean FileOutputByteStream::open(const wchar_t *filename)
  212 {
  213   int fd = _wopen(filename, openFlags, protMode);
  214   if (fd >= 0)
  215     return attach(fd);
  216   // _wopen will always fail on Windows 95
  217   String<char> buf;
  218   int len = WideCharToMultiByte(CP_ACP, 0, filename, -1, 0, 0, 0, 0);
  219   buf.resize(len + 1);
  220   WideCharToMultiByte(CP_ACP, 0, filename, -1, buf.begin(), len, 0, 0);
  221   buf[len] = '\0';
  222   return attach(::open(buf.data(), openFlags, protMode));
  223 }
  224 
  225 #else /* not SP_WIDE_SYSTEM */
  226 
  227 Boolean FileOutputByteStream::open(const char *filename)
  228 {
  229   return attach(::open(filename, openFlags, protMode));
  230 }
  231 
  232 #endif /* not SP_WIDE_SYSTEM */
  233 
  234 Boolean FileOutputByteStream::attach(int fd, Boolean closeFd)
  235 {
  236   close();
  237   fd_ = fd;
  238   closeFd_ = closeFd;
  239   return fd_ >= 0;
  240 }
  241 
  242 Boolean FileOutputByteStream::close()
  243 {
  244   if (fd_ < 0)
  245     return 0;
  246   flush();
  247   int fd = fd_;
  248   fd_ = -1;
  249   if (!closeFd_)
  250     return 1;
  251   return ::close(fd) == 0;
  252 }
  253 
  254 void FileOutputByteStream::flush()
  255 {
  256   if (!buf_.size()) {
  257     if (fd_ < 0)
  258       return;
  259     buf_.resize(bufSize);
  260     ptr_ = &buf_[0];
  261     end_ = ptr_ + buf_.size();
  262   }
  263   size_t n = ptr_ - &buf_[0];
  264   const char *s = buf_.data();
  265   while (n > 0) {
  266     int nw = ::write(fd_, s, n);
  267     if (nw < 0)
  268       break;
  269     n -= nw;
  270     s += nw;
  271   }
  272   ptr_ = &buf_[0];
  273 }
  274 
  275 void FileOutputByteStream::flushBuf(char c)
  276 {
  277   flush();
  278   *ptr_++ = c;
  279 }
  280 
  281 #ifdef SP_NAMESPACE
  282 }
  283 #endif