DCL 4.0
Loading...
Searching...
No Matches
StringWriter.cpp
Go to the documentation of this file.
1#include <string.h> // memcpy
2#include <dcl/StringWriter.h>
3#include <dcl/size_t.h>
4
5#if __DCL_DEBUG
6#undef __THIS_FILE__
7static const char_t __THIS_FILE__[] = __T("dcl/StringWriter.cpp");
8#endif
9
10__DCL_BEGIN_NAMESPACE
11
13
14String StringWriter::toString() const
15{
16 String r;
17 if (__buf != NULL) {
18 // STRING::assign 에서 __buf의 참조카운터가 증가한다.
19 r.assign(__buf);
20 }
21 return r;
22}
23
24StringWriter::StringWriter(size_t _capacity)
25{
26 __DCL_ASSERT_PARAM(_capacity > 0);
27
28 __buf = NULL;
29 __capacity = _capacity;
30}
31
32StringWriter::~StringWriter()
33{
34 if (__buf) {
35 try {
36 close();
37 }
38 catch (Exception* cause) {
39 __DCL_TRACE1(__T("%ls\n"), cause->toString().data());
40 cause->destroy();
41 }
42 }
43}
44
45void StringWriter::close()
47{
48 if (__buf != NULL) {
49 __buf->release();
50 __buf = NULL;
51 }
52}
53
54Writer& StringWriter::write(const wchar_t* _buf, size_t _n)
56{
57 __DCL_ASSERT_PARAM(_buf != NULL);
58
59 if (_n) {
60 if (__buf == NULL)
61 __buf = CharBuffer::create_e(__MAX(_n, __capacity));
62 else if (__buf->__refCount > 1) {
63 CharBuffer* buf = CharBuffer::create_e(__MAX(_n, __capacity));
64 memcpy(buf->data(), __buf->data(), (__buf->__dataLength + 1) * sizeof(wchar_t));
65 buf->__dataLength = __buf->__dataLength;
66 __DCL_ASSERT(buf->data()[buf->__dataLength] == L'\0');
67 __buf->release();
68 __buf = buf;
69 }
70 else {
71 __DCL_ASSERT(__buf->__refCount == 1);
72 }
73
74 CharBuffer::write(__buf, _buf, _n);
75 }
76 return *this;
77}
78
79int StringWriter::vprintf(const wchar_t* _format, va_list _arglist)
81{
82 __DCL_ASSERT_PARAM(_format != NULL);
83
84 if (__buf == NULL)
85 __buf = CharBuffer::create_e(__capacity);
86 else if (__buf->__refCount > 1) {
87 CharBuffer* buf = CharBuffer::create_e(__capacity);
88 memcpy(buf->data(), __buf->data(), (__buf->__dataLength + 1) * sizeof(wchar_t));
89 buf->__dataLength = __buf->__dataLength;
90 __DCL_ASSERT(buf->data()[buf->__dataLength] == L'\0');
91 __buf->release();
92 __buf = buf;
93 }
94 else {
95 __DCL_ASSERT(__buf->__refCount == 1);
96 }
97
98 return CharBuffer::vformat(__buf, _format, _arglist);
99}
100
101size_t StringWriter::writeTo(Writer& _writer) const
103{
104 if (__buf != NULL && __buf->__dataLength) {
105 _writer.write(__buf->data(), __buf->__dataLength);
106 return __buf->__dataLength;
107 }
108 return 0;
109}
110
111size_t StringWriter::flushTo(Writer& _writer)
113{
114 size_t n = writeTo(_writer);
115 if (n > 0) {
116 reset();
117 }
118 return n;
119}
120
121void StringWriter::reset()
122{
123 if (__buf != NULL) {
124 if (__buf->__refCount == 1) {
125 __buf->__dataLength = 0;
126 __buf->data()[__buf->__dataLength] = __T('\0');
127 }
128 else {
129 __DCL_ASSERT(__buf->__refCount > 1);
130 __buf->release();
131 __buf = NULL;
132 }
133 }
134}
135
136__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
#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
ByteString r
ByteBuffer * buf
void CharsetConvertException *size_t n
Definition SQLField.cpp:253
virtual String toString() const
Definition Exception.cpp:40
virtual void destroy()
Definition Exception.cpp:74
virtual String toString() const
Definition Object.cpp:187
CharBuffer * __buf
size_t __capacity
size_t __MAX(size_t x, size_t y)
Definition size_t.h:43