
Module: ppl/ppl Branch: master Commit: 8764c47130fdceb681e539b9aa58b0767cb38bd3 URL: http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=8764c47130fdc...
Author: Abramo Bagnara abramo.bagnara@gmail.com Date: Sat Mar 21 12:17:48 2009 +0100
Added callback based streambufs.
---
src/Makefile.am | 4 ++ src/c_streambuf.cc | 101 ++++++++++++++++++++++++++++++++++++ src/c_streambuf.defs.hh | 121 ++++++++++++++++++++++++++++++++++++++++++++ src/c_streambuf.inlines.hh | 35 +++++++++++++ src/c_streambuf.types.hh | 21 ++++++++ 5 files changed, 282 insertions(+), 0 deletions(-)
diff --git a/src/Makefile.am b/src/Makefile.am index 83a2ad6..cc3fc03 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -123,6 +123,7 @@ Checked_Number.types.hh \ $(COEFFICIENT_TYPES_INCLUDE_FILES) \ Coefficient.types.hh \ stdiobuf.types.hh \ +c_streambuf.types.hh \ globals.types.hh \ iterator_to_const.types.hh \ distances.types.hh \ @@ -194,6 +195,8 @@ Coefficient.defs.hh \ Coefficient.inlines.hh \ stdiobuf.defs.hh \ stdiobuf.inlines.hh \ +c_streambuf.defs.hh \ +c_streambuf.inlines.hh \ globals.defs.hh \ globals.inlines.hh \ math_utilities.defs.hh \ @@ -393,6 +396,7 @@ simplify.cc \ Grid_conversion.cc \ Grid_simplify.cc \ stdiobuf.cc \ +c_streambuf.cc \ globals.cc \ mp_std_bits.cc \ version.cc \ diff --git a/src/c_streambuf.cc b/src/c_streambuf.cc new file mode 100644 index 0000000..f8a8065 --- /dev/null +++ b/src/c_streambuf.cc @@ -0,0 +1,101 @@ +/* c_streambuf class implementation (non-inline functions). + Copyright (C) 2001-2009 Roberto Bagnara bagnara@cs.unipr.it + +This file is part of the Parma Polyhedra Library (PPL). + +The PPL is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +The PPL is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software Foundation, +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. + +For the most up-to-date information see the Parma Polyhedra Library +site: http://www.cs.unipr.it/ppl/ . */ + +#include <ppl-config.h> + +#include "c_streambuf.defs.hh" + +namespace Parma_Polyhedra_Library { + +c_streambuf::int_type +c_streambuf::uflow() { + int_type c = underflow(); + nextc_buf = traits_type::eof(); + return c; +} + +c_streambuf::int_type +c_streambuf::underflow() { + const int_type eof = traits_type::eof(); + if (traits_type::eq_int_type(nextc_buf, eof)) { + char buf; + if (read(data, &buf, 1) != 1) + nextc_buf = eof; + else + nextc_buf = buf; + } + return nextc_buf; +} + +std::streamsize +c_streambuf::xsgetn(char_type* s, std::streamsize n) { + if (n == 0) + return n; + const int_type eof = traits_type::eof(); + int a; + if (traits_type::eq_int_type(nextc_buf, eof)) + a = 0; + else { + s[0] = nextc_buf; + a = 1; + } + std::streamsize r = read(data, s + a, n - a) + a; + if (r > 0) + ungetc_buf = traits_type::to_int_type(s[r - 1]); + else + ungetc_buf = traits_type::eof(); + return r; +} + +c_streambuf::int_type +c_streambuf::pbackfail(int_type c) { + const int_type eof = traits_type::eof(); + nextc_buf = traits_type::eq_int_type(c, eof) ? ungetc_buf : c; + ungetc_buf = eof; + return nextc_buf; +} + +std::streamsize +c_streambuf::xsputn(const char_type* s, std::streamsize n) { + return write(data, s, n); +} + +c_streambuf::int_type +c_streambuf::overflow(int_type c) { + const int_type eof = traits_type::eof(); + if (traits_type::eq_int_type(c, eof)) + return sync() ? eof : traits_type::not_eof(c); + else { + char buf = c; + if (write(data, &buf, 1) != 1) + return eof; + else + return c; + } +} + +int +c_streambuf::sync() { + return 0; +} + +} // namespace Parma_Polyhedra_Library diff --git a/src/c_streambuf.defs.hh b/src/c_streambuf.defs.hh new file mode 100644 index 0000000..d58d918 --- /dev/null +++ b/src/c_streambuf.defs.hh @@ -0,0 +1,121 @@ +/* c_streambuf class declaration. + Copyright (C) 2001-2009 Roberto Bagnara bagnara@cs.unipr.it + +This file is part of the Parma Polyhedra Library (PPL). + +The PPL is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +The PPL is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software Foundation, +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. + +For the most up-to-date information see the Parma Polyhedra Library +site: http://www.cs.unipr.it/ppl/ . */ + +#ifndef PPL_c_streambuf_defs_hh +#define PPL_c_streambuf_defs_hh 1 + +#include "c_streambuf.types.hh" +#include <streambuf> + +typedef size_t (*read_function)(void* data, void *buf, size_t size); +typedef size_t (*write_function)(void* data, const void *buf, size_t size); + +class Parma_Polyhedra_Library::c_streambuf + : public std::basic_streambuf<char, std::char_traits<char> > { +public: + //! Constructor. + c_streambuf(read_function read, write_function write, void *data); + +protected: + /*! \brief + Gets a character in case of underflow. + + \remarks + Specified by ISO/IEC 14882:1998: 27.5.2.4.3. + */ + int_type underflow(); + + /*! \brief + In case of underflow, gets a character and advances the next pointer. + + \remarks + Specified by ISO/IEC 14882:1998: 27.5.2.4.3. + */ + int_type uflow(); + + /*! \brief + Gets a sequence of characters. + + \remarks + Specified by ISO/IEC 14882:1998: 27.5.2.4.3. + */ + std::streamsize xsgetn(char_type* s, std::streamsize n); + + /*! \brief + Puts character back in case of backup underflow. + + \remarks + Specified by ISO/IEC 14882:1998: 27.5.2.4.4. + */ + int_type pbackfail(int_type c = traits_type::eof()); + + /*! \brief + Writes a sequence of characters. + + \remarks + Specified by ISO/IEC 14882:1998: 27.5.2.4.5. + */ + std::streamsize xsputn(const char_type* s, std::streamsize n); + + /*! \brief + Writes a character in case of overflow. + + Specified by ISO/IEC 14882:1998: 27.5.2.4.5. + */ + int_type overflow(int_type c); + + /*! \brief + Synchronizes the stream buffer. + + Specified by ISO/IEC 14882:1998: 27.5.2.4.2. + */ + int sync(); + +private: + //! Character type of the streambuf. + typedef char char_type; + + //! Traits type of the streambuf. + typedef std::char_traits<char_type> traits_type; + + //! Integer type of the streambuf. + typedef traits_type::int_type int_type; + + //! The read callback + read_function read; + + //! The write callback + write_function write; + + //! The callback data + void *data; + + //! Buffer for the last character read. + int_type ungetc_buf; + + //! Buffer for next character + int_type nextc_buf; +}; + +#include "c_streambuf.inlines.hh" + +#endif // !defined(PPL_c_streambuf_defs_hh) diff --git a/src/c_streambuf.inlines.hh b/src/c_streambuf.inlines.hh new file mode 100644 index 0000000..709d873 --- /dev/null +++ b/src/c_streambuf.inlines.hh @@ -0,0 +1,35 @@ +/* c_streambuf class implementation: inline functions. + Copyright (C) 2001-2009 Roberto Bagnara bagnara@cs.unipr.it + +This file is part of the Parma Polyhedra Library (PPL). + +The PPL is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +The PPL is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software Foundation, +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. + +For the most up-to-date information see the Parma Polyhedra Library +site: http://www.cs.unipr.it/ppl/ . */ + +#ifndef PPL_c_streambuf_inlines_hh +#define PPL_c_streambuf_inlines_hh 1 + +namespace Parma_Polyhedra_Library { + +inline +c_streambuf::c_streambuf(read_function read, write_function write, void* data) + : read(read), write(write), data(data), ungetc_buf(traits_type::eof()) { +} + +} // namespace Parma_Polyhedra_Library + +#endif // !defined(PPL_c_streambuf_inlines_hh) diff --git a/src/c_streambuf.types.hh b/src/c_streambuf.types.hh new file mode 100644 index 0000000..c9aea28 --- /dev/null +++ b/src/c_streambuf.types.hh @@ -0,0 +1,21 @@ +/* Copyright (C) 2001-2009 Roberto Bagnara bagnara@cs.unipr.it + +This file is free software; as a special exception the author gives +unlimited permission to copy and/or distribute it, with or without +modifications, as long as this notice is preserved. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY, to the extent permitted by law; without even the +implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. */ + +#ifndef PPL_c_streambuf_types_hh +#define PPL_c_streambuf_types_hh 1 + +namespace Parma_Polyhedra_Library { + +class c_streambuf; + +} // namespace Parma_Polyhedra_Library + +#endif // !defined(PPL_c_streambuf_types_hh)