mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
platform/Stream.cpp@181:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 181:57724642e740
- Parent:
- 167:e84263d55307
- Child:
- 186:707f6e361f3e
mbed-dev library. Release version 159.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 167:e84263d55307 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 167:e84263d55307 | 2 | * Copyright (c) 2006-2013 ARM Limited |
AnnaBridge | 167:e84263d55307 | 3 | * |
AnnaBridge | 167:e84263d55307 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 167:e84263d55307 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 167:e84263d55307 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 167:e84263d55307 | 7 | * |
AnnaBridge | 167:e84263d55307 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 167:e84263d55307 | 9 | * |
AnnaBridge | 167:e84263d55307 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 167:e84263d55307 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 167:e84263d55307 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 167:e84263d55307 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 167:e84263d55307 | 14 | * limitations under the License. |
AnnaBridge | 167:e84263d55307 | 15 | */ |
AnnaBridge | 167:e84263d55307 | 16 | #include "platform/Stream.h" |
AnnaBridge | 167:e84263d55307 | 17 | #include "platform/mbed_error.h" |
AnnaBridge | 167:e84263d55307 | 18 | #include <errno.h> |
AnnaBridge | 167:e84263d55307 | 19 | |
AnnaBridge | 167:e84263d55307 | 20 | namespace mbed { |
AnnaBridge | 167:e84263d55307 | 21 | |
AnnaBridge | 167:e84263d55307 | 22 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
AnnaBridge | 167:e84263d55307 | 23 | // No lock needed in constructor |
AnnaBridge | 167:e84263d55307 | 24 | /* open ourselves */ |
AnnaBridge | 167:e84263d55307 | 25 | _file = fdopen(this, "w+"); |
AnnaBridge | 167:e84263d55307 | 26 | // fdopen() will make us buffered because Stream::isatty() |
AnnaBridge | 167:e84263d55307 | 27 | // wrongly returns zero which is not being changed for |
AnnaBridge | 167:e84263d55307 | 28 | // backward compatibility |
AnnaBridge | 167:e84263d55307 | 29 | if (_file) { |
AnnaBridge | 167:e84263d55307 | 30 | mbed_set_unbuffered_stream(_file); |
AnnaBridge | 167:e84263d55307 | 31 | } else { |
AnnaBridge | 167:e84263d55307 | 32 | error("Stream obj failure, errno=%d\r\n", errno); |
AnnaBridge | 167:e84263d55307 | 33 | } |
AnnaBridge | 167:e84263d55307 | 34 | } |
AnnaBridge | 167:e84263d55307 | 35 | |
AnnaBridge | 167:e84263d55307 | 36 | Stream::~Stream() { |
AnnaBridge | 167:e84263d55307 | 37 | // No lock can be used in destructor |
AnnaBridge | 167:e84263d55307 | 38 | fclose(_file); |
AnnaBridge | 167:e84263d55307 | 39 | } |
AnnaBridge | 167:e84263d55307 | 40 | |
AnnaBridge | 167:e84263d55307 | 41 | int Stream::putc(int c) { |
AnnaBridge | 167:e84263d55307 | 42 | lock(); |
AnnaBridge | 167:e84263d55307 | 43 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 44 | int ret = std::fputc(c, _file); |
AnnaBridge | 167:e84263d55307 | 45 | unlock(); |
AnnaBridge | 167:e84263d55307 | 46 | return ret; |
AnnaBridge | 167:e84263d55307 | 47 | } |
AnnaBridge | 167:e84263d55307 | 48 | int Stream::puts(const char *s) { |
AnnaBridge | 167:e84263d55307 | 49 | lock(); |
AnnaBridge | 167:e84263d55307 | 50 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 51 | int ret = std::fputs(s, _file); |
AnnaBridge | 167:e84263d55307 | 52 | unlock(); |
AnnaBridge | 167:e84263d55307 | 53 | return ret; |
AnnaBridge | 167:e84263d55307 | 54 | } |
AnnaBridge | 167:e84263d55307 | 55 | int Stream::getc() { |
AnnaBridge | 167:e84263d55307 | 56 | lock(); |
AnnaBridge | 167:e84263d55307 | 57 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 58 | int ret = mbed_getc(_file); |
AnnaBridge | 167:e84263d55307 | 59 | unlock(); |
AnnaBridge | 167:e84263d55307 | 60 | return ret; |
AnnaBridge | 167:e84263d55307 | 61 | } |
AnnaBridge | 167:e84263d55307 | 62 | char* Stream::gets(char *s, int size) { |
AnnaBridge | 167:e84263d55307 | 63 | lock(); |
AnnaBridge | 167:e84263d55307 | 64 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 65 | char *ret = mbed_gets(s,size,_file); |
AnnaBridge | 167:e84263d55307 | 66 | unlock(); |
AnnaBridge | 167:e84263d55307 | 67 | return ret; |
AnnaBridge | 167:e84263d55307 | 68 | } |
AnnaBridge | 167:e84263d55307 | 69 | |
AnnaBridge | 167:e84263d55307 | 70 | int Stream::close() { |
AnnaBridge | 167:e84263d55307 | 71 | return 0; |
AnnaBridge | 167:e84263d55307 | 72 | } |
AnnaBridge | 167:e84263d55307 | 73 | |
AnnaBridge | 167:e84263d55307 | 74 | ssize_t Stream::write(const void* buffer, size_t length) { |
AnnaBridge | 167:e84263d55307 | 75 | const char* ptr = (const char*)buffer; |
AnnaBridge | 167:e84263d55307 | 76 | const char* end = ptr + length; |
AnnaBridge | 167:e84263d55307 | 77 | |
AnnaBridge | 167:e84263d55307 | 78 | lock(); |
AnnaBridge | 167:e84263d55307 | 79 | while (ptr != end) { |
AnnaBridge | 167:e84263d55307 | 80 | if (_putc(*ptr++) == EOF) { |
AnnaBridge | 167:e84263d55307 | 81 | break; |
AnnaBridge | 167:e84263d55307 | 82 | } |
AnnaBridge | 167:e84263d55307 | 83 | } |
AnnaBridge | 167:e84263d55307 | 84 | unlock(); |
AnnaBridge | 167:e84263d55307 | 85 | |
AnnaBridge | 167:e84263d55307 | 86 | return ptr - (const char*)buffer; |
AnnaBridge | 167:e84263d55307 | 87 | } |
AnnaBridge | 167:e84263d55307 | 88 | |
AnnaBridge | 167:e84263d55307 | 89 | ssize_t Stream::read(void* buffer, size_t length) { |
AnnaBridge | 167:e84263d55307 | 90 | char* ptr = (char*)buffer; |
AnnaBridge | 167:e84263d55307 | 91 | char* end = ptr + length; |
AnnaBridge | 167:e84263d55307 | 92 | |
AnnaBridge | 167:e84263d55307 | 93 | lock(); |
AnnaBridge | 167:e84263d55307 | 94 | while (ptr != end) { |
AnnaBridge | 167:e84263d55307 | 95 | int c = _getc(); |
AnnaBridge | 167:e84263d55307 | 96 | if (c==EOF) break; |
AnnaBridge | 167:e84263d55307 | 97 | *ptr++ = c; |
AnnaBridge | 167:e84263d55307 | 98 | } |
AnnaBridge | 167:e84263d55307 | 99 | unlock(); |
AnnaBridge | 167:e84263d55307 | 100 | |
AnnaBridge | 167:e84263d55307 | 101 | return ptr - (const char*)buffer; |
AnnaBridge | 167:e84263d55307 | 102 | } |
AnnaBridge | 167:e84263d55307 | 103 | |
AnnaBridge | 167:e84263d55307 | 104 | off_t Stream::seek(off_t offset, int whence) { |
AnnaBridge | 167:e84263d55307 | 105 | return 0; |
AnnaBridge | 167:e84263d55307 | 106 | } |
AnnaBridge | 167:e84263d55307 | 107 | |
AnnaBridge | 167:e84263d55307 | 108 | off_t Stream::tell() { |
AnnaBridge | 167:e84263d55307 | 109 | return 0; |
AnnaBridge | 167:e84263d55307 | 110 | } |
AnnaBridge | 167:e84263d55307 | 111 | |
AnnaBridge | 167:e84263d55307 | 112 | void Stream::rewind() { |
AnnaBridge | 167:e84263d55307 | 113 | } |
AnnaBridge | 167:e84263d55307 | 114 | |
AnnaBridge | 167:e84263d55307 | 115 | int Stream::isatty() { |
AnnaBridge | 167:e84263d55307 | 116 | return 0; |
AnnaBridge | 167:e84263d55307 | 117 | } |
AnnaBridge | 167:e84263d55307 | 118 | |
AnnaBridge | 167:e84263d55307 | 119 | int Stream::sync() { |
AnnaBridge | 167:e84263d55307 | 120 | return 0; |
AnnaBridge | 167:e84263d55307 | 121 | } |
AnnaBridge | 167:e84263d55307 | 122 | |
AnnaBridge | 167:e84263d55307 | 123 | off_t Stream::size() { |
AnnaBridge | 167:e84263d55307 | 124 | return 0; |
AnnaBridge | 167:e84263d55307 | 125 | } |
AnnaBridge | 167:e84263d55307 | 126 | |
AnnaBridge | 167:e84263d55307 | 127 | int Stream::printf(const char* format, ...) { |
AnnaBridge | 167:e84263d55307 | 128 | lock(); |
AnnaBridge | 167:e84263d55307 | 129 | std::va_list arg; |
AnnaBridge | 167:e84263d55307 | 130 | va_start(arg, format); |
AnnaBridge | 167:e84263d55307 | 131 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 132 | int r = vfprintf(_file, format, arg); |
AnnaBridge | 167:e84263d55307 | 133 | va_end(arg); |
AnnaBridge | 167:e84263d55307 | 134 | unlock(); |
AnnaBridge | 167:e84263d55307 | 135 | return r; |
AnnaBridge | 167:e84263d55307 | 136 | } |
AnnaBridge | 167:e84263d55307 | 137 | |
AnnaBridge | 167:e84263d55307 | 138 | int Stream::scanf(const char* format, ...) { |
AnnaBridge | 167:e84263d55307 | 139 | lock(); |
AnnaBridge | 167:e84263d55307 | 140 | std::va_list arg; |
AnnaBridge | 167:e84263d55307 | 141 | va_start(arg, format); |
AnnaBridge | 167:e84263d55307 | 142 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 143 | int r = vfscanf(_file, format, arg); |
AnnaBridge | 167:e84263d55307 | 144 | va_end(arg); |
AnnaBridge | 167:e84263d55307 | 145 | unlock(); |
AnnaBridge | 167:e84263d55307 | 146 | return r; |
AnnaBridge | 167:e84263d55307 | 147 | } |
AnnaBridge | 167:e84263d55307 | 148 | |
AnnaBridge | 167:e84263d55307 | 149 | int Stream::vprintf(const char* format, std::va_list args) { |
AnnaBridge | 167:e84263d55307 | 150 | lock(); |
AnnaBridge | 167:e84263d55307 | 151 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 152 | int r = vfprintf(_file, format, args); |
AnnaBridge | 167:e84263d55307 | 153 | unlock(); |
AnnaBridge | 167:e84263d55307 | 154 | return r; |
AnnaBridge | 167:e84263d55307 | 155 | } |
AnnaBridge | 167:e84263d55307 | 156 | |
AnnaBridge | 167:e84263d55307 | 157 | int Stream::vscanf(const char* format, std::va_list args) { |
AnnaBridge | 167:e84263d55307 | 158 | lock(); |
AnnaBridge | 167:e84263d55307 | 159 | fflush(_file); |
AnnaBridge | 167:e84263d55307 | 160 | int r = vfscanf(_file, format, args); |
AnnaBridge | 167:e84263d55307 | 161 | unlock(); |
AnnaBridge | 167:e84263d55307 | 162 | return r; |
AnnaBridge | 167:e84263d55307 | 163 | } |
AnnaBridge | 167:e84263d55307 | 164 | |
AnnaBridge | 167:e84263d55307 | 165 | } // namespace mbed |