From f62084035feaf8b9a22804821b22e2a3c15e6eda Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 14 May 2011 15:21:04 +0000 Subject: Implemented line-oriented buffering for std output git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3606 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/lib/stdio/Make.defs | 5 ++ nuttx/lib/stdio/lib_libnoflush.c | 103 ++++++++++++++++++++++++++++++++++++ nuttx/lib/stdio/lib_libvsprintf.c | 13 +++++ nuttx/lib/stdio/lib_lowinstream.c | 4 +- nuttx/lib/stdio/lib_lowoutstream.c | 7 ++- nuttx/lib/stdio/lib_memoutstream.c | 11 ++-- nuttx/lib/stdio/lib_nulloutstream.c | 7 ++- nuttx/lib/stdio/lib_rawoutstream.c | 9 ++-- nuttx/lib/stdio/lib_stdoutstream.c | 25 +++++++-- 9 files changed, 168 insertions(+), 16 deletions(-) create mode 100644 nuttx/lib/stdio/lib_libnoflush.c (limited to 'nuttx/lib/stdio') diff --git a/nuttx/lib/stdio/Make.defs b/nuttx/lib/stdio/Make.defs index 9478369f3..fe6e106bb 100644 --- a/nuttx/lib/stdio/Make.defs +++ b/nuttx/lib/stdio/Make.defs @@ -51,6 +51,11 @@ STDIO_SRCS += lib_fopen.c lib_fclose.c lib_fread.c lib_libfread.c lib_fseek.c \ lib_fprintf.c lib_vfprintf.c lib_stdinstream.c lib_stdoutstream.c endif endif + ifeq ($(CONFIG_LIBC_FLOATINGPOINT),y) STDIO_SRCS += lib_dtoa.c endif + +ifeq ($(CONFIG_STDIO_LINEBUFFER),y) +STDIO_SRCS += lib_libnoflush.c +endif diff --git a/nuttx/lib/stdio/lib_libnoflush.c b/nuttx/lib/stdio/lib_libnoflush.c new file mode 100644 index 000000000..90e5f9f5c --- /dev/null +++ b/nuttx/lib/stdio/lib_libnoflush.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * lib/stdio/lib_libnoflush.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include + +#include "lib_internal.h" + +#ifdef CONFIG_STDIO_LINEBUFFER + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Global Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Global Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_noflush + * + * Description: + * lib_noflush() provides a common, dummy flush method for output streams + * that are not flushable. Only used if CONFIG_STDIO_LINEBUFFER is selected. + * + * Return: + * Always returns OK + * + ****************************************************************************/ + +int lib_noflush(FAR struct lib_outstream_s *this) +{ + return OK; +} + +#endif /* CONFIG_STDIO_LINEBUFFER */ + diff --git a/nuttx/lib/stdio/lib_libvsprintf.c b/nuttx/lib/stdio/lib_libvsprintf.c index 24591d875..8e0709ec9 100644 --- a/nuttx/lib/stdio/lib_libvsprintf.c +++ b/nuttx/lib/stdio/lib_libvsprintf.c @@ -1127,7 +1127,20 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, const char *src, va_list ap) if (*src != '%') { + /* Output the character */ + obj->put(obj, *src); + + /* Flush the buffer if a newline is encountered */ + +#ifdef CONFIG_STDIO_LINEBUFFER + if (*src == '\n') + { + (void)obj->flush(obj); + } +#endif + /* Process the next character in the format */ + continue; } diff --git a/nuttx/lib/stdio/lib_lowinstream.c b/nuttx/lib/stdio/lib_lowinstream.c index 85f916acb..6dcc4a37e 100644 --- a/nuttx/lib/stdio/lib_lowinstream.c +++ b/nuttx/lib/stdio/lib_lowinstream.c @@ -55,7 +55,7 @@ * Name: lowinstream_getc ****************************************************************************/ -static int lowinstream_getc(FAR struct lib_outstream_s *this) +static int lowinstream_getc(FAR struct lib_instream_s *this) { if (this && up_getc(ch) != EOF) { @@ -82,7 +82,7 @@ static int lowinstream_getc(FAR struct lib_outstream_s *this) * ****************************************************************************/ -void lib_lowinstream(FAR struct lib_outstream_s *stream) +void lib_lowinstream(FAR struct lib_instream_s *stream) { stream->get = lowinstream_getc; stream->nget = 0; diff --git a/nuttx/lib/stdio/lib_lowoutstream.c b/nuttx/lib/stdio/lib_lowoutstream.c index 2ab7c9f67..3b3d467b2 100644 --- a/nuttx/lib/stdio/lib_lowoutstream.c +++ b/nuttx/lib/stdio/lib_lowoutstream.c @@ -84,8 +84,11 @@ static void lowoutstream_putc(FAR struct lib_outstream_s *this, int ch) void lib_lowoutstream(FAR struct lib_outstream_s *stream) { - stream->put = lowoutstream_putc; - stream->nput = 0; + stream->put = lowoutstream_putc; +#ifdef CONFIG_STDIO_LINEBUFFER + stream->flush = lib_noflush; +#endif + stream->nput = 0; } #endif /* CONFIG_ARCH_LOWPUTC */ diff --git a/nuttx/lib/stdio/lib_memoutstream.c b/nuttx/lib/stdio/lib_memoutstream.c index 1cf2a8f29..d5a673b3a 100644 --- a/nuttx/lib/stdio/lib_memoutstream.c +++ b/nuttx/lib/stdio/lib_memoutstream.c @@ -82,10 +82,13 @@ static void memoutstream_putc(FAR struct lib_outstream_s *this, int ch) void lib_memoutstream(FAR struct lib_memoutstream_s *memoutstream, FAR char *bufstart, int buflen) { - memoutstream->public.put = memoutstream_putc; - memoutstream->public.nput = 0; /* Will be buffer index */ - memoutstream->buffer = bufstart; /* Start of buffer */ - memoutstream->buflen = buflen - 1; /* Save space for null terminator */ + memoutstream->public.put = memoutstream_putc; +#ifdef CONFIG_STDIO_LINEBUFFER + memoutstream->public.flush = lib_noflush; +#endif + memoutstream->public.nput = 0; /* Will be buffer index */ + memoutstream->buffer = bufstart; /* Start of buffer */ + memoutstream->buflen = buflen - 1; /* Save space for null terminator */ } diff --git a/nuttx/lib/stdio/lib_nulloutstream.c b/nuttx/lib/stdio/lib_nulloutstream.c index 85b7daa9e..f92cb0f33 100644 --- a/nuttx/lib/stdio/lib_nulloutstream.c +++ b/nuttx/lib/stdio/lib_nulloutstream.c @@ -72,7 +72,10 @@ static void nulloutstream_putc(FAR struct lib_outstream_s *this, int ch) void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream) { - nulloutstream->put = nulloutstream_putc; - nulloutstream->nput = 0; + nulloutstream->put = nulloutstream_putc; +#ifdef CONFIG_STDIO_LINEBUFFER + nulloutstream->flush = lib_noflush; +#endif + nulloutstream->nput = 0; } diff --git a/nuttx/lib/stdio/lib_rawoutstream.c b/nuttx/lib/stdio/lib_rawoutstream.c index 05cb5853f..bea47f3ce 100644 --- a/nuttx/lib/stdio/lib_rawoutstream.c +++ b/nuttx/lib/stdio/lib_rawoutstream.c @@ -91,8 +91,11 @@ static void rawoutstream_putc(FAR struct lib_outstream_s *this, int ch) void lib_rawoutstream(FAR struct lib_rawoutstream_s *rawoutstream, int fd) { - rawoutstream->public.put = rawoutstream_putc; - rawoutstream->public.nput = 0; - rawoutstream->fd = fd; + rawoutstream->public.put = rawoutstream_putc; +#ifdef CONFIG_STDIO_LINEBUFFER + rawoutstream->public.flush = lib_noflush; +#endif + rawoutstream->public.nput = 0; + rawoutstream->fd = fd; } diff --git a/nuttx/lib/stdio/lib_stdoutstream.c b/nuttx/lib/stdio/lib_stdoutstream.c index 272a93309..99fae11b2 100644 --- a/nuttx/lib/stdio/lib_stdoutstream.c +++ b/nuttx/lib/stdio/lib_stdoutstream.c @@ -59,6 +59,18 @@ static void stdoutstream_putc(FAR struct lib_outstream_s *this, int ch) } } +/**************************************************************************** + * Name: stdoutstream_flush + ****************************************************************************/ + +#if defined(CONFIG_STDIO_LINEBUFFER) && CONFIG_STDIO_BUFFER_SIZE > 0 +int stdoutstream_flush(FAR struct lib_outstream_s *this) +{ + FAR struct lib_stdoutstream_s *sthis = (FAR struct lib_stdoutstream_s *)this; + return lib_fflush(sthis->stream, true); +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -83,9 +95,16 @@ static void stdoutstream_putc(FAR struct lib_outstream_s *this, int ch) void lib_stdoutstream(FAR struct lib_stdoutstream_s *stdoutstream, FAR FILE *stream) { - stdoutstream->public.put = stdoutstream_putc; - stdoutstream->public.nput = 0; - stdoutstream->stream = stream; + stdoutstream->public.put = stdoutstream_putc; +#ifdef CONFIG_STDIO_LINEBUFFER +#if CONFIG_STDIO_BUFFER_SIZE > 0 + stdoutstream->public.flush = stdoutstream_flush; +#else + stdoutstream->public.flush = lib_noflush; +#endif +#endif + stdoutstream->public.nput = 0; + stdoutstream->stream = stream; } -- cgit v1.2.3