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.
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 // No lock needed in constructor 00024 /* open ourselves */ 00025 _file = fdopen(this, "w+"); 00026 // fdopen() will make us buffered because Stream::isatty() 00027 // wrongly returns zero which is not being changed for 00028 // backward compatibility 00029 if (_file) { 00030 mbed_set_unbuffered_stream(_file); 00031 } else { 00032 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file); 00033 } 00034 } 00035 00036 Stream::~Stream() { 00037 // No lock can be used in destructor 00038 fclose(_file); 00039 } 00040 00041 int Stream::putc(int c) { 00042 lock(); 00043 fflush(_file); 00044 int ret = std::fputc(c, _file); 00045 unlock(); 00046 return ret; 00047 } 00048 int Stream::puts(const char *s) { 00049 lock(); 00050 fflush(_file); 00051 int ret = std::fputs(s, _file); 00052 unlock(); 00053 return ret; 00054 } 00055 int Stream::getc() { 00056 lock(); 00057 fflush(_file); 00058 int ret = mbed_getc(_file); 00059 unlock(); 00060 return ret; 00061 } 00062 char* Stream::gets(char *s, int size) { 00063 lock(); 00064 fflush(_file); 00065 char *ret = mbed_gets(s,size,_file); 00066 unlock(); 00067 return ret; 00068 } 00069 00070 int Stream::close() { 00071 return 0; 00072 } 00073 00074 ssize_t Stream::write(const void* buffer, size_t length) { 00075 const char* ptr = (const char*)buffer; 00076 const char* end = ptr + length; 00077 00078 lock(); 00079 while (ptr != end) { 00080 if (_putc(*ptr++) == EOF) { 00081 break; 00082 } 00083 } 00084 unlock(); 00085 00086 return ptr - (const char*)buffer; 00087 } 00088 00089 ssize_t Stream::read(void* buffer, size_t length) { 00090 char* ptr = (char*)buffer; 00091 char* end = ptr + length; 00092 00093 lock(); 00094 while (ptr != end) { 00095 int c = _getc(); 00096 if (c==EOF) break; 00097 *ptr++ = c; 00098 } 00099 unlock(); 00100 00101 return ptr - (const char*)buffer; 00102 } 00103 00104 off_t Stream::seek(off_t offset, int whence) { 00105 return 0; 00106 } 00107 00108 off_t Stream::tell() { 00109 return 0; 00110 } 00111 00112 void Stream::rewind() { 00113 } 00114 00115 int Stream::isatty() { 00116 return 0; 00117 } 00118 00119 int Stream::sync() { 00120 return 0; 00121 } 00122 00123 off_t Stream::size() { 00124 return 0; 00125 } 00126 00127 int Stream::printf(const char* format, ...) { 00128 lock(); 00129 std::va_list arg; 00130 va_start(arg, format); 00131 fflush(_file); 00132 int r = vfprintf(_file, format, arg); 00133 va_end(arg); 00134 unlock(); 00135 return r; 00136 } 00137 00138 int Stream::scanf(const char* format, ...) { 00139 lock(); 00140 std::va_list arg; 00141 va_start(arg, format); 00142 fflush(_file); 00143 int r = vfscanf(_file, format, arg); 00144 va_end(arg); 00145 unlock(); 00146 return r; 00147 } 00148 00149 int Stream::vprintf(const char* format, std::va_list args) { 00150 lock(); 00151 fflush(_file); 00152 int r = vfprintf(_file, format, args); 00153 unlock(); 00154 return r; 00155 } 00156 00157 int Stream::vscanf(const char* format, std::va_list args) { 00158 lock(); 00159 fflush(_file); 00160 int r = vfscanf(_file, format, args); 00161 unlock(); 00162 return r; 00163 } 00164 00165 } // namespace mbed
Generated on Tue Jul 12 2022 12:45:49 by
