1 /*
2  * ByteArrayBinaryOutputPort.cpp -
3  *
4  *   Copyright (c) 2009  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: ByteArrayBinaryOutputPort.cpp 1331 2009-03-13 08:27:20Z higepon $
30  */
31 
32 #include <string.h> // memcpy
33 #include "Object.h"
34 #include "Object-inl.h"
35 #include "Pair.h"
36 #include "Pair-inl.h"
37 #include "ByteArrayBinaryOutputPort.h"
38 #include "ByteVector.h"
39 #include "Symbol.h"
40 #include "Bignum.h"
41 #include "ErrorProcedures.h"
42 #include "PortProcedures.h"
43 
44 using namespace scheme;
45 
ByteArrayBinaryOutputPort()46 ByteArrayBinaryOutputPort::ByteArrayBinaryOutputPort() :
47     position_(0),
48     isClosed_(false),
49     isPseudoClosed_(false)
50 {
51 }
52 
~ByteArrayBinaryOutputPort()53 ByteArrayBinaryOutputPort::~ByteArrayBinaryOutputPort()
54 {
55 }
56 
isClosed() const57 bool ByteArrayBinaryOutputPort::isClosed() const
58 {
59     return isClosed_ || isPseudoClosed_;
60 }
61 
putU8(uint8_t v)62 int ByteArrayBinaryOutputPort::putU8(uint8_t v)
63 {
64     buffer_.resize(position_ + 1, 0);
65     buffer_[position_++] = v;
66     return 1;
67 }
68 
putU8(uint8_t * v,int64_t _size)69 int64_t ByteArrayBinaryOutputPort::putU8(uint8_t* v, int64_t _size)
70 {
71     MOSH_ASSERT(isInSize_t(_size));
72     const size_t size = static_cast<size_t>(_size);
73     buffer_.resize(position_ + size, 0);
74     for (size_t i = 0; i < size; i++) {
75         buffer_[position_ + i] = v[i];
76     }
77     position_ += size;
78     return size;
79 }
80 
putByteVector(ByteVector * bv,int64_t start)81 int64_t ByteArrayBinaryOutputPort::putByteVector(ByteVector* bv, int64_t start /* = 0 */)
82 {
83     return putByteVector(bv, start, bv->length() - start);
84 }
85 
putByteVector(ByteVector * bv,int64_t start,int64_t count)86 int64_t ByteArrayBinaryOutputPort::putByteVector(ByteVector* bv, int64_t start, int64_t count)
87 {
88     uint8_t* buf = bv->data();
89     return putU8(&buf[start], count);
90 }
91 
open()92 int ByteArrayBinaryOutputPort::open()
93 {
94     return MOSH_SUCCESS;
95 }
96 
close()97 int ByteArrayBinaryOutputPort::close()
98 {
99     isClosed_ = true;
100     return MOSH_SUCCESS;
101 }
102 
pseudoClose()103 int ByteArrayBinaryOutputPort::pseudoClose()
104 {
105     isPseudoClosed_ = true;
106     return MOSH_SUCCESS;
107 }
108 
flush()109 void ByteArrayBinaryOutputPort::flush()
110 {
111 }
112 
toString()113 ucs4string ByteArrayBinaryOutputPort::toString()
114 {
115     return UC("<bytevector-output-port>");
116 }
117 
hasPosition() const118 bool ByteArrayBinaryOutputPort::hasPosition() const
119 {
120     return true;
121 }
122 
hasSetPosition() const123 bool ByteArrayBinaryOutputPort::hasSetPosition() const
124 {
125     return true;
126 }
127 
position() const128 Object ByteArrayBinaryOutputPort::position() const
129 {
130     return Bignum::makeInteger(position_);
131 }
132 
setPosition(int64_t position)133 bool ByteArrayBinaryOutputPort::setPosition(int64_t position)
134 {
135     if (position >= 0) {
136         MOSH_ASSERT(isInSize_t(position));
137         position_ = static_cast<uintptr_t>(position);
138         return true;
139     } else {
140         return false;
141     }
142 }
143 
toByteVector()144 ByteVector* ByteArrayBinaryOutputPort::toByteVector()
145 {
146     ByteVector* ret = new ByteVector(buffer_);
147     buffer_.clear();
148     return ret;
149 }
150 
getFile()151 File* ByteArrayBinaryOutputPort::getFile()
152 {
153     return NULL;
154 }
155