stream: Add fileinstream support

This commit introduces a new fileinstream implementation
in the NuttX stream library.

Signed-off-by: yangao1 <yangao1@xiaomi.com>
This commit is contained in:
yangao1 2025-05-06 20:26:32 +08:00 committed by Donny(董九柱)
parent 0b9bd22e19
commit 47dd21c3c1
4 changed files with 172 additions and 4 deletions

View file

@ -211,6 +211,12 @@ struct lib_rawoutstream_s
int fd;
};
struct lib_fileinstream_s
{
struct lib_instream_s common;
struct file file;
};
struct lib_fileoutstream_s
{
struct lib_outstream_s common;
@ -399,6 +405,36 @@ void lib_stdsistream(FAR struct lib_stdsistream_s *stream,
void lib_stdsostream(FAR struct lib_stdsostream_s *stream,
FAR FILE *handle);
/****************************************************************************
* Name: lib_fileinstream_open, lib_fileinstream_close,
* lib_fileoutstream_open, lib_fileoutstream_close
*
* Description:
* Initializes or release a file-based stream instance.
* Defined in lib_fileinstream.c and lib/lib_fileoutstream.c
*
* Input Parameters:
* For open:
* stream - User allocated, uninitialized instance of stream struct
* to be initialized.
* path - Path to the file to be opened.
* oflag - File open flags.
* mode - File access mode.
* For close:
* stream - User allocated, initialized instance of stream struct
* Returned Value:
* open: Zero on success; a negated errno value on failure.
* close: None (resource cleanup only).
*
****************************************************************************/
int lib_fileinstream_open(FAR struct lib_fileinstream_s *stream,
FAR const char *path, int oflag, mode_t mode);
void lib_fileinstream_close(FAR struct lib_fileinstream_s *stream);
int lib_fileoutstream_open(FAR struct lib_fileoutstream_s *stream,
FAR const char *path, int oflag, mode_t mode);
void lib_fileoutstream_close(FAR struct lib_fileoutstream_s *stream);
/****************************************************************************
* Name: lib_rawinstream, lib_rawoutstream, lib_rawsistream, and
* lib_rawsostream,
@ -424,8 +460,6 @@ 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

View file

@ -43,6 +43,7 @@ list(
lib_bufferedoutstream.c
lib_hexdumpstream.c
lib_base64outstream.c
lib_fileinstream.c
lib_fileoutstream.c)
if(CONFIG_FILE_STREAM)

View file

@ -29,8 +29,8 @@ 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 lib_base64outstream.c lib_fileoutstream.c
CSRCS += lib_mtdsostream.c
CSRCS += lib_hexdumpstream.c lib_base64outstream.c lib_mtdsostream.c
CSRCS += lib_fileinstream.c lib_fileoutstream.c
# The remaining sources files depend upon C streams

View file

@ -0,0 +1,133 @@
/****************************************************************************
* libs/libc/stream/lib_fileinstream.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 <nuttx/config.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include "libc.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: fileinstream_gets
****************************************************************************/
static ssize_t fileinstream_gets(FAR struct lib_instream_s *self,
FAR void *buf, size_t len)
{
FAR struct lib_fileinstream_s *stream =
(FAR struct lib_fileinstream_s *)self;
ssize_t nread;
do
{
nread = file_read(&stream->file, buf, len);
}
while (nread == -EINTR);
if (nread >= 0)
{
self->nget += nread;
}
return nread;
}
/****************************************************************************
* Name: fileinstream_getc
****************************************************************************/
static int fileinstream_getc(FAR struct lib_instream_s *self)
{
unsigned char ch;
return fileinstream_gets(self, &ch, 1) == 1 ? ch : EOF;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: lib_fileinstream_open
*
* Description:
* Initializes a stream for use with a file descriptor.
*
* Input Parameters:
* stream - User allocated, uninitialized instance of struct
* lib_fileinstream_s to be initialized.
*
* Returned Value:
* None (User allocated instance initialized).
*
****************************************************************************/
int lib_fileinstream_open(FAR struct lib_fileinstream_s *stream,
FAR const char *path, int oflag, mode_t mode)
{
int ret;
ret = file_open(&stream->file, path, oflag, mode);
if (ret < 0)
{
return ret;
}
stream->common.getc = fileinstream_getc;
stream->common.gets = fileinstream_gets;
stream->common.nget = 0;
return 0;
}
/****************************************************************************
* Name: lib_fileinstream_close
*
* Description:
* Close the file associated with the stream.
*
* Input Parameters:
* stream - User allocated, uninitialized instance of struct
* lib_fileinstream_s to be initialized.
*
* Returned Value:
* None (User allocated instance initialized).
*
****************************************************************************/
void lib_fileinstream_close(FAR struct lib_fileinstream_s *stream)
{
if (stream != NULL)
{
file_close(&stream->file);
stream->common.nget = 0;
}
}