From 675646f42ffedba4c7827c050d40a46ab6b10caf Mon Sep 17 00:00:00 2001 From: David Tardon Date: Sun, 13 Mar 2016 10:07:14 +0100 Subject: add iterator adapter for stream --- configure.ac | 1 + src/lib/Makefile.am | 2 ++ src/lib/SW602StreamIterator.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ src/lib/SW602StreamIterator.h | 45 +++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 src/lib/SW602StreamIterator.cpp create mode 100644 src/lib/SW602StreamIterator.h diff --git a/configure.ac b/configure.ac index 49e9b52..0ff3164 100644 --- a/configure.ac +++ b/configure.ac @@ -51,6 +51,7 @@ AC_SUBST(REVENGE_LIBS) # Find boost headers # ================== AC_CHECK_HEADERS( + boost/iterator/iterator_facade.hpp \ boost/optional.hpp \ boost/scoped_ptr.hpp \ boost/shared_ptr.hpp \ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 33159c6..66efcdd 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -64,6 +64,8 @@ libsw602_@SW602_MAJOR_VERSION@_@SW602_MINOR_VERSION@_la_SOURCES = \ SW602SpreadsheetEncoder.h \ SW602SpreadsheetListener.cpp \ SW602SpreadsheetListener.h \ + SW602StreamIterator.cpp \ + SW602StreamIterator.h \ SW602SubDocument.cpp \ SW602SubDocument.h \ SW602Table.cpp \ diff --git a/src/lib/SW602StreamIterator.cpp b/src/lib/SW602StreamIterator.cpp new file mode 100644 index 0000000..c88d6e9 --- /dev/null +++ b/src/lib/SW602StreamIterator.cpp @@ -0,0 +1,64 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libsw602 project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "SW602StreamIterator.h" + +#include + +#include "libsw602_utils.h" + +namespace libsw602 +{ + +SW602StreamIterator::SW602StreamIterator() + : m_input(0) + , m_pos(0) + , m_current('\0') +{ +} + +SW602StreamIterator::SW602StreamIterator(librevenge::RVNGInputStream &input) + : m_input(&input) + , m_pos(input.tell()) + , m_current('\0') +{ + read(); +} + +SW602StreamIterator::reference SW602StreamIterator::dereference() const +{ + assert(m_input); + return m_current; +} + +bool SW602StreamIterator::equal(const SW602StreamIterator &other) const +{ + return (m_input == other.m_input) && (!m_input || (m_pos == other.m_pos)); +} + +void SW602StreamIterator::increment() +{ + assert(m_input); + ++m_pos; + read(); +} + +void SW602StreamIterator::read() +{ + if (m_input->tell() != m_pos) + m_input->seek(m_pos, librevenge::RVNG_SEEK_SET); + if (m_input->isEnd()) + m_input = 0; + else + m_current = char(readU8(*m_input)); +} + +} + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/lib/SW602StreamIterator.h b/src/lib/SW602StreamIterator.h new file mode 100644 index 0000000..7ad39f1 --- /dev/null +++ b/src/lib/SW602StreamIterator.h @@ -0,0 +1,45 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libsw602 project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_SW602_STREAMITERATOR_H +#define INCLUDED_SW602_STREAMITERATOR_H + +#include + +#include + +namespace libsw602 +{ + +class SW602StreamIterator : public boost::iterator_facade +{ + friend class boost::iterator_core_access; + +public: + SW602StreamIterator(); + explicit SW602StreamIterator(librevenge::RVNGInputStream &input); + +private: + reference dereference() const; + bool equal(const SW602StreamIterator &other) const; + void increment(); + + void read(); + +private: + librevenge::RVNGInputStream *m_input; + long m_pos; + char m_current; +}; + +} + +#endif + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ -- cgit v1.2.3