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 {
29 Stream::Stream(const NodePtr& parent,
34 Assert(m_parent && "Stream needs to have parent.");
36 std::ios_base::openmode mode_ = std::ios_base::binary;
38 mode_ |= std::ios_base::in;
40 if (mode & AM_WRITE) {
41 mode_ |= std::ios_base::out;
45 mode_ |= (std::ios_base::app | std::ios_base::out);
48 m_stream.open(m_parent->getPath()->getFullPath().c_str(), mode_);
50 ThrowMsg(Commons::PlatformException, "Could not open stream.");
59 IStreamPtr Stream::write(bool arg)
64 IStreamPtr Stream::write(unsigned char arg)
69 IStreamPtr Stream::write(char arg)
74 IStreamPtr Stream::write(int arg)
79 IStreamPtr Stream::write(double arg)
84 IStreamPtr Stream::write(const std::string& arg)
89 IStreamPtr Stream::read(bool& arg)
94 IStreamPtr Stream::read(unsigned char& arg)
99 IStreamPtr Stream::read(char& arg)
104 IStreamPtr Stream::read(int& arg)
109 IStreamPtr Stream::read(double& arg)
114 IStreamPtr Stream::read(std::string& arg)
119 char* Stream::getChars(std::size_t num)
123 std::size_t maxChars = num + 1;
124 DPL::ScopedArray<char> result;
126 result.Reset(new char[maxChars]);
128 Catch(std::bad_alloc) {
129 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
131 if (m_stream.get(result.Get(), maxChars).bad()) {
132 ThrowMsg(Commons::PlatformException,
133 "Error while reading from the stream.");
136 return result.Release();
139 unsigned char* Stream::getBytes(std::size_t num)
143 DPL::ScopedArray<char> buffer;
145 buffer.Reset(new char[num]);
147 Catch(std::bad_alloc) {
148 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
150 if (m_stream.read(buffer.Get(), num).bad()) {
151 ThrowMsg(Commons::PlatformException,
152 "Error while reading from the stream.");
155 return static_cast<unsigned char*>(static_cast<void*>(buffer.Release()));
158 std::size_t Stream::getCount() const
161 ThrowMsg(Commons::PlatformException, "Stream is closed.");
165 ThrowMsg(Commons::PlatformException, "Stream is not readable.");
168 return m_stream.gcount();
171 std::string Stream::getLine()
176 std::getline(m_stream, result);
181 bool Stream::isOpen() const
183 return m_stream.is_open();
186 bool Stream::isEof() const
188 return m_stream.eof();
195 m_parent->onStreamClose(SharedFromThis());
199 int Stream::getMode() const
204 long Stream::getPosition() const
206 return static_cast<long>(m_stream.tellg());
209 void Stream::setPosition(long position)
211 if (m_stream.rdstate() & std::ios_base::eofbit) {
214 if (!(m_stream.seekg(position)) || !(m_stream.seekp(position))) {
215 ThrowMsg(Commons::PlatformException, "Could not set position.");
219 long Stream::getSize() const
221 std::fstream::streampos pos = m_stream.tellg();
226 if (!m_stream.seekg(0, std::_S_end)) {
230 long result = m_stream.tellg();
231 m_stream.seekg(pos, std::_S_beg);
233 return (result == -1 ? result : result + 1);
237 IStreamPtr Stream::write_(T arg)
241 if (!(m_stream << arg)) {
242 LogError("Error while writing to the stream.");
243 ThrowMsg(Commons::PlatformException,
244 "Error while writing to the stream.");
248 return DPL::StaticPointerCast<IStream>(SharedFromThis());
252 IStreamPtr Stream::read_(T& arg)
256 if (!(m_stream >> arg)) {
257 ThrowMsg(Commons::PlatformException,
258 "Error while reading from the stream.");
261 return DPL::StaticPointerCast<IStream>(SharedFromThis());
264 bool Stream::isReadable() const
266 return (m_mode & AM_READ);
269 bool Stream::isWriteable() const
271 return ((m_mode & AM_WRITE) || (m_mode & AM_APPEND));
274 void Stream::checkForReading() const
277 ThrowMsg(Commons::PlatformException, "Stream is closed.");
281 ThrowMsg(Commons::PlatformException, "Stream is marked as EOF.");
285 ThrowMsg(Commons::PlatformException, "Stream is not readable.");
289 void Stream::checkForWriting() const
292 LogError("Stream is closed.");
293 ThrowMsg(Commons::PlatformException, "Stream is closed.");
296 if (!isWriteable()) {
297 LogError("Stream is not writeable.");
298 ThrowMsg(Commons::PlatformException, "Stream is not writeable.");