Add support for seekable memory streams

This commit is contained in:
Gregory Nutt
2014-06-14 15:35:18 -06:00
parent ced8cf0d6b
commit 24e603f23b
8 changed files with 538 additions and 54 deletions
+108 -30
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -159,6 +159,12 @@ void stream_semgive(FAR struct streamlist *list);
int lib_noflush(FAR struct lib_outstream_s *this);
#endif
/* Defined in lib_libsnoflush.c */
#ifdef CONFIG_STDIO_LINEBUFFER
int lib_snoflush(FAR struct lib_sostream_s *this);
#endif
/* Defined in lib_libsprintf.c */
int lib_sprintf(FAR struct lib_outstream_s *obj,
+5 -4
View File
@@ -41,9 +41,10 @@ CSRCS += lib_fileno.c lib_printf.c lib_syslog.c lib_lowsyslog.c
CSRCS += lib_sprintf.c lib_asprintf.c lib_snprintf.c lib_libsprintf.c
CSRCS += lib_vsprintf.c lib_avsprintf.c lib_vsnprintf.c lib_libvsprintf.c
CSRCS += lib_dprintf.c lib_vdprintf.c
CSRCS += lib_meminstream.c lib_memoutstream.c lib_lowinstream.c
CSRCS += lib_lowoutstream.c lib_zeroinstream.c lib_nullinstream.c
CSRCS += lib_nulloutstream.c lib_sscanf.c
CSRCS += lib_meminstream.c lib_memoutstream.c lib_memsistream.c
CSRCS += lib_memsostream.c lib_lowinstream.c lib_lowoutstream.c
CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c
CSRCS += lib_sscanf.c
# The remaining sources files depend upon file descriptors
@@ -78,7 +79,7 @@ CSRCS += lib_dtoa.c
endif
ifeq ($(CONFIG_STDIO_LINEBUFFER),y)
CSRCS += lib_libnoflush.c
CSRCS += lib_libnoflush.c lib_libsnoflush.c
endif
# Add the stdio directory to the build
+104
View File
@@ -0,0 +1,104 @@
/****************************************************************************
* libc/stdio/lib_libsnoflush.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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/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_snoflush
*
* Description:
* lib_snoflush() provides a common, dummy flush method for seekable output
* streams that are not flushable. Only used if CONFIG_STDIO_LINEBUFFER
* is selected.
*
* Return:
* Always returns OK
*
****************************************************************************/
int lib_snoflush(FAR struct lib_sostream_s *this)
{
return OK;
}
#endif /* CONFIG_STDIO_LINEBUFFER */
+8 -8
View File
@@ -1,7 +1,7 @@
/****************************************************************************
* libc/stdio/lib_meminstream.c
*
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2011-2012, 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -82,23 +82,23 @@ static int meminstream_getc(FAR struct lib_instream_s *this)
* Initializes a stream for use with a fixed-size memory buffer.
*
* Input parameters:
* meminstream - User allocated, uninitialized instance of struct
* instream - User allocated, uninitialized instance of struct
* lib_meminstream_s to be initialized.
* bufstart - Address of the beginning of the fixed-size memory buffer
* buflen - Size of the fixed-sized memory buffer in bytes
*
* Returned Value:
* None (meminstream initialized).
* None (instream initialized).
*
****************************************************************************/
void lib_meminstream(FAR struct lib_meminstream_s *meminstream,
void lib_meminstream(FAR struct lib_meminstream_s *instream,
FAR const char *bufstart, int buflen)
{
meminstream->public.get = meminstream_getc;
meminstream->public.nget = 0; /* Will be buffer index */
meminstream->buffer = bufstart; /* Start of buffer */
meminstream->buflen = buflen; /* Length of the buffer */
instream->public.get = meminstream_getc;
instream->public.nget = 0; /* Will be buffer index */
instream->buffer = bufstart; /* Start of buffer */
instream->buflen = buflen; /* Length of the buffer */
}
+10 -12
View File
@@ -1,7 +1,7 @@
/****************************************************************************
* libc/stdio/lib_memoutstream.c
*
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2011-2012, 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -79,27 +79,25 @@ static void memoutstream_putc(FAR struct lib_outstream_s *this, int ch)
* Initializes a stream for use with a fixed-size memory buffer.
*
* Input parameters:
* memoutstream - User allocated, uninitialized instance of struct
* outstream - User allocated, uninitialized instance of struct
* lib_memoutstream_s to be initialized.
* bufstart - Address of the beginning of the fixed-size memory buffer
* buflen - Size of the fixed-sized memory buffer in bytes
*
* Returned Value:
* None (memoutstream initialized).
* None (outstream initialized).
*
****************************************************************************/
void lib_memoutstream(FAR struct lib_memoutstream_s *memoutstream,
void lib_memoutstream(FAR struct lib_memoutstream_s *outstream,
FAR char *bufstart, int buflen)
{
memoutstream->public.put = memoutstream_putc;
outstream->public.put = memoutstream_putc;
#ifdef CONFIG_STDIO_LINEBUFFER
memoutstream->public.flush = lib_noflush;
outstream->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 */
memoutstream->buffer[0] = '\0'; /* Start with an empty string */
outstream->public.nput = 0; /* Will be buffer index */
outstream->buffer = bufstart; /* Start of buffer */
outstream->buflen = buflen - 1; /* Save space for null terminator */
outstream->buffer[0] = '\0'; /* Start with an empty string */
}
+148
View File
@@ -0,0 +1,148 @@
/****************************************************************************
* libc/stdio/lib_memsistream.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <assert.h>
#include "lib_internal.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: memsistream_getc
****************************************************************************/
static int memsistream_getc(FAR struct lib_sistream_s *this)
{
FAR struct lib_memsistream_s *mthis = (FAR struct lib_memsistream_s *)this;
int ret;
DEBUGASSERT(this);
/* Get the next character (if any) from the buffer */
if (mthis->offset < mthis->buflen)
{
ret = mthis->buffer[mthis->offset];
mthis->offset++;
this->nget++;
}
else
{
ret = EOF;
}
return ret;
}
/****************************************************************************
* Name: memsistream_seek
****************************************************************************/
static off_t memsistream_seek(FAR struct lib_sistream_s *this, off_t offset,
int whence)
{
FAR struct lib_memsistream_s *mthis = (FAR struct lib_memsistream_s *)this;
off_t newpos;
DEBUGASSERT(this);
switch (whence)
{
case SEEK_CUR:
newpos = (off_t)mthis->offset + offset;
break;
case SEEK_SET:
newpos = offset;
break;
case SEEK_END:
newpos = (off_t)mthis->buflen + offset;
break;
default:
return (off_t)ERROR;
}
/* Make sure that the new position is within range */
if (newpos < 0 || newpos >= (off_t)mthis->buflen)
{
return (off_t)ERROR;
}
/* Return the new position */
mthis->offset = (size_t)newpos;
return newpos;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: lib_memsistream
*
* Description:
* Initializes a stream for use with a fixed-size memory buffer.
*
* Input parameters:
* instream - User allocated, uninitialized instance of struct
* lib_memsistream_s to be initialized.
* bufstart - Address of the beginning of the fixed-size memory buffer
* buflen - Size of the fixed-sized memory buffer in bytes
*
* Returned Value:
* None (instream initialized).
*
****************************************************************************/
void lib_memsistream(FAR struct lib_memsistream_s *instream,
FAR const char *bufstart, int buflen)
{
instream->public.get = memsistream_getc;
instream->public.seek = memsistream_seek;
instream->public.nget = 0; /* Total number of characters read */
instream->buffer = bufstart; /* Start of buffer */
instream->offset = 0; /* Will be the buffer index */
instream->buflen = buflen; /* Length of the buffer */
}
+149
View File
@@ -0,0 +1,149 @@
/****************************************************************************
* libc/stdio/lib_memsostream.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <assert.h>
#include "lib_internal.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: memsostream_putc
****************************************************************************/
static void memsostream_putc(FAR struct lib_sostream_s *this, int ch)
{
FAR struct lib_memsostream_s *mthis = (FAR struct lib_memsostream_s *)this;
DEBUGASSERT(this);
/* If this will not overrun the buffer, then write the character to the
* buffer. Not that buflen was pre-decremented when the stream was
* created so it is okay to write past the end of the buflen by one.
*/
if (mthis->offset < mthis->buflen)
{
mthis->buffer[mthis->offset] = ch;
mthis->offset++;
this->nput++;
mthis->buffer[mthis->offset] = '\0';
}
}
/****************************************************************************
* Name: memsostream_seek
****************************************************************************/
static off_t memsostream_seek(FAR struct lib_sostream_s *this, off_t offset,
int whence)
{
FAR struct lib_memsostream_s *mthis = (FAR struct lib_memsostream_s *)this;
off_t newpos;
DEBUGASSERT(this);
switch (whence)
{
case SEEK_CUR:
newpos = (off_t)mthis->offset + offset;
break;
case SEEK_SET:
newpos = offset;
break;
case SEEK_END:
newpos = (off_t)mthis->buflen + offset;
break;
default:
return (off_t)ERROR;
}
/* Make sure that the new position is within range */
if (newpos < 0 || newpos >= (off_t)mthis->buflen)
{
return (off_t)ERROR;
}
/* Return the new position */
mthis->offset = (size_t)newpos;
return newpos;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: lib_memsostream
*
* Description:
* Initializes a stream for use with a fixed-size memory buffer.
*
* Input parameters:
* outstream - User allocated, uninitialized instance of struct
* lib_memsostream_s to be initialized.
* bufstart - Address of the beginning of the fixed-size memory buffer
* buflen - Size of the fixed-sized memory buffer in bytes
*
* Returned Value:
* None (outstream initialized).
*
****************************************************************************/
void lib_memsostream(FAR struct lib_memsostream_s *outstream,
FAR char *bufstart, int buflen)
{
outstream->public.put = memsostream_putc;
#ifdef CONFIG_STDIO_LINEBUFFER
outstream->public.flush = lib_snoflush;
#endif
outstream->public.seek = memsostream_seek;
outstream->public.nput = 0; /* Total number of characters written */
outstream->buffer = bufstart; /* Start of buffer */
outstream->offset = 0; /* Will be the buffer index */
outstream->buflen = buflen - 1; /* Save space for null terminator */
outstream->buffer[0] = '\0'; /* Start with an empty string */
}