nuttx/libs/libc/stream/lib_stdsostream.c

189 lines
5.8 KiB
C
Raw Normal View History

2014-06-15 00:19:42 +02:00
/****************************************************************************
* libs/libc/stream/lib_stdsostream.c
2014-06-15 00:19:42 +02: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
*
* http://www.apache.org/licenses/LICENSE-2.0
2014-06-15 00:19:42 +02: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>
#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 *self, int ch)
2014-06-15 00:19:42 +02:00
{
FAR struct lib_stdsostream_s *stream =
(FAR struct lib_stdsostream_s *)self;
2014-06-15 00:19:42 +02:00
int result;
DEBUGASSERT(self && stream->handle);
2014-06-15 00:19:42 +02:00
/* Loop until the character is successfully transferred or an irrecoverable
* error occurs.
*/
do
{
result = fputc(ch, stream->handle);
2014-06-15 00:19:42 +02:00
if (result != EOF)
{
self->nput++;
2014-06-15 00:19:42 +02:00
return;
}
/* EINTR (meaning that fputc was interrupted by a signal) is the only
* recoverable error.
*/
}
while (get_errno() == EINTR);
}
/****************************************************************************
* Name: stdsostream_puts
****************************************************************************/
static int stdsostream_puts(FAR struct lib_sostream_s *self,
FAR const void *buffer, int len)
{
FAR struct lib_stdsostream_s *stream =
(FAR struct lib_stdsostream_s *)self;
int result;
DEBUGASSERT(self && stream->handle);
/* Loop until the character is successfully transferred or an irrecoverable
* error occurs.
*/
do
{
result = lib_fwrite(buffer, len, stream->handle);
if (result >= 0)
{
self->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
****************************************************************************/
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
static int stdsostream_flush(FAR struct lib_sostream_s *self)
2014-06-15 00:19:42 +02:00
{
FAR struct lib_stdsostream_s *stream =
(FAR struct lib_stdsostream_s *)self;
DEBUGASSERT(stream != NULL && stream->handle != NULL);
return lib_fflush(stream->handle);
2014-06-15 00:19:42 +02:00
}
#endif
/****************************************************************************
* Name: stdsostream_seek
****************************************************************************/
static off_t stdsostream_seek(FAR struct lib_sostream_s *self, off_t offset,
2014-06-15 00:19:42 +02:00
int whence)
{
FAR struct lib_stdsostream_s *stream =
(FAR struct lib_stdsostream_s *)self;
2014-06-15 00:19:42 +02:00
DEBUGASSERT(stream != NULL && stream->handle != NULL);
return fseek(stream->handle, 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.
*
* Input Parameters:
* stream - User allocated, uninitialized instance of struct
* lib_stdsostream_s to be initialized.
* handle - User provided FILE instance (must have been opened for
* write access).
2014-06-15 00:19:42 +02:00
*
* Returned Value:
* None (User allocated instance initialized).
*
****************************************************************************/
void lib_stdsostream(FAR struct lib_stdsostream_s *stream,
FAR FILE *handle)
2014-06-15 00:19:42 +02:00
{
/* Select the putc operation */
2014-06-15 00:19:42 +02:00
stream->common.putc = stdsostream_putc;
stream->common.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 file stream. However, we do not
* want to support this line buffering behavior if the file was
2014-06-15 00:19:42 +02:00
* opened in binary mode. In binary mode, the newline has no special
* meaning.
*/
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
if (handle->fs_bufstart != NULL && (handle->fs_oflags & O_TEXT) != 0)
2014-06-15 00:19:42 +02:00
{
stream->common.flush = stdsostream_flush;
2014-06-15 00:19:42 +02:00
}
else
#endif
{
stream->common.flush = lib_snoflush;
2014-06-15 00:19:42 +02:00
}
/* Select the seek operation */
stream->common.seek = stdsostream_seek;
2014-06-15 00:19:42 +02:00
/* Set the number of bytes put to zero and remember the handle */
2014-06-15 00:19:42 +02:00
stream->common.nput = 0;
stream->handle = handle;
2014-06-15 00:19:42 +02:00
}