2014-06-15 00:19:42 +02:00
|
|
|
/****************************************************************************
|
2021-12-13 14:28:23 +01:00
|
|
|
* libs/libc/stream/lib_stdsostream.c
|
2014-06-15 00:19:42 +02: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
|
2014-06-15 00:19:42 +02:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-15 00:19:42 +02: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.
|
2014-06-15 00:19:42 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2014-06-15 00:19:42 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: stdsostream_putc
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void stdsostream_putc(FAR struct lib_sostream_s *this, int ch)
|
|
|
|
{
|
|
|
|
FAR struct lib_stdsostream_s *sthis = (FAR struct lib_stdsostream_s *)this;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
DEBUGASSERT(this && sthis->stream);
|
|
|
|
|
|
|
|
/* Loop until the character is successfully transferred or an irrecoverable
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = fputc(ch, sthis->stream);
|
|
|
|
if (result != EOF)
|
|
|
|
{
|
|
|
|
this->nput++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EINTR (meaning that fputc was interrupted by a signal) is the only
|
|
|
|
* recoverable error.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
while (get_errno() == EINTR);
|
|
|
|
}
|
|
|
|
|
2023-03-01 14:03:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: stdsostream_puts
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int stdsostream_puts(FAR struct lib_sostream_s *this,
|
|
|
|
FAR const void *buffer, int len)
|
|
|
|
{
|
|
|
|
FAR struct lib_stdsostream_s *sthis = (FAR struct lib_stdsostream_s *)this;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
DEBUGASSERT(this && sthis->stream);
|
|
|
|
|
|
|
|
/* Loop until the character is successfully transferred or an irrecoverable
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = lib_fwrite(buffer, len, sthis->stream);
|
|
|
|
if (result >= 0)
|
|
|
|
{
|
|
|
|
this->nput += result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = _NX_GETERRVAL(result);
|
|
|
|
|
|
|
|
/* EINTR (meaning that fputc was interrupted by a signal) is the only
|
|
|
|
* recoverable error.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
while (result == -EINTR);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-15 00:19:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: stdsostream_flush
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2014-06-15 00:19:42 +02:00
|
|
|
static int stdsostream_flush(FAR struct lib_sostream_s *this)
|
|
|
|
{
|
2017-02-09 16:53:50 +01:00
|
|
|
FAR struct lib_stdsostream_s *sthis = (FAR struct lib_stdsostream_s *)this;
|
2017-02-09 16:24:44 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(sthis != NULL && sthis->stream != NULL);
|
2017-02-09 16:53:50 +01:00
|
|
|
return lib_fflush(sthis->stream, true);
|
2014-06-15 00:19:42 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: stdsostream_seek
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t stdsostream_seek(FAR struct lib_sostream_s *this, off_t offset,
|
|
|
|
int whence)
|
|
|
|
{
|
2017-02-09 16:53:50 +01:00
|
|
|
FAR struct lib_stdsostream_s *sthis = (FAR struct lib_stdsostream_s *)this;
|
2014-06-15 00:19:42 +02:00
|
|
|
|
2017-02-09 16:53:50 +01:00
|
|
|
DEBUGASSERT(sthis != NULL && sthis->stream != NULL);
|
|
|
|
return fseek(sthis->stream, offset, whence);
|
2014-06-15 00:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_stdsostream
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initializes a stream for use with a FILE instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2014-06-15 00:19:42 +02:00
|
|
|
* outstream - User allocated, uninitialized instance of struct
|
|
|
|
* lib_stdsostream_s to be initialized.
|
|
|
|
* stream - User provided stream instance (must have been opened for
|
|
|
|
* write access).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None (User allocated instance initialized).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void lib_stdsostream(FAR struct lib_stdsostream_s *outstream,
|
|
|
|
FAR FILE *stream)
|
|
|
|
{
|
2022-12-04 10:35:13 +01:00
|
|
|
/* Select the putc operation */
|
2014-06-15 00:19:42 +02:00
|
|
|
|
2022-12-04 10:35:13 +01:00
|
|
|
outstream->public.putc = stdsostream_putc;
|
2023-03-01 14:03:07 +01:00
|
|
|
outstream->public.puts = stdsostream_puts;
|
2014-06-15 00:19:42 +02:00
|
|
|
|
|
|
|
/* Select the correct flush operation. This flush is only called when
|
|
|
|
* a newline is encountered in the output stream. However, we do not
|
|
|
|
* want to support this line buffering behavior if the stream was
|
|
|
|
* opened in binary mode. In binary mode, the newline has no special
|
|
|
|
* meaning.
|
|
|
|
*/
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2022-06-02 18:23:24 +02:00
|
|
|
if (stream->fs_bufstart != NULL && (stream->fs_oflags & O_TEXT) != 0)
|
2014-06-15 00:19:42 +02:00
|
|
|
{
|
|
|
|
outstream->public.flush = stdsostream_flush;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
outstream->public.flush = lib_snoflush;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select the seek operation */
|
|
|
|
|
|
|
|
outstream->public.seek = stdsostream_seek;
|
|
|
|
|
|
|
|
/* Set the number of bytes put to zero and remember the stream */
|
|
|
|
|
|
|
|
outstream->public.nput = 0;
|
2021-12-13 14:28:23 +01:00
|
|
|
outstream->stream = stream;
|
2014-06-15 00:19:42 +02:00
|
|
|
}
|