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