2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdio/lib_memoutstream.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: memoutstream_putc
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void memoutstream_putc(FAR struct lib_outstream_s *this, int ch)
|
|
|
|
{
|
2021-03-02 14:33:27 +01:00
|
|
|
FAR struct lib_memoutstream_s *mthis =
|
|
|
|
(FAR struct lib_memoutstream_s *)this;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
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 (this->nput < mthis->buflen)
|
|
|
|
{
|
|
|
|
mthis->buffer[this->nput] = ch;
|
|
|
|
this->nput++;
|
|
|
|
mthis->buffer[this->nput] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_memoutstream
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initializes a stream for use with a fixed-size memory buffer.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2014-06-14 23:35:18 +02:00
|
|
|
* outstream - User allocated, uninitialized instance of struct
|
2012-11-10 17:06:01 +01:00
|
|
|
* 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:
|
2014-06-14 23:35:18 +02:00
|
|
|
* None (outstream initialized).
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-14 23:35:18 +02:00
|
|
|
void lib_memoutstream(FAR struct lib_memoutstream_s *outstream,
|
2012-11-10 17:06:01 +01:00
|
|
|
FAR char *bufstart, int buflen)
|
|
|
|
{
|
2014-06-14 23:35:18 +02:00
|
|
|
outstream->public.put = memoutstream_putc;
|
|
|
|
outstream->public.flush = lib_noflush;
|
|
|
|
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 */
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|