libc/streams: Implement gets/puts for all streams

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai
2023-03-01 21:03:07 +08:00
committed by Xiang Xiao
parent 6e35a51feb
commit 205ca26556
13 changed files with 340 additions and 0 deletions
+30
View File
@@ -57,6 +57,35 @@ static int memsistream_getc(FAR struct lib_sistream_s *this)
return ret;
}
/****************************************************************************
* Name: meminstream_gets
****************************************************************************/
static int memsistream_gets(FAR struct lib_instream_s *this,
FAR void *buffer, int len)
{
FAR struct lib_memsistream_s *mthis = (FAR struct lib_memsistream_s *)this;
int ret;
DEBUGASSERT(this);
/* Get the buffer (if any) from the stream */
if (this->nget < mthis->buflen)
{
ret = mthis->buflen - this->nget;
ret = ret < len ? ret : len;
this->nget += ret;
memcpy(buffer, mthis->buffer, ret);
}
else
{
ret = EOF;
}
return ret;
}
/****************************************************************************
* Name: memsistream_seek
****************************************************************************/
@@ -125,6 +154,7 @@ void lib_memsistream(FAR struct lib_memsistream_s *instream,
FAR const char *bufstart, int buflen)
{
instream->public.getc = memsistream_getc;
instream->public.gets = memsistream_gets;
instream->public.seek = memsistream_seek;
instream->public.nget = 0; /* Total number of characters read */
instream->buffer = bufstart; /* Start of buffer */