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

View File

@ -1,7 +1,7 @@
/****************************************************************************
* include/nuttx/streams.h
*
* Copyright (C) 2009, 2011-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 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
@ -54,26 +54,58 @@
/* These are the generic representations of a streams used by the NuttX */
struct lib_instream_s;
struct lib_outstream_s;
typedef int (*lib_getc_t)(FAR struct lib_instream_s *this);
struct lib_outstream_s;
typedef void (*lib_putc_t)(FAR struct lib_outstream_s *this, int ch);
typedef int (*lib_flush_t)(FAR struct lib_outstream_s *this);
struct lib_instream_s
{
lib_getc_t get; /* Pointer to function to get one character */
int nget; /* Total number of characters gotten. Written
lib_getc_t get; /* Get one character from the instream */
int nget; /* Total number of characters gotten. Written
* by get method, readable by user */
};
struct lib_outstream_s
{
lib_putc_t put; /* Pointer to function to put one character */
lib_putc_t put; /* Put one character to the outstream */
#ifdef CONFIG_STDIO_LINEBUFFER
lib_flush_t flush; /* Pointer to function flush buffered characters */
lib_flush_t flush; /* Flush any buffered characters in the outstream */
#endif
int nput; /* Total number of characters put. Written
int nput; /* Total number of characters put. Written
* by put method, readable by user */
};
/* Seek-able streams */
struct lib_sistream_s;
typedef int (*lib_sigetc_t)(FAR struct lib_sistream_s *this);
typedef off_t (*lib_siseek_t)(FAR struct lib_sistream_s *this, off_t offset,
int whence);
struct lib_sostream_s;
typedef void (*lib_soputc_t)(FAR struct lib_sostream_s *this, int ch);
typedef int (*lib_soflush_t)(FAR struct lib_sostream_s *this);
typedef off_t (*lib_soseek_t)(FAR struct lib_sostream_s *this, off_t offset,
int whence);
struct lib_sistream_s
{
lib_sigetc_t get; /* Get one character from the instream */
lib_siseek_t seek; /* Seek to a position in the instream */
int nget; /* Total number of characters gotten. Written
* by get method, readable by user */
};
struct lib_sostream_s
{
lib_soputc_t put; /* Put one character to the outstream */
#ifdef CONFIG_STDIO_LINEBUFFER
lib_soflush_t flush; /* Flush any buffered characters in the outstream */
#endif
lib_soseek_t seek; /* Seek a position in the output stream */
int nput; /* Total number of characters put. Written
* by put method, readable by user */
};
@ -83,14 +115,30 @@ struct lib_meminstream_s
{
struct lib_instream_s public;
FAR const char *buffer; /* Address of first byte in the buffer */
int buflen; /* Size of the buffer in bytes */
size_t buflen; /* Size of the buffer in bytes */
};
struct lib_memoutstream_s
{
struct lib_outstream_s public;
FAR char *buffer; /* Address of first byte in the buffer */
int buflen; /* Size of the buffer in bytes */
size_t buflen; /* Size of the buffer in bytes */
};
struct lib_memsistream_s
{
struct lib_sistream_s public;
FAR const char *buffer; /* Address of first byte in the buffer */
size_t offset; /* Current buffer offset in bytes */
size_t buflen; /* Size of the buffer in bytes */
};
struct lib_memsostream_s
{
struct lib_sostream_s public;
FAR char *buffer; /* Address of first byte in the buffer */
size_t offset; /* Current buffer offset in bytes */
size_t buflen; /* Size of the buffer in bytes */
};
/* These are streams that operate on a FILE */
@ -107,17 +155,41 @@ struct lib_stdoutstream_s
FAR FILE *stream;
};
struct lib_stdsistream_s
{
struct lib_sistream_s public;
FAR FILE *stream;
};
struct lib_stdsostream_s
{
struct lib_sostream_s public;
FAR FILE *stream;
};
/* These are streams that operate on a file descriptor */
struct lib_rawinstream_s
{
struct lib_instream_s public;
int fd;
};
struct lib_rawoutstream_s
{
struct lib_outstream_s public;
int fd;
};
struct lib_rawinstream_s
struct lib_rawsistream_s
{
struct lib_instream_s public;
struct lib_sistream_s public;
int fd;
};
struct lib_rawsostream_s
{
struct lib_sostream_s public;
int fd;
};
@ -139,16 +211,18 @@ extern "C"
****************************************************************************/
/****************************************************************************
* Name: lib_meminstream, lib_memoutstream
* Name: lib_meminstream, lib_memoutstream, lib_memsistream, lib_memsostream
*
* Description:
* Initializes a stream for use with a fixed-size memory buffer.
* Defined in lib/lib_meminstream.c and lib/lib_memoutstream.c
* Defined in lib/stdio/lib_meminstream.c and lib/stdio/lib_memoutstream.c.
* Seekable versions are defined in lib/stdio/lib_memsistream.c and
* lib/stdio/lib_memsostream.c.
*
* Input parameters:
* meminstream - User allocated, uninitialized instance of struct
* memstream - User allocated, uninitialized instance of struct
* lib_meminstream_s to be initialized.
* memoutstream - User allocated, uninitialized instance of struct
* memstream - 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
@ -158,10 +232,14 @@ extern "C"
*
****************************************************************************/
EXTERN void lib_meminstream(FAR struct lib_meminstream_s *meminstream,
FAR const char *bufstart, int buflen);
EXTERN void lib_memoutstream(FAR struct lib_memoutstream_s *memoutstream,
FAR char *bufstart, int buflen);
void lib_meminstream(FAR struct lib_meminstream_s *instream,
FAR const char *bufstart, int buflen);
void lib_memoutstream(FAR struct lib_memoutstream_s *outstream,
FAR char *bufstart, int buflen);
void lib_memsistream(FAR struct lib_memsistream_s *instream,
FAR const char *bufstart, int buflen);
void lib_memsostream(FAR struct lib_memsostream_s *outstream,
FAR char *bufstart, int buflen);
/****************************************************************************
* Name: lib_stdinstream, lib_stdoutstream
@ -183,9 +261,9 @@ EXTERN void lib_memoutstream(FAR struct lib_memoutstream_s *memoutstream,
*
****************************************************************************/
EXTERN void lib_stdinstream(FAR struct lib_stdinstream_s *stdinstream,
void lib_stdinstream(FAR struct lib_stdinstream_s *stdinstream,
FAR FILE *stream);
EXTERN void lib_stdoutstream(FAR struct lib_stdoutstream_s *stdoutstream,
void lib_stdoutstream(FAR struct lib_stdoutstream_s *stdoutstream,
FAR FILE *stream);
/****************************************************************************
@ -208,9 +286,9 @@ EXTERN void lib_stdoutstream(FAR struct lib_stdoutstream_s *stdoutstream,
*
****************************************************************************/
EXTERN void lib_rawinstream(FAR struct lib_rawinstream_s *rawinstream,
void lib_rawinstream(FAR struct lib_rawinstream_s *rawinstream,
int fd);
EXTERN void lib_rawoutstream(FAR struct lib_rawoutstream_s *rawoutstream,
void lib_rawoutstream(FAR struct lib_rawoutstream_s *rawoutstream,
int fd);
/****************************************************************************
@ -232,10 +310,10 @@ EXTERN void lib_rawoutstream(FAR struct lib_rawoutstream_s *rawoutstream,
****************************************************************************/
#ifdef CONFIG_ARCH_LOWGETC
EXTERN void lib_lowinstream(FAR struct lib_instream_s *lowinstream);
void lib_lowinstream(FAR struct lib_instream_s *lowinstream);
#endif
#ifdef CONFIG_ARCH_LOWPUTC
EXTERN void lib_lowoutstream(FAR struct lib_outstream_s *lowoutstream);
void lib_lowoutstream(FAR struct lib_outstream_s *lowoutstream);
#endif
/****************************************************************************
@ -264,9 +342,9 @@ EXTERN void lib_lowoutstream(FAR struct lib_outstream_s *lowoutstream);
*
****************************************************************************/
EXTERN void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream);
EXTERN void lib_nullinstream(FAR struct lib_instream_s *nullinstream);
EXTERN void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream);
void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream);
void lib_nullinstream(FAR struct lib_instream_s *nullinstream);
void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream);
/****************************************************************************
* Name: lib_sylogstream
@ -284,7 +362,7 @@ EXTERN void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream);
****************************************************************************/
#ifdef CONFIG_SYSLOG
EXTERN void lib_syslogstream(FAR struct lib_outstream_s *stream);
void lib_syslogstream(FAR struct lib_outstream_s *stream);
#endif
#undef EXTERN

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,

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

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 */

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 */
}

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 */
}

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 */
}

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 */
}