Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stream.cpp Source File

Stream.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "platform/Stream.h"
00018 #include "platform/mbed_error.h"
00019 #include <errno.h>
00020 
00021 namespace mbed {
00022 
00023 Stream::Stream(const char *name) :
00024     FileLike(name)
00025 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00026     , _file(NULL)
00027 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00028 {
00029     // No lock needed in constructor
00030 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00031     /* open ourselves */
00032     _file = fdopen(this, "w+");
00033     // fdopen() will make us buffered because Stream::isatty()
00034     // wrongly returns zero which is not being changed for
00035     // backward compatibility
00036     if (_file) {
00037         mbed_set_unbuffered_stream(_file);
00038     } else {
00039         MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
00040     }
00041 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00042 }
00043 
00044 Stream::~Stream()
00045 {
00046 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00047     // No lock can be used in destructor
00048     fclose(_file);
00049 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00050 }
00051 
00052 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00053 int Stream::putc(int c)
00054 {
00055     lock();
00056     std::fseek(_file, 0, SEEK_CUR);
00057     int ret = std::fputc(c, _file);
00058     unlock();
00059     return ret;
00060 }
00061 int Stream::puts(const char *s)
00062 {
00063     lock();
00064     std::fseek(_file, 0, SEEK_CUR);
00065     int ret = std::fputs(s, _file);
00066     unlock();
00067     return ret;
00068 }
00069 int Stream::getc()
00070 {
00071     lock();
00072     fflush(_file);
00073     int ret = std::fgetc(_file);
00074     unlock();
00075     return ret;
00076 }
00077 char *Stream::gets(char *s, int size)
00078 {
00079     lock();
00080     fflush(_file);
00081     char *ret = std::fgets(s, size, _file);
00082     unlock();
00083     return ret;
00084 }
00085 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00086 
00087 int Stream::close()
00088 {
00089     return 0;
00090 }
00091 
00092 ssize_t Stream::write(const void *buffer, size_t length)
00093 {
00094     const char *ptr = (const char *)buffer;
00095     const char *end = ptr + length;
00096 
00097     lock();
00098     while (ptr != end) {
00099         if (_putc(*ptr++) == EOF) {
00100             break;
00101         }
00102     }
00103     unlock();
00104 
00105     return ptr - (const char *)buffer;
00106 }
00107 
00108 ssize_t Stream::read(void *buffer, size_t length)
00109 {
00110     char *ptr = (char *)buffer;
00111     char *end = ptr + length;
00112 
00113     lock();
00114     while (ptr != end) {
00115         int c = _getc();
00116         if (c == EOF) {
00117             break;
00118         }
00119         *ptr++ = c;
00120     }
00121     unlock();
00122 
00123     return ptr - (const char *)buffer;
00124 }
00125 
00126 off_t Stream::seek(off_t offset, int whence)
00127 {
00128     return 0;
00129 }
00130 
00131 off_t Stream::tell()
00132 {
00133     return 0;
00134 }
00135 
00136 void Stream::rewind()
00137 {
00138 }
00139 
00140 int Stream::isatty()
00141 {
00142     return 0;
00143 }
00144 
00145 int Stream::sync()
00146 {
00147     return 0;
00148 }
00149 
00150 off_t Stream::size()
00151 {
00152     return 0;
00153 }
00154 
00155 #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00156 
00157 int Stream::printf(const char *format, ...)
00158 {
00159     lock();
00160     std::va_list arg;
00161     va_start(arg, format);
00162     std::fseek(_file, 0, SEEK_CUR);
00163     int r = vfprintf(_file, format, arg);
00164     va_end(arg);
00165     unlock();
00166     return r;
00167 }
00168 
00169 int Stream::scanf(const char *format, ...)
00170 {
00171     lock();
00172     std::va_list arg;
00173     va_start(arg, format);
00174     fflush(_file);
00175     int r = vfscanf(_file, format, arg);
00176     va_end(arg);
00177     unlock();
00178     return r;
00179 }
00180 
00181 int Stream::vprintf(const char *format, std::va_list args)
00182 {
00183     lock();
00184     std::fseek(_file, 0, SEEK_CUR);
00185     int r = vfprintf(_file, format, args);
00186     unlock();
00187     return r;
00188 }
00189 
00190 int Stream::vscanf(const char *format, std::va_list args)
00191 {
00192     lock();
00193     fflush(_file);
00194     int r = vfscanf(_file, format, args);
00195     unlock();
00196     return r;
00197 }
00198 
00199 #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY
00200 
00201 } // namespace mbed