xiaoxiang781216 commented on code in PR #8685:
URL: https://github.com/apache/nuttx/pull/8685#discussion_r1122161302
##########
libs/libc/stream/lib_memsostream.c:
##########
@@ -50,10 +50,33 @@ static void memsostream_putc(FAR struct lib_sostream_s
*this, int ch)
mthis->buffer[mthis->offset] = ch;
mthis->offset++;
this->nput++;
- mthis->buffer[mthis->offset] = '\0';
}
}
+/****************************************************************************
+ * Name: memoutstream_puts
+ ****************************************************************************/
+
+static int memsostream_puts(FAR struct lib_sostream_s *this,
+ FAR const void *buf, int len)
+{
+ int ncopy;
+ FAR struct lib_memsostream_s *mthis = (FAR struct lib_memsostream_s *)this;
+
+ DEBUGASSERT(this);
+
+ ncopy = mthis->offset + len < mthis->buflen ? len :
Review Comment:
```suggestion
ncopy = mthis->offset + len + 1 < mthis->buflen ? len :
mthis->buflen - mthis->offset - 1;
```
##########
libs/libc/stream/lib_memsostream.c:
##########
@@ -50,10 +50,33 @@ static void memsostream_putc(FAR struct lib_sostream_s
*this, int ch)
mthis->buffer[mthis->offset] = ch;
mthis->offset++;
this->nput++;
- mthis->buffer[mthis->offset] = '\0';
}
}
+/****************************************************************************
+ * Name: memoutstream_puts
+ ****************************************************************************/
+
+static int memsostream_puts(FAR struct lib_sostream_s *this,
+ FAR const void *buf, int len)
+{
+ int ncopy;
+ FAR struct lib_memsostream_s *mthis = (FAR struct lib_memsostream_s *)this;
+
+ DEBUGASSERT(this);
+
+ ncopy = mthis->offset + len < mthis->buflen ? len :
+ mthis->buflen - mthis->offset;
+ if (ncopy > 0)
+ {
+ memcpy(mthis->buffer + mthis->offset, buf, ncopy);
+ mthis->public.nput += ncopy;
+ mthis->offset += ncopy;
Review Comment:
append '\0'
##########
libs/libc/stream/lib_memsostream.c:
##########
@@ -50,10 +50,33 @@ static void memsostream_putc(FAR struct lib_sostream_s
*this, int ch)
mthis->buffer[mthis->offset] = ch;
mthis->offset++;
this->nput++;
- mthis->buffer[mthis->offset] = '\0';
Review Comment:
why remove the tail zero? which is required to ensure the buffer always
terminated with '\0'.
##########
libs/libc/stream/lib_rawsistream.c:
##########
@@ -66,6 +66,33 @@ static int rawsistream_getc(FAR struct lib_sistream_s *this)
return EOF;
}
+/****************************************************************************
+ * Name: rawsistream_gets
+ ****************************************************************************/
+
+static int rawsistream_gets(FAR struct lib_instream_s *this,
+ FAR void *buffer, int len)
+{
+ FAR struct lib_rawsistream_s *rthis = (FAR struct lib_rawsistream_s *)this;
+ int nread;
+
+ DEBUGASSERT(this && rthis->fd >= 0);
+
+ /* Attempt to read a buffer */
+
+ nread = _NX_READ(rthis->fd, buffer, len);
+ if (nread >= 0)
+ {
+ this->nget += nread;
+ }
+ else
+ {
+ nread = get_errno();
Review Comment:
nread = _NX_GETERRNO(nread);
##########
libs/libc/stream/lib_rawsostream.c:
##########
@@ -73,6 +73,45 @@ static void rawsostream_putc(FAR struct lib_sostream_s
*this, int ch)
while (errcode == EINTR);
}
+/****************************************************************************
+ * Name: rawsostream_puts
+ ****************************************************************************/
+
+static int rawsostream_puts(FAR struct lib_sostream_s *this,
+ FAR const void *buffer, int len)
+{
+ FAR struct lib_rawsostream_s *rthis = (FAR struct lib_rawsostream_s *)this;
+ int nwritten;
+ int errcode;
+
+ DEBUGASSERT(this && rthis->fd >= 0);
+
+ /* Loop until the buffer is successfully transferred or until an
+ * irrecoverable error occurs.
+ */
+
+ do
+ {
+ nwritten = _NX_WRITE(rthis->fd, buffer, len);
+ if (nwritten >= 0)
+ {
+ this->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().
+ */
+
+ errcode = _NX_GETERRNO(nwritten);
+ DEBUGASSERT(nwritten < 0);
+ }
+ while (errcode == EINTR);
+
+ return errcode;
Review Comment:
should we return nwritten in case of succeed.
##########
libs/libc/stream/lib_stdoutstream.c:
##########
@@ -64,6 +65,44 @@ static void stdoutstream_putc(FAR struct lib_outstream_s
*this, int ch)
while (get_errno() == EINTR);
}
+/****************************************************************************
+ * Name: stdoutstream_puts
+ ****************************************************************************/
+
+static int stdoutstream_puts(FAR struct lib_outstream_s *this,
+ FAR const void *buffer, int len)
+{
+ FAR struct lib_stdoutstream_s *sthis =
+ (FAR struct lib_stdoutstream_s *)this;
+ int result;
+ int errcode;
+
+ DEBUGASSERT(this && sthis->stream);
+
+ /* Loop until the buffer is successfully transferred or an irrecoverable
+ * error occurs.
+ */
+
+ do
+ {
+ result = fwrite(buffer, len, 1, sthis->stream);
+ if (result >= 0)
+ {
+ this->nput += result;
+ return result;
+ }
+
+ errcode = get_errno();
+
+ /* EINTR (meaning that fputc was interrupted by a signal) is the only
+ * recoverable error.
+ */
+ }
+ while (errcode == EINTR);
+
+ return errcode;
Review Comment:
return the written size when pass
##########
libs/libc/stream/lib_stdsistream.c:
##########
@@ -52,6 +52,31 @@ static int stdsistream_getc(FAR struct lib_sistream_s *this)
return ret;
}
+/****************************************************************************
+ * Name: stdsistream_gets
+ ****************************************************************************/
+
+static int stdsistream_gets(FAR struct lib_instream_s *this,
+ FAR void *buffer, int len)
+{
+ FAR struct lib_stdsistream_s *sthis = (FAR struct lib_stdsistream_s *)this;
+ int nread = 0;
+ char *ret;
+
+ DEBUGASSERT(this);
+
+ /* Get the buffer from the incoming stream */
+
+ ret = fgets(buffer, len, sthis->stream);
Review Comment:
why use fgets, but not fread
##########
libs/libc/stream/lib_nulloutstream.c:
##########
@@ -39,6 +39,16 @@ static void nulloutstream_putc(FAR struct lib_outstream_s
*this, int ch)
this->nput++;
}
+static int nulloutstream_puts(FAR struct lib_outstream_s *this,
+ FAR const void *buffer, int len)
Review Comment:
```suggestion
FAR const void *buffer, int len)
```
##########
libs/libc/stream/lib_stdoutstream.c:
##########
@@ -64,6 +65,44 @@ static void stdoutstream_putc(FAR struct lib_outstream_s
*this, int ch)
while (get_errno() == EINTR);
}
+/****************************************************************************
+ * Name: stdoutstream_puts
+ ****************************************************************************/
+
+static int stdoutstream_puts(FAR struct lib_outstream_s *this,
+ FAR const void *buffer, int len)
Review Comment:
```suggestion
FAR const void *buffer, int len)
```
##########
libs/libc/stream/lib_rawinstream.c:
##########
@@ -66,6 +66,33 @@ static int rawinstream_getc(FAR struct lib_instream_s *this)
return EOF;
}
+/****************************************************************************
+ * Name: rawinstream_getc
+ ****************************************************************************/
+
+static int rawinstream_gets(FAR struct lib_instream_s *this,
+ FAR void *buffer, int len)
+{
+ FAR struct lib_rawinstream_s *rthis = (FAR struct lib_rawinstream_s *)this;
+ int nread;
+
+ DEBUGASSERT(this && rthis->fd >= 0);
+
+ /* Attempt to read one character */
+
+ nread = _NX_READ(rthis->fd, buffer, len);
+ if (nread >= 0)
+ {
+ this->nget += nread;
+ }
+ else
+ {
+ nread = get_errno();
Review Comment:
nread = _NX_GETERRNO(nread);
##########
libs/libc/stream/lib_stdsostream.c:
##########
@@ -63,6 +63,43 @@ static void stdsostream_putc(FAR struct lib_sostream_s
*this, int ch)
while (get_errno() == EINTR);
}
+/****************************************************************************
+ * 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;
+ int errcode;
+
+ 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;
+ }
+
+ errcode = get_errno();
+
+ /* EINTR (meaning that fputc was interrupted by a signal) is the only
+ * recoverable error.
+ */
+ }
+ while (errcode == EINTR);
+
+ return errcode;
Review Comment:
return length when pass
##########
libs/libc/stream/lib_stdsostream.c:
##########
@@ -63,6 +63,43 @@ static void stdsostream_putc(FAR struct lib_sostream_s
*this, int ch)
while (get_errno() == EINTR);
}
+/****************************************************************************
+ * Name: stdsostream_puts
+ ****************************************************************************/
+
+static int stdsostream_puts(FAR struct lib_sostream_s *this,
+ FAR const void *buffer, int len)
Review Comment:
```suggestion
FAR const void *buffer, int len)
```
##########
libs/libc/stream/lib_rawsostream.c:
##########
@@ -73,6 +73,45 @@ static void rawsostream_putc(FAR struct lib_sostream_s
*this, int ch)
while (errcode == EINTR);
}
+/****************************************************************************
+ * Name: rawsostream_puts
+ ****************************************************************************/
+
+static int rawsostream_puts(FAR struct lib_sostream_s *this,
+ FAR const void *buffer, int len)
Review Comment:
```suggestion
FAR const void *buffer, int len)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]