summaryrefslogtreecommitdiff
path: root/wearable_src/Filesystem/Stream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'wearable_src/Filesystem/Stream.cpp')
-rw-r--r--wearable_src/Filesystem/Stream.cpp317
1 files changed, 317 insertions, 0 deletions
diff --git a/wearable_src/Filesystem/Stream.cpp b/wearable_src/Filesystem/Stream.cpp
new file mode 100644
index 0000000..2660193
--- /dev/null
+++ b/wearable_src/Filesystem/Stream.cpp
@@ -0,0 +1,317 @@
+//
+// Tizen Web Device API
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+
+#include "Stream.h"
+
+#include <stdio.h>
+#include <new>
+#include <dpl/scoped_array.h>
+
+#include <Commons/Exception.h>
+#include "Manager.h"
+#include "Node.h"
+#include <Logger.h>
+
+using namespace WrtDeviceApis;
+using namespace WrtDeviceApis::Commons;
+
+namespace DeviceAPI {
+namespace Filesystem {
+Stream::Stream(const NodePtr& parent,
+ int mode) :
+ m_parent(parent),
+ m_mode(mode)
+{
+
+ std::ios_base::openmode mode_ = std::ios_base::binary;
+ if (mode & AM_READ) { mode_ |= std::ios_base::in; }
+ if (mode & AM_WRITE) { mode_ |= std::ios_base::out; }
+ else if (mode & AM_APPEND) {
+ mode_ |= (std::ios_base::app | std::ios_base::out);
+ }
+
+ m_stream.open(m_parent->getPath()->getFullPath().c_str(), mode_);
+ if (!m_stream) {
+ ThrowMsg(Commons::PlatformException, "Could not open stream.");
+ }
+}
+
+Stream::~Stream()
+{
+ close();
+}
+
+IStreamPtr Stream::write(bool arg)
+{
+ return write_(arg);
+}
+
+IStreamPtr Stream::write(unsigned char arg)
+{
+ return write_(arg);
+}
+
+IStreamPtr Stream::write(char arg)
+{
+ return write_(arg);
+}
+
+IStreamPtr Stream::write(int arg)
+{
+ return write_(arg);
+}
+
+IStreamPtr Stream::write(double arg)
+{
+ return write_(arg);
+}
+
+IStreamPtr Stream::write(const std::string& arg)
+{
+ return write_(arg);
+}
+
+IStreamPtr Stream::read(bool& arg)
+{
+ return read_(arg);
+}
+
+IStreamPtr Stream::read(unsigned char& arg)
+{
+ return read_(arg);
+}
+
+IStreamPtr Stream::read(char& arg)
+{
+ return read_(arg);
+}
+
+IStreamPtr Stream::read(int& arg)
+{
+ return read_(arg);
+}
+
+IStreamPtr Stream::read(double& arg)
+{
+ return read_(arg);
+}
+
+IStreamPtr Stream::read(std::string& arg)
+{
+ return read_(arg);
+}
+
+void Stream::setCharSet(const std::string &charSet)
+{
+ m_charSet = charSet;
+}
+
+std::string Stream::getCharSet() const
+{
+ return m_charSet;
+}
+
+char* Stream::getChars(std::size_t num)
+{
+ checkForReading();
+
+ std::size_t maxChars = num + 1;
+ DPL::ScopedArray<char> result;
+ Try {
+ result.Reset(new char[maxChars]);
+ memset(result.Get(), 0, maxChars);
+
+ }
+ Catch(std::bad_alloc) {
+ ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
+ }
+// if (m_stream.get(result.Get(), maxChars).bad()) {
+ m_stream.read(result.Get(), num);
+
+ if (m_stream.bad()) {
+ ThrowMsg(Commons::PlatformException,
+ "Error while reading from the stream.");
+ }
+
+ return result.Release();
+}
+
+unsigned char* Stream::getBytes(std::size_t num)
+{
+ checkForReading();
+
+ DPL::ScopedArray<char> buffer;
+ Try {
+ buffer.Reset(new char[num]);
+ }
+ Catch(std::bad_alloc) {
+ ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
+ }
+ if (m_stream.read(buffer.Get(), num).bad()) {
+ ThrowMsg(Commons::PlatformException,
+ "Error while reading from the stream.");
+ }
+
+ return static_cast<unsigned char*>(static_cast<void*>(buffer.Release()));
+}
+
+std::size_t Stream::getCount() const
+{
+ if (!isOpen()) {
+ ThrowMsg(Commons::PlatformException, "Stream is closed.");
+ }
+
+ if (!isReadable()) {
+ ThrowMsg(Commons::PlatformException, "Stream is not readable.");
+ }
+
+ return m_stream.gcount();
+}
+
+std::string Stream::getLine()
+{
+ checkForReading();
+
+ std::string result;
+ std::getline(m_stream, result);
+
+ return result;
+}
+
+bool Stream::isOpen() const
+{
+ return m_stream.is_open();
+}
+
+bool Stream::isEof() const
+{
+ return m_stream.eof();
+}
+
+void Stream::close()
+{
+ if (isOpen()) {
+ m_stream.close();
+ m_parent->onStreamClose(SharedFromThis());
+ }
+}
+
+int Stream::getMode() const
+{
+ return m_mode;
+}
+
+long Stream::getPosition() const
+{
+ return static_cast<long>(m_stream.tellg());
+}
+
+void Stream::setPosition(long position)
+{
+ if (m_stream.rdstate() & std::ios_base::eofbit) {
+ m_stream.clear();
+ }
+ if (!(m_stream.seekg(position)) || !(m_stream.seekp(position))) {
+ ThrowMsg(PlatformException, "Could not set position.");
+ }
+}
+
+unsigned long long Stream::getSize() const
+{
+ std::fstream::streampos pos = m_stream.tellg();
+ if (pos == -1) {
+ return 0;
+ }
+
+ if (!m_stream.seekg(0, std::_S_end)) {
+ return 0;
+ }
+
+ unsigned long long result = m_stream.tellg();
+ m_stream.seekg(pos, std::_S_beg);
+
+ return result + 1;
+}
+
+template<typename T>
+IStreamPtr Stream::write_(T arg)
+{
+ checkForWriting();
+
+ if (!(m_stream << arg)) {
+ LoggerE("Error while writing to the stream.");
+ ThrowMsg(Commons::PlatformException,
+ "Error while writing to the stream.");
+ }
+ m_stream.flush();
+
+ return DPL::StaticPointerCast<IStream>(SharedFromThis());
+}
+
+template<typename T>
+IStreamPtr Stream::read_(T& arg)
+{
+ checkForReading();
+
+ if (!(m_stream >> arg)) {
+ ThrowMsg(Commons::PlatformException,
+ "Error while reading from the stream.");
+ }
+
+ return DPL::StaticPointerCast<IStream>(SharedFromThis());
+}
+
+bool Stream::isReadable() const
+{
+ return (m_mode & AM_READ);
+}
+
+bool Stream::isWriteable() const
+{
+ return ((m_mode & AM_WRITE) || (m_mode & AM_APPEND));
+}
+
+void Stream::checkForReading() const
+{
+ if (!isOpen()) {
+ ThrowMsg(Commons::PlatformException, "Stream is closed.");
+ }
+
+ if (isEof()) {
+ ThrowMsg(Commons::PlatformException, "Stream is marked as EOF.");
+ }
+
+ if (!isReadable()) {
+ ThrowMsg(Commons::PlatformException, "Stream is not readable.");
+ }
+}
+
+void Stream::checkForWriting() const
+{
+ if (!isOpen()) {
+ LoggerE("Stream is closed.");
+ ThrowMsg(Commons::PlatformException, "Stream is closed.");
+ }
+
+ if (!isWriteable()) {
+ LoggerE("Stream is not writeable.");
+ ThrowMsg(Commons::PlatformException, "Stream is not writeable.");
+ }
+}
+} // Filesystem
+} // TizenApis \ No newline at end of file