summaryrefslogtreecommitdiffhomepage
path: root/digital/ucoolib
diff options
context:
space:
mode:
authorNicolas Schodet2012-12-21 23:46:20 +0100
committerNicolas Schodet2012-12-23 12:42:25 +0100
commit2aa471e14cd1ad9a2066159925cdb998fe9eae05 (patch)
tree6d47959e7483e806a80105ba25232a5381a54a26 /digital/ucoolib
parente28a7a19e24f57c87c9f17bb114f3f5b80e091d5 (diff)
digital/ucoolib/ucoolib/base/stdio: add stdio functions
Diffstat (limited to 'digital/ucoolib')
-rw-r--r--digital/ucoolib/ucoolib/base/stdio/Module1
-rw-r--r--digital/ucoolib/ucoolib/base/stdio/stdio.cc68
-rw-r--r--digital/ucoolib/ucoolib/base/stdio/stdio.hh39
3 files changed, 108 insertions, 0 deletions
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 <unistd.h>
+
+namespace ucoo {
+
+/// Callback to read a stream.
+ssize_t
+stream_io_read (void *cookie, char *buf, size_t n)
+{
+ Stream *stream = static_cast<Stream *> (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<Stream *> (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<void *> (&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 <cstdio>
+
+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