1 /*
2  * StringTextualOutputPort.cpp -
3  *
4  *   Copyright (c) 2008  Higepon(Taro Minowa)  <higepon@users.sourceforge.jp>
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  $Id: StringTextualOutputPort.cpp 183 2008-07-04 06:19:28Z higepon $
30  */
31 
32 #include "Object.h"
33 #include "Object-inl.h"
34 #include "Bignum.h"
35 #include "StringTextualOutputPort.h"
36 #include "Transcoder.h"
37 #include "OSCompat.h"
38 
39 using namespace scheme;
40 
StringTextualOutputPort()41 StringTextualOutputPort::StringTextualOutputPort() : isClosed_(false), index_(0)
42 {
43 }
44 
~StringTextualOutputPort()45 StringTextualOutputPort::~StringTextualOutputPort()
46 {
47     close();
48 }
49 
putChar(ucs4char c)50 void StringTextualOutputPort::putChar(ucs4char c)
51 {
52     if (buffer_.size() > index_) {
53         buffer_[index_] = c;
54     } else {
55         buffer_ += c;
56     }
57     index_++;
58 }
59 
getString()60 ucs4string StringTextualOutputPort::getString()
61 {
62     return buffer_;
63 }
64 
reset()65 void StringTextualOutputPort::reset()
66 {
67     buffer_ = UC("");
68     index_ = 0;
69 }
70 
close()71 int StringTextualOutputPort::close()
72 {
73     isClosed_ = true;
74     return 0;
75 }
76 
isClosed() const77 bool StringTextualOutputPort::isClosed() const
78 {
79     return isClosed_;
80 }
81 
flush()82 void StringTextualOutputPort::flush()
83 {
84     return;
85 }
86 
toString()87 ucs4string StringTextualOutputPort::toString()
88 {
89     return UC("<string-output-port>");
90 }
91 
hasPosition() const92 bool StringTextualOutputPort::hasPosition() const
93 {
94     return true;
95 }
96 
hasSetPosition() const97 bool StringTextualOutputPort::hasSetPosition() const
98 {
99     return true;
100 }
101 
position() const102 Object StringTextualOutputPort::position() const
103 {
104     return Bignum::makeInteger(index_);
105 }
106 
setPosition(int64_t _position)107 bool StringTextualOutputPort::setPosition(int64_t _position)
108 {
109     MOSH_ASSERT(isInSize_t(_position));
110     const size_t position = static_cast<size_t>(_position);
111     if (position > index_) {
112         buffer_.resize(position, ' ');
113     }
114     index_ = position;
115     return true;
116 }
117 
bufferMode() const118 enum OutputPort::bufferMode StringTextualOutputPort::bufferMode() const
119 {
120     return NONE;
121 }
122 
transcoder() const123 Transcoder* StringTextualOutputPort::transcoder() const
124 {
125     return createNativeTranscoder();
126 }
127