DCL 4.0
Loading...
Searching...
No Matches
OutputStreamWriter.cpp
Go to the documentation of this file.
1
2#include <dcl/Config.h>
3
4#if __DCL_WINDOWS
5#include <windows.h>
6#endif
7
8#include <dcl/Charset.h>
10
11#if __DCL_HAVE_ALLOC_DEBUG
12#undef __DCL_ALLOC_LEVEL
13#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
14#endif
15
16#if __DCL_DEBUG
17#undef __THIS_FILE__
18static const char_t __THIS_FILE__[] = __T("dcl/OutputStreamWriter.cpp");
19#endif
20
21__DCL_BEGIN_NAMESPACE
22
24
26{
27 StringBuilder r = className();
28 if (__output)
29 {
30 r += __T("(") + __output->toString() + __T(",");
31 if (__encoder)
32 r += __encoder->className();
33 else
34 r += __T("null");
35 r += __T(")");
36 }
37 else
38 r += __T(" closed");
39 return r;
40}
41
43{
44 __output = NULL;
46 __closeDestroy = false;
47}
48
50 OutputStream& _output,
51 CharsetEncoder& _encoder
52 )
53{
54 __output = &_output;
55 __encoder = &_encoder;
56 __closeDestroy = false;
57}
58
60 OutputStream* _pOutput, // new OutputStream
61 CharsetEncoder* _pEncoder // = NULL
62 )
63{
64 __DCL_ASSERT_PARAM(_pOutput != NULL);
65
66 __output = _pOutput;
67 __encoder = _pEncoder;
68 __closeDestroy = true;
69
70 if (!__encoder)
71 __encoder = new LocaleEncoder();
72}
73
75{
76 if (__output)
77 {
78 try
79 {
80 close();
81 }
82 catch (Exception* cause)
83 {
84 __DCL_TRACE1(__T("%ls\n"), cause->toString().data());
85 cause->destroy();
86 }
87 }
88}
89
92{
94
95 Exception* e = NULL;
96 OutputStream* output = __output;
97 CharsetEncoder* encoder = __encoder;
98 __output = NULL;
100
101 if (__closeDestroy)
102 {
103 if (encoder)
104 encoder->destroy();
105
106 try
107 {
108 output->close();
109 }
110 catch (Exception* cause)
111 {
112 e = cause;
113 }
114 output->destroy();
115 }
116
117 if (e)
118 throw e;
119}
120
128
129Writer& OutputStreamWriter::write(const wchar_t* _buf, size_t _n)
131{
133 __DCL_ASSERT_PARAM(_buf != NULL);
134
136
137 const wchar_t* pCur = _buf;
138 const wchar_t* pEnd = _buf + _n;
139 byte_t aOutBuf[512];
140 size_t nInCount;
141
142 while ((nInCount = pEnd - pCur) > 0)
143 {
144 size_t nOutCount = __countof(aOutBuf, byte_t);
145 int r = __encoder->encode(pCur, nInCount, aOutBuf, nOutCount);
146 if (r != CS_NOERROR)
147 throw (new IOException(toString(), new CharsetConvertException(r)));
148 __output->write(aOutBuf, nOutCount);
149 pCur += nInCount;
150 }
151
152 return *this;
153}
154
155__DCL_END_NAMESPACE
156
#define __THIS_FILE__
Definition _trace.h:14
@ CS_NOERROR
Definition Charset.h:61
#define NULL
Definition Config.h:340
#define __countof(array, type)
Definition Config.h:365
wchar_t char_t
Definition Config.h:275
unsigned char byte_t
Definition Config.h:274
#define __DCL_THROWS1(e)
Definition Config.h:167
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
#define __DCL_ASSERT_PARAM(expr)
Definition Object.h:384
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
#define __DCL_ASSERT_HANDLE(expr)
Definition Object.h:383
ByteString r
virtual String toString() const
Definition Exception.cpp:40
virtual void destroy()
Definition Exception.cpp:74
virtual String toString() const
Definition Object.cpp:187
virtual void destroy()
Definition Object.cpp:192
String className() const
Definition Object.cpp:163
CharsetEncoder * __encoder
virtual void close() __DCL_THROWS1(IOException *)
virtual Writer & write(const wchar_t *_buf, size_t _n) __DCL_THROWS1(IOException *)
virtual void flush() __DCL_THROWS1(IOException *)
virtual String toString() const
OutputStreamWriter(OutputStream &__noclose__ _output, CharsetEncoder &_encoder)