From 2aa471e14cd1ad9a2066159925cdb998fe9eae05 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Fri, 21 Dec 2012 23:46:20 +0100 Subject: digital/ucoolib/ucoolib/base/stdio: add stdio functions --- digital/ucoolib/ucoolib/base/stdio/Module | 1 + digital/ucoolib/ucoolib/base/stdio/stdio.cc | 68 +++++++++++++++++++++++++++++ digital/ucoolib/ucoolib/base/stdio/stdio.hh | 39 +++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 digital/ucoolib/ucoolib/base/stdio/Module create mode 100644 digital/ucoolib/ucoolib/base/stdio/stdio.cc create mode 100644 digital/ucoolib/ucoolib/base/stdio/stdio.hh (limited to 'digital') diff --git a/digital/ucoolib/ucoolib/base/stdio/Module b/digital/ucoolib/ucoolib/base/stdio/Module new file mode 100644 index 00000000..f9cffeb2 --- /dev/null +++ b/digital/ucoolib/ucoolib/base/stdio/Module @@ -0,0 +1 @@ +base_stdio_SOURCES := stdio.cc diff --git a/digital/ucoolib/ucoolib/base/stdio/stdio.cc b/digital/ucoolib/ucoolib/base/stdio/stdio.cc new file mode 100644 index 00000000..051f226e --- /dev/null +++ b/digital/ucoolib/ucoolib/base/stdio/stdio.cc @@ -0,0 +1,68 @@ +// ucoolib - Microcontroller object oriented library. {{{ +// +// Copyright (C) 2012 Nicolas Schodet +// +// APBTeam: +// Web: http://apbteam.org/ +// Email: team AT apbteam DOT org +// +// This program 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 2 of the License, or +// (at your option) any later version. +// +// This program 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "stdio.hh" + +#include + +namespace ucoo { + +/// Callback to read a stream. +ssize_t +stream_io_read (void *cookie, char *buf, size_t n) +{ + Stream *stream = static_cast (cookie); + int r = stream->read (buf, n); + if (r == -2) + return 0; + else + return r; +} + +/// Callback to write a stream. +ssize_t +stream_io_write (void *cookie, const char *buf, size_t n) +{ + Stream *stream = static_cast (cookie); + int r = stream->write (buf, n); + return r; +} + +/// Table of callback for fopencookie. +static const cookie_io_functions_t stream_io_functions = +{ + stream_io_read, + stream_io_write, + NULL, + NULL, +}; + +FILE * +fstreamopen (Stream &stream, const char *mode) +{ + // fopencookie is a glibc extension also implemented in newlib, nice! + return fopencookie (static_cast (&stream), mode, + stream_io_functions); +} + +} // namespace ucoo diff --git a/digital/ucoolib/ucoolib/base/stdio/stdio.hh b/digital/ucoolib/ucoolib/base/stdio/stdio.hh new file mode 100644 index 00000000..f4b05f54 --- /dev/null +++ b/digital/ucoolib/ucoolib/base/stdio/stdio.hh @@ -0,0 +1,39 @@ +#ifndef ucoolib_base_stdio_stdio_hh +#define ucoolib_base_stdio_stdio_hh +// ucoolib - Microcontroller object oriented library. {{{ +// +// Copyright (C) 2012 Nicolas Schodet +// +// APBTeam: +// Web: http://apbteam.org/ +// Email: team AT apbteam DOT org +// +// This program 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 2 of the License, or +// (at your option) any later version. +// +// This program 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "ucoolib/intf/stream.hh" + +#include + +namespace ucoo { + +/// Open a stdio stream connecter to STREAM. The MODE parameter uses the same +/// syntax as stdio fopen. +FILE * +fstreamopen (Stream &stream, const char *mode); + +} // namespace ucoo + +#endif // ucoolib_base_stdio_stdio_hh -- cgit v1.2.3