xiaoxiang781216 commented on code in PR #8641: URL: https://github.com/apache/nuttx/pull/8641#discussion_r1118873360
########## libs/libc/stream/lib_bufferedoutstream.c: ########## @@ -0,0 +1,146 @@ +/**************************************************************************** + * libs/libc/stream/lib_bufferedoutstream.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 <nuttx/config.h> + +#include <unistd.h> +#include <assert.h> +#include <errno.h> + +#include <nuttx/fs/fs.h> + +#include "libc.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: bufferedoutstream_flush + ****************************************************************************/ + +static int bufferedoutstream_flush(FAR struct lib_outstream_s *this) +{ + FAR struct lib_bufferedoutstream_s *rthis = + (FAR struct lib_bufferedoutstream_s *)this; + int ret = OK; + int i; + + if (rthis->pending > 0) + { + /* Try to write full internal buffer to fd */ + + for (i = 0; i < rthis->pending; ) + { + ret = _NX_WRITE(rthis->fd, rthis->buffer + i, rthis->pending - i); + + if (ret > 0) + { + i += ret; + } + else + { + break; + } + } + + if (i == rthis->pending) + { + /* Whole buffer write ok, clear pending */ + + rthis->pending = 0; + ret = OK; + } + else if (i > 0) + { + /* Only part of the buffer write ok, update buffer info */ + + memmove(rthis->buffer, rthis->buffer + i, rthis->pending - i); + rthis->pending -= i; + } + else + { + /* Can't write anything, report error */ + + ret = ERROR; Review Comment: why hardcode to ERROR ########## libs/libc/stream/lib_bufferedoutstream.c: ########## @@ -0,0 +1,146 @@ +/**************************************************************************** + * libs/libc/stream/lib_bufferedoutstream.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 <nuttx/config.h> + +#include <unistd.h> +#include <assert.h> +#include <errno.h> + +#include <nuttx/fs/fs.h> + +#include "libc.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: bufferedoutstream_flush + ****************************************************************************/ + +static int bufferedoutstream_flush(FAR struct lib_outstream_s *this) +{ + FAR struct lib_bufferedoutstream_s *rthis = + (FAR struct lib_bufferedoutstream_s *)this; + int ret = OK; + int i; + + if (rthis->pending > 0) + { + /* Try to write full internal buffer to fd */ + + for (i = 0; i < rthis->pending; ) + { + ret = _NX_WRITE(rthis->fd, rthis->buffer + i, rthis->pending - i); + + if (ret > 0) + { + i += ret; + } + else + { + break; + } + } + + if (i == rthis->pending) + { + /* Whole buffer write ok, clear pending */ + + rthis->pending = 0; + ret = OK; + } + else if (i > 0) + { + /* Only part of the buffer write ok, update buffer info */ + + memmove(rthis->buffer, rthis->buffer + i, rthis->pending - i); + rthis->pending -= i; + } + else + { + /* Can't write anything, report error */ + + ret = ERROR; + } + } + + return ret; +} + +/**************************************************************************** + * Name: bufferedoutstream_putc + ****************************************************************************/ + +static void bufferedoutstream_putc(FAR struct lib_outstream_s *this, int ch) +{ + FAR struct lib_bufferedoutstream_s *rthis = + (FAR struct lib_bufferedoutstream_s *)this; + + DEBUGASSERT(rthis->pending < CONFIG_STREAM_OUT_BUFFER_SIZE); + + rthis->buffer[rthis->pending++] = ch; + + if (rthis->pending == CONFIG_STREAM_OUT_BUFFER_SIZE) + { + bufferedoutstream_flush(this); + } +} + +/**************************************************************************** + * Name: buffereoutstream_puts + ****************************************************************************/ + +static int buffereoutstream_puts(FAR struct lib_outstream_s *this, + FAR const void *buf, int len) +{ + int i; + + for (i = 0; i < len; i++) + { + bufferedoutstream_putc(this, ((FAR char *)buf)[i]); Review Comment: let's bufferedoutstream_putc call bufferedoutstream_puts instead ########## libs/libc/stream/lib_bufferedoutstream.c: ########## @@ -0,0 +1,146 @@ +/**************************************************************************** + * libs/libc/stream/lib_bufferedoutstream.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 <nuttx/config.h> + +#include <unistd.h> +#include <assert.h> +#include <errno.h> + +#include <nuttx/fs/fs.h> + +#include "libc.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: bufferedoutstream_flush + ****************************************************************************/ + +static int bufferedoutstream_flush(FAR struct lib_outstream_s *this) +{ + FAR struct lib_bufferedoutstream_s *rthis = + (FAR struct lib_bufferedoutstream_s *)this; + int ret = OK; + int i; + + if (rthis->pending > 0) + { + /* Try to write full internal buffer to fd */ + + for (i = 0; i < rthis->pending; ) + { + ret = _NX_WRITE(rthis->fd, rthis->buffer + i, rthis->pending - i); + + if (ret > 0) + { + i += ret; + } + else + { + break; + } + } + + if (i == rthis->pending) + { + /* Whole buffer write ok, clear pending */ + + rthis->pending = 0; + ret = OK; + } + else if (i > 0) + { + /* Only part of the buffer write ok, update buffer info */ + + memmove(rthis->buffer, rthis->buffer + i, rthis->pending - i); + rthis->pending -= i; + } + else + { + /* Can't write anything, report error */ + + ret = ERROR; + } + } + + return ret; +} + +/**************************************************************************** + * Name: bufferedoutstream_putc + ****************************************************************************/ + +static void bufferedoutstream_putc(FAR struct lib_outstream_s *this, int ch) +{ + FAR struct lib_bufferedoutstream_s *rthis = + (FAR struct lib_bufferedoutstream_s *)this; + + DEBUGASSERT(rthis->pending < CONFIG_STREAM_OUT_BUFFER_SIZE); + + rthis->buffer[rthis->pending++] = ch; + + if (rthis->pending == CONFIG_STREAM_OUT_BUFFER_SIZE) + { + bufferedoutstream_flush(this); + } +} + +/**************************************************************************** + * Name: buffereoutstream_puts + ****************************************************************************/ + +static int buffereoutstream_puts(FAR struct lib_outstream_s *this, + FAR const void *buf, int len) Review Comment: ```suggestion FAR const void *buf, int len) ``` ########## include/nuttx/streams.h: ########## @@ -217,6 +217,14 @@ struct lib_rawsostream_s int fd; }; +struct lib_bufferedoutstream_s +{ + struct lib_outstream_s public; + int fd; Review Comment: let's change fd to: FAR struct lib_outstream_s *backend; so the buffer can apply to all stream -- 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: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org