From 81fccf96db9989c2384608b2eec203b2eb9b502b Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Thu, 21 Dec 2023 22:19:19 +0800 Subject: [PATCH] libc/stream: add file out stream Signed-off-by: yinshengkai --- include/nuttx/streams.h | 8 ++ libs/libc/stream/Make.defs | 2 +- libs/libc/stream/lib_fileoutstream.c | 110 +++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 libs/libc/stream/lib_fileoutstream.c diff --git a/include/nuttx/streams.h b/include/nuttx/streams.h index e6375ebba7..09f72a9b2a 100644 --- a/include/nuttx/streams.h +++ b/include/nuttx/streams.h @@ -205,6 +205,12 @@ struct lib_rawoutstream_s int fd; }; +struct lib_fileoutstream_s +{ + struct lib_outstream_s common; + struct file *file; +}; + struct lib_rawsistream_s { struct lib_sistream_s common; @@ -396,6 +402,8 @@ void lib_rawinstream(FAR struct lib_rawinstream_s *stream, int fd); void lib_rawoutstream(FAR struct lib_rawoutstream_s *stream, int fd); void lib_rawsistream(FAR struct lib_rawsistream_s *stream, int fd); void lib_rawsostream(FAR struct lib_rawsostream_s *stream, int fd); +void lib_fileoutstream(FAR struct lib_fileoutstream_s *stream, + FAR struct file *file); /**************************************************************************** * Name: lib_bufferedoutstream diff --git a/libs/libc/stream/Make.defs b/libs/libc/stream/Make.defs index 2be97e9b19..e9ae093d3e 100644 --- a/libs/libc/stream/Make.defs +++ b/libs/libc/stream/Make.defs @@ -27,7 +27,7 @@ CSRCS += lib_rawoutstream.c lib_rawsistream.c lib_rawsostream.c CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c CSRCS += lib_mtdoutstream.c lib_libnoflush.c lib_libsnoflush.c CSRCS += lib_syslogstream.c lib_syslograwstream.c lib_bufferedoutstream.c -CSRCS += lib_hexdumpstream.c +CSRCS += lib_hexdumpstream.c lib_fileoutstream.c # The remaining sources files depend upon C streams diff --git a/libs/libc/stream/lib_fileoutstream.c b/libs/libc/stream/lib_fileoutstream.c new file mode 100644 index 0000000000..02e0796073 --- /dev/null +++ b/libs/libc/stream/lib_fileoutstream.c @@ -0,0 +1,110 @@ +/**************************************************************************** + * libs/libc/stream/lib_fileoutstream.c + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include + +#include "libc.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rawoutstream_puts + ****************************************************************************/ + +static int fileoutstream_puts(FAR struct lib_outstream_s *self, + FAR const void *buf, int len) +{ + FAR struct lib_fileoutstream_s *stream = + (FAR struct lib_fileoutstream_s *)self; + int nwritten; + + do + { + nwritten = file_write(stream->file, buf, len); + if (nwritten >= 0) + { + self->nput += nwritten; + return nwritten; + } + + /* The only expected error is EINTR, meaning that the write operation + * was awakened by a signal. Zero would not be a valid return value + * from _NX_WRITE(). + */ + + DEBUGASSERT(nwritten < 0); + } + while (nwritten == -EINTR); + + return nwritten; +} + +/**************************************************************************** + * Name: rawoutstream_putc + ****************************************************************************/ + +static void fileoutstream_putc(FAR struct lib_outstream_s *self, int ch) +{ + char tmp = ch; + fileoutstream_puts(self, &tmp, 1); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_fileoutstream + * + * Description: + * Initializes a stream for use with a file descriptor. + * + * Input Parameters: + * stream - User allocated, uninitialized instance of struct + * lib_rawoutstream_s to be initialized. + * file - User provided FILE instance (must have been opened for + * write access). + * + * Returned Value: + * None (User allocated instance initialized). + * + ****************************************************************************/ + +void lib_fileoutstream(FAR struct lib_fileoutstream_s *stream, + FAR struct file *file) +{ + stream->common.putc = fileoutstream_putc; + stream->common.puts = fileoutstream_puts; + stream->common.flush = lib_noflush; + stream->common.nput = 0; + stream->file = file; +}