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