mbed library sources. Supersedes mbed-src.
Dependents: LPCXpresso1769_blinky
Fork of mbed-dev by
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 "Stream.h" 00017 00018 namespace mbed { 00019 00020 Stream::Stream(const char *name) : FileLike(name), _file(NULL) { 00021 // No lock needed in constructor 00022 /* open ourselves */ 00023 char buf[12]; /* :0x12345678 + null byte */ 00024 std::sprintf(buf, ":%p", this); 00025 _file = std::fopen(buf, "w+"); 00026 mbed_set_unbuffered_stream(_file); 00027 } 00028 00029 Stream::~Stream() { 00030 // No lock can be used in destructor 00031 fclose(_file); 00032 } 00033 00034 int Stream::putc(int c) { 00035 lock(); 00036 fflush(_file); 00037 int ret = std::fputc(c, _file); 00038 unlock(); 00039 return ret; 00040 } 00041 int Stream::puts(const char *s) { 00042 lock(); 00043 fflush(_file); 00044 int ret = std::fputs(s, _file); 00045 unlock(); 00046 return ret; 00047 } 00048 int Stream::getc() { 00049 lock(); 00050 fflush(_file); 00051 int ret = mbed_getc(_file); 00052 unlock(); 00053 return ret; 00054 } 00055 char* Stream::gets(char *s, int size) { 00056 lock(); 00057 fflush(_file); 00058 char *ret = mbed_gets(s,size,_file); 00059 unlock(); 00060 return ret; 00061 } 00062 00063 int Stream::close() { 00064 return 0; 00065 } 00066 00067 ssize_t Stream::write(const void* buffer, size_t length) { 00068 const char* ptr = (const char*)buffer; 00069 const char* end = ptr + length; 00070 00071 lock(); 00072 while (ptr != end) { 00073 if (_putc(*ptr++) == EOF) { 00074 break; 00075 } 00076 } 00077 unlock(); 00078 00079 return ptr - (const char*)buffer; 00080 } 00081 00082 ssize_t Stream::read(void* buffer, size_t length) { 00083 char* ptr = (char*)buffer; 00084 char* end = ptr + length; 00085 00086 lock(); 00087 while (ptr != end) { 00088 int c = _getc(); 00089 if (c==EOF) break; 00090 *ptr++ = c; 00091 } 00092 unlock(); 00093 00094 return ptr - (const char*)buffer; 00095 } 00096 00097 off_t Stream::lseek(off_t offset, int whence) { 00098 return 0; 00099 } 00100 00101 int Stream::isatty() { 00102 return 0; 00103 } 00104 00105 int Stream::fsync() { 00106 return 0; 00107 } 00108 00109 off_t Stream::flen() { 00110 return 0; 00111 } 00112 00113 int Stream::printf(const char* format, ...) { 00114 lock(); 00115 std::va_list arg; 00116 va_start(arg, format); 00117 fflush(_file); 00118 int r = vfprintf(_file, format, arg); 00119 va_end(arg); 00120 unlock(); 00121 return r; 00122 } 00123 00124 int Stream::scanf(const char* format, ...) { 00125 lock(); 00126 std::va_list arg; 00127 va_start(arg, format); 00128 fflush(_file); 00129 int r = vfscanf(_file, format, arg); 00130 va_end(arg); 00131 unlock(); 00132 return r; 00133 } 00134 00135 int Stream::vprintf(const char* format, std::va_list args) { 00136 lock(); 00137 fflush(_file); 00138 int r = vfprintf(_file, format, args); 00139 unlock(); 00140 return r; 00141 } 00142 00143 int Stream::vscanf(const char* format, std::va_list args) { 00144 lock(); 00145 fflush(_file); 00146 int r = vfscanf(_file, format, args); 00147 unlock(); 00148 return r; 00149 } 00150 00151 } // namespace mbed
Generated on Tue Jul 12 2022 16:04:26 by 1.7.2