2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dpl/assert.h>
20 #include <dpl/scoped_array.h>
21 #include <Commons/Exception.h>
25 namespace WrtDeviceApis {
26 namespace Filesystem {
30 Stream::Stream(const NodePtr& parent,
35 Assert(m_parent && "Stream needs to have parent.");
37 std::ios_base::openmode mode_ = std::ios_base::binary;
38 if (mode & AM_READ) { mode_ |= std::ios_base::in; }
39 if (mode & AM_WRITE) { mode_ |= std::ios_base::out; } else if (mode &
41 mode_ |= (std::ios_base::app | std::ios_base::out);
44 m_stream.open(m_parent->getPath()->getFullPath().c_str(), mode_);
46 ThrowMsg(Commons::PlatformException, "Could not open stream.");
55 IStreamPtr Stream::write(bool arg)
60 IStreamPtr Stream::write(unsigned char arg)
65 IStreamPtr Stream::write(char arg)
70 IStreamPtr Stream::write(int arg)
75 IStreamPtr Stream::write(double arg)
80 IStreamPtr Stream::write(const std::string& arg)
85 IStreamPtr Stream::read(bool& arg)
90 IStreamPtr Stream::read(unsigned char& arg)
95 IStreamPtr Stream::read(char& arg)
100 IStreamPtr Stream::read(int& arg)
105 IStreamPtr Stream::read(double& arg)
110 IStreamPtr Stream::read(std::string& arg)
115 char* Stream::getChars(std::size_t num)
119 std::size_t maxChars = num + 1;
120 DPL::ScopedArray<char> result;
122 result.Reset(new char[maxChars]);
124 Catch(std::bad_alloc) {
125 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
127 if (m_stream.get(result.Get(), maxChars).bad()) {
128 ThrowMsg(Commons::PlatformException,
129 "Error while reading from the stream.");
132 return result.Release();
135 unsigned char* Stream::getBytes(std::size_t num)
139 DPL::ScopedArray<char> buffer;
141 buffer.Reset(new char[num]);
143 Catch(std::bad_alloc) {
144 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
146 if (m_stream.read(buffer.Get(), num).bad()) {
147 ThrowMsg(Commons::PlatformException,
148 "Error while reading from the stream.");
151 return static_cast<unsigned char*>(static_cast<void*>(buffer.Release()));
154 std::size_t Stream::getCount() const
157 ThrowMsg(Commons::PlatformException, "Stream is closed.");
161 ThrowMsg(Commons::PlatformException, "Stream is not readable.");
164 return m_stream.gcount();
167 std::string Stream::getLine()
172 std::getline(m_stream, result);
177 bool Stream::isOpen() const
179 return m_stream.is_open();
182 bool Stream::isEof() const
184 return m_stream.eof();
191 m_parent->onStreamClose(SharedFromThis());
195 int Stream::getMode() const
200 long Stream::getPosition() const
202 return static_cast<long>(m_stream.tellg());
205 void Stream::setPosition(long position)
207 if (m_stream.rdstate() & std::ios_base::eofbit) {
210 if (!(m_stream.seekg(position)) || !(m_stream.seekp(position))) {
211 ThrowMsg(Commons::PlatformException, "Could not set position.");
215 long Stream::getSize() const
217 std::fstream::streampos pos = m_stream.tellg();
222 if (!m_stream.seekg(0, std::_S_end)) {
226 long result = m_stream.tellg();
227 m_stream.seekg(pos, std::_S_beg);
229 return (result == -1 ? result : result + 1);
233 IStreamPtr Stream::write_(T arg)
237 if (!(m_stream << arg)) {
238 LogError("Error while writing to the stream.");
239 ThrowMsg(Commons::PlatformException,
240 "Error while writing to the stream.");
244 return DPL::StaticPointerCast<IStream>(SharedFromThis());
248 IStreamPtr Stream::read_(T& arg)
252 if (!(m_stream >> arg)) {
253 ThrowMsg(Commons::PlatformException,
254 "Error while reading from the stream.");
257 return DPL::StaticPointerCast<IStream>(SharedFromThis());
260 bool Stream::isReadable() const
262 return (m_mode & AM_READ);
265 bool Stream::isWriteable() const
267 return ((m_mode & AM_WRITE) || (m_mode & AM_APPEND));
270 void Stream::checkForReading() const
273 ThrowMsg(Commons::PlatformException, "Stream is closed.");
277 ThrowMsg(Commons::PlatformException, "Stream is marked as EOF.");
281 ThrowMsg(Commons::PlatformException, "Stream is not readable.");
285 void Stream::checkForWriting() const
288 LogError("Stream is closed.");
289 ThrowMsg(Commons::PlatformException, "Stream is closed.");
292 if (!isWriteable()) {
293 LogError("Stream is not writeable.");
294 ThrowMsg(Commons::PlatformException, "Stream is not writeable.");