OutputCharStream.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: OutputCharStream.h /main/1 1996/07/29 16:59:39 cde-hp $ */
  24. // Copyright (c) 1994 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifndef OutputCharStream_INCLUDED
  27. #define OutputCharStream_INCLUDED 1
  28. #include "types.h"
  29. #include <stddef.h>
  30. #include "StringC.h"
  31. #include "Owner.h"
  32. #include "CodingSystem.h"
  33. #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
  34. #include <streambuf>
  35. using namespace std;
  36. #else
  37. class streambuf;
  38. #endif
  39. #ifdef SP_NAMESPACE
  40. namespace SP_NAMESPACE {
  41. #endif
  42. class SP_API OutputCharStream {
  43. public:
  44. enum Newline { newline };
  45. typedef void (*Escaper)(OutputCharStream &, Char);
  46. OutputCharStream();
  47. virtual ~OutputCharStream();
  48. OutputCharStream &put(Char);
  49. OutputCharStream &write(const Char *, size_t);
  50. virtual void flush() = 0;
  51. void setEscaper(Escaper);
  52. OutputCharStream &operator<<(char);
  53. OutputCharStream &operator<<(const char *);
  54. OutputCharStream &operator<<(const StringC &);
  55. OutputCharStream &operator<<(unsigned long);
  56. OutputCharStream &operator<<(int);
  57. OutputCharStream &operator<<(Newline);
  58. private:
  59. OutputCharStream(const OutputCharStream &); // undefined
  60. void operator=(const OutputCharStream &); // undefined
  61. virtual void flushBuf(Char) = 0;
  62. Escaper escaper_;
  63. protected:
  64. void escape(OutputCharStream &, Char c);
  65. Char *ptr_;
  66. Char *end_;
  67. };
  68. class SP_API IosOutputCharStream : public OutputCharStream,
  69. private Encoder::Handler {
  70. public:
  71. IosOutputCharStream();
  72. // the streambuf will not be deleted
  73. IosOutputCharStream(streambuf *, const OutputCodingSystem *);
  74. ~IosOutputCharStream();
  75. void open(streambuf *, const OutputCodingSystem *);
  76. void flush();
  77. private:
  78. IosOutputCharStream(const IosOutputCharStream &); // undefined
  79. void operator=(const IosOutputCharStream &); // undefined
  80. IosOutputCharStream(streambuf *, Encoder *);
  81. void allocBuf(int bytesPerChar);
  82. void flushBuf(Char);
  83. void handleUnencodable(Char c, streambuf *);
  84. Char *buf_;
  85. streambuf *byteStream_;
  86. Encoder *encoder_;
  87. Owner<Encoder> ownedEncoder_;
  88. };
  89. class SP_API StrOutputCharStream : public OutputCharStream {
  90. public:
  91. StrOutputCharStream();
  92. ~StrOutputCharStream();
  93. void extractString(StringC &);
  94. void flush();
  95. private:
  96. void flushBuf(Char);
  97. void sync(size_t);
  98. StrOutputCharStream(const StrOutputCharStream &); // undefined
  99. void operator=(const StrOutputCharStream &); // undefined
  100. Char *buf_;
  101. size_t bufSize_;
  102. };
  103. class SP_API RecordOutputCharStream : public OutputCharStream {
  104. public:
  105. RecordOutputCharStream(OutputCharStream *);
  106. ~RecordOutputCharStream();
  107. void flush();
  108. private:
  109. RecordOutputCharStream(const RecordOutputCharStream &); // undefined
  110. void operator=(const RecordOutputCharStream &); // undefined
  111. void flushBuf(Char);
  112. void outputBuf();
  113. OutputCharStream *os_;
  114. enum { bufSize_ = 1024 };
  115. Char buf_[bufSize_];
  116. };
  117. inline
  118. OutputCharStream &OutputCharStream::put(Char c)
  119. {
  120. if (ptr_ < end_)
  121. *ptr_++ = c;
  122. else
  123. flushBuf(c);
  124. return *this;
  125. }
  126. inline
  127. OutputCharStream &OutputCharStream::operator<<(char c)
  128. {
  129. return put(Char(c));
  130. }
  131. inline
  132. OutputCharStream &OutputCharStream::operator<<(Newline)
  133. {
  134. #ifdef SP_HAVE_SETMODE
  135. put(Char('\r'));
  136. #endif
  137. return put(Char('\n'));
  138. }
  139. inline
  140. OutputCharStream &OutputCharStream::operator<<(const StringC &str)
  141. {
  142. return write(str.data(), str.size());
  143. }
  144. inline
  145. void OutputCharStream::setEscaper(Escaper f)
  146. {
  147. escaper_ = f;
  148. }
  149. inline
  150. void OutputCharStream::escape(OutputCharStream &s, Char c)
  151. {
  152. if (escaper_)
  153. (*escaper_)(s, c);
  154. }
  155. #ifdef SP_NAMESPACE
  156. }
  157. #endif
  158. #endif /* not OutputCharStream_INCLUDED */