2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
23 #include <dpl/assert.h>
24 #include <dpl/scoped_array.h>
26 #include <Commons/Exception.h>
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::Commons;
35 namespace Filesystem {
36 Stream::Stream(const NodePtr& parent,
41 Assert(m_parent && "Stream needs to have parent.");
43 std::ios_base::openmode mode_ = std::ios_base::binary;
44 if (mode & AM_READ) { mode_ |= std::ios_base::in; }
45 if (mode & AM_WRITE) { mode_ |= std::ios_base::out; }
46 else if (mode & AM_APPEND) {
47 mode_ |= (std::ios_base::app | std::ios_base::out);
50 m_stream.open(m_parent->getPath()->getFullPath().c_str(), mode_);
52 ThrowMsg(Commons::PlatformException, "Could not open stream.");
61 IStreamPtr Stream::write(bool arg)
66 IStreamPtr Stream::write(unsigned char arg)
71 IStreamPtr Stream::write(char arg)
76 IStreamPtr Stream::write(int arg)
81 IStreamPtr Stream::write(double arg)
86 IStreamPtr Stream::write(const std::string& arg)
91 IStreamPtr Stream::read(bool& arg)
96 IStreamPtr Stream::read(unsigned char& arg)
101 IStreamPtr Stream::read(char& arg)
106 IStreamPtr Stream::read(int& arg)
111 IStreamPtr Stream::read(double& arg)
116 IStreamPtr Stream::read(std::string& arg)
121 void Stream::setCharSet(const std::string &charSet)
126 std::string Stream::getCharSet() const
131 char* Stream::getChars(std::size_t num)
135 std::size_t maxChars = num + 1;
136 DPL::ScopedArray<char> result;
138 result.Reset(new char[maxChars]);
139 memset(result.Get(), 0, maxChars);
142 Catch(std::bad_alloc) {
143 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
145 // if (m_stream.get(result.Get(), maxChars).bad()) {
146 m_stream.read(result.Get(), num);
148 if (m_stream.bad()) {
149 ThrowMsg(Commons::PlatformException,
150 "Error while reading from the stream.");
153 return result.Release();
156 unsigned char* Stream::getBytes(std::size_t num)
160 DPL::ScopedArray<char> buffer;
162 buffer.Reset(new char[num]);
164 Catch(std::bad_alloc) {
165 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
167 if (m_stream.read(buffer.Get(), num).bad()) {
168 ThrowMsg(Commons::PlatformException,
169 "Error while reading from the stream.");
172 return static_cast<unsigned char*>(static_cast<void*>(buffer.Release()));
175 std::size_t Stream::getCount() const
178 ThrowMsg(Commons::PlatformException, "Stream is closed.");
182 ThrowMsg(Commons::PlatformException, "Stream is not readable.");
185 return m_stream.gcount();
188 std::string Stream::getLine()
193 std::getline(m_stream, result);
198 bool Stream::isOpen() const
200 return m_stream.is_open();
203 bool Stream::isEof() const
205 return m_stream.eof();
212 m_parent->onStreamClose(SharedFromThis());
216 int Stream::getMode() const
221 long Stream::getPosition() const
223 return static_cast<long>(m_stream.tellg());
226 void Stream::setPosition(long position)
228 if (m_stream.rdstate() & std::ios_base::eofbit) {
231 if (!(m_stream.seekg(position)) || !(m_stream.seekp(position))) {
232 ThrowMsg(PlatformException, "Could not set position.");
236 unsigned long long Stream::getSize() const
238 std::fstream::streampos pos = m_stream.tellg();
243 if (!m_stream.seekg(0, std::_S_end)) {
247 unsigned long long result = m_stream.tellg();
248 m_stream.seekg(pos, std::_S_beg);
250 return (result == -1 ? result : result + 1);
254 IStreamPtr Stream::write_(T arg)
258 if (!(m_stream << arg)) {
259 LoggerE("Error while writing to the stream.");
260 ThrowMsg(Commons::PlatformException,
261 "Error while writing to the stream.");
265 return DPL::StaticPointerCast<IStream>(SharedFromThis());
269 IStreamPtr Stream::read_(T& arg)
273 if (!(m_stream >> arg)) {
274 ThrowMsg(Commons::PlatformException,
275 "Error while reading from the stream.");
278 return DPL::StaticPointerCast<IStream>(SharedFromThis());
281 bool Stream::isReadable() const
283 return (m_mode & AM_READ);
286 bool Stream::isWriteable() const
288 return ((m_mode & AM_WRITE) || (m_mode & AM_APPEND));
291 void Stream::checkForReading() const
294 ThrowMsg(Commons::PlatformException, "Stream is closed.");
298 ThrowMsg(Commons::PlatformException, "Stream is marked as EOF.");
302 ThrowMsg(Commons::PlatformException, "Stream is not readable.");
306 void Stream::checkForWriting() const
309 LoggerE("Stream is closed.");
310 ThrowMsg(Commons::PlatformException, "Stream is closed.");
313 if (!isWriteable()) {
314 LoggerE("Stream is not writeable.");
315 ThrowMsg(Commons::PlatformException, "Stream is not writeable.");