1 /*
2  * FileBinaryOutputPort.cpp -
3  *
4  *   Copyright (c) 2008  Higepon(Taro Minowa)  <higepon@users.sourceforge.jp>
5  *   Copyright (c) 2009  Kokosabu(MIURA Yasuyuki)  <kokosabu@gmail.com>
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *   1. Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *
14  *   2. Redistributions in binary form must reproduce the above copyright
15  *      notice, this list of conditions and the following disclaimer in the
16  *      documentation and/or other materials provided with the distribution.
17  *
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *  $Id$
31  */
32 
33 // #ifdef _WIN32
34 //     #include <io.h>
35 // #else
36 // #include <unistd.h>
37 // #endif
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <string.h> // memcpy
42 #include "Object.h"
43 #include "Object-inl.h"
44 #include "Pair.h"
45 #include "Pair-inl.h"
46 #include "FileBinaryOutputPort.h"
47 #include "ByteVector.h"
48 #include "Symbol.h"
49 #include "Bignum.h"
50 #include "ErrorProcedures.h"
51 #include "OSCompat.h"
52 
53 using namespace scheme;
54 
FileBinaryOutputPort(File * file)55 FileBinaryOutputPort::FileBinaryOutputPort(File* file) : file_(file), fileName_(UC("<unknown file>")), isClosed_(false), isPseudoClosed_(false), position_(0)
56 {
57 }
58 
FileBinaryOutputPort(ucs4string file)59 FileBinaryOutputPort::FileBinaryOutputPort(ucs4string file) : file_(new File), fileName_(file), isClosed_(false), isPseudoClosed_(false), position_(0)
60 {
61     file_->open(file, File::Read | File::Write | File::Create);
62 }
63 
FileBinaryOutputPort(ucs4string file,int openFlags)64 FileBinaryOutputPort::FileBinaryOutputPort(ucs4string file, int openFlags) : file_(new File), fileName_(file), isClosed_(false), isPseudoClosed_(), position_(0)
65 {
66     file_->open(file, File::Read | File::Write | File::Create | openFlags);
67 }
68 
~FileBinaryOutputPort()69 FileBinaryOutputPort::~FileBinaryOutputPort()
70 {
71     close();
72 }
73 
isClosed() const74 bool FileBinaryOutputPort::isClosed() const
75 {
76     return isClosed_ || isPseudoClosed_;
77 }
78 
pseudoClose()79 int FileBinaryOutputPort::pseudoClose()
80 {
81     isPseudoClosed_ = true;
82     return MOSH_SUCCESS;
83 }
84 
putU8(uint8_t v)85 int FileBinaryOutputPort::putU8(uint8_t v)
86 {
87     return static_cast<int>(putU8(&v, 1));
88 }
89 
putU8(uint8_t * v,int64_t size)90 int64_t FileBinaryOutputPort::putU8(uint8_t* v, int64_t size)
91 {
92     return file_->write(v, size);
93 }
94 
putByteVector(ByteVector * bv,int64_t start)95 int64_t FileBinaryOutputPort::putByteVector(ByteVector* bv, int64_t start /* = 0 */)
96 {
97     return putByteVector(bv, start, bv->length() - start);
98 }
99 
putByteVector(ByteVector * bv,int64_t start,int64_t count)100 int64_t FileBinaryOutputPort::putByteVector(ByteVector* bv, int64_t start, int64_t count)
101 {
102     uint8_t* buf = bv->data();
103     return file_->write(&buf[start], count);
104 }
105 
open()106 int FileBinaryOutputPort::open()
107 {
108     if (file_->isOpen()) {
109         return MOSH_SUCCESS;
110     } else {
111         return MOSH_FAILURE;
112     }
113 }
114 
close()115 int FileBinaryOutputPort::close()
116 {
117     if (!isClosed_) {
118         isClosed_ = true;
119         file_->close();
120     }
121     return MOSH_SUCCESS;
122 }
123 
getFile()124 File* FileBinaryOutputPort::getFile()
125 {
126     return file_;
127 }
128 
flush()129 void FileBinaryOutputPort::flush()
130 {
131 }
132 
toString()133 ucs4string FileBinaryOutputPort::toString()
134 {
135     ucs4string ret = UC("<binary-output-port ");
136     ret += fileName_;
137     ret += UC(">");
138     return ret;
139 }
140 
hasPosition() const141 bool FileBinaryOutputPort::hasPosition() const
142 {
143     return true;
144 }
145 
hasSetPosition() const146 bool FileBinaryOutputPort::hasSetPosition() const
147 {
148     return true;
149 }
150 
position() const151 Object FileBinaryOutputPort::position() const
152 {
153     return Bignum::makeIntegerFromS64(position_);
154 }
155 
setPosition(int64_t position)156 bool FileBinaryOutputPort::setPosition(int64_t position)
157 {
158     const int64_t ret = file_->seek(position);
159     if (position == ret) {
160         position_ = position;
161         return true;
162     } else {
163         return false;
164     }
165 }
166 
167 
168