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
This commit is contained in:
patacongo
2011-05-14 15:21:04 +00:00
parent 764a1177eb
commit 6e4aa998e8
15 changed files with 214 additions and 24 deletions
+8 -2
View File
@@ -110,10 +110,16 @@ extern void stream_semtake(FAR struct streamlist *list);
extern void stream_semgive(FAR struct streamlist *list);
#endif
/* Defined in lib_libnoflush.c */
#ifdef CONFIG_STDIO_LINEBUFFER
extern int lib_noflush(FAR struct lib_outstream_s *this);
#endif
/* Defined in lib_libsprintf.c */
extern int lib_sprintf (FAR struct lib_outstream_s *obj,
const char *fmt, ...);
extern int lib_sprintf(FAR struct lib_outstream_s *obj,
const char *fmt, ...);
/* Defined lib_libvsprintf.c */
+5
View File
@@ -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
+103
View File
@@ -0,0 +1,103 @@
/****************************************************************************
* lib/stdio/lib_libnoflush.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <stdbool.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <nuttx/fs.h>
#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 */
+13
View File
@@ -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;
}
+2 -2
View File
@@ -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;
+5 -2
View File
@@ -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 */
+7 -4
View File
@@ -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 */
}
+5 -2
View File
@@ -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;
}
+6 -3
View File
@@ -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;
}
+22 -3
View File
@@ -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;
}