This fork captures the mbed lib v125 for ease of integration into older projects.
Fork of mbed-dev by
common/Stream.cpp@187:92cbb9eec47b, 2018-07-20 (annotated)
- Committer:
- apluscw
- Date:
- Fri Jul 20 21:24:42 2018 +0000
- Revision:
- 187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apluscw | 187:92cbb9eec47b | 1 | /* mbed Microcontroller Library |
apluscw | 187:92cbb9eec47b | 2 | * Copyright (c) 2006-2013 ARM Limited |
apluscw | 187:92cbb9eec47b | 3 | * |
apluscw | 187:92cbb9eec47b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
apluscw | 187:92cbb9eec47b | 5 | * you may not use this file except in compliance with the License. |
apluscw | 187:92cbb9eec47b | 6 | * You may obtain a copy of the License at |
apluscw | 187:92cbb9eec47b | 7 | * |
apluscw | 187:92cbb9eec47b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
apluscw | 187:92cbb9eec47b | 9 | * |
apluscw | 187:92cbb9eec47b | 10 | * Unless required by applicable law or agreed to in writing, software |
apluscw | 187:92cbb9eec47b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
apluscw | 187:92cbb9eec47b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
apluscw | 187:92cbb9eec47b | 13 | * See the License for the specific language governing permissions and |
apluscw | 187:92cbb9eec47b | 14 | * limitations under the License. |
apluscw | 187:92cbb9eec47b | 15 | */ |
apluscw | 187:92cbb9eec47b | 16 | #include "Stream.h" |
apluscw | 187:92cbb9eec47b | 17 | |
apluscw | 187:92cbb9eec47b | 18 | namespace mbed { |
apluscw | 187:92cbb9eec47b | 19 | |
apluscw | 187:92cbb9eec47b | 20 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
apluscw | 187:92cbb9eec47b | 21 | // No lock needed in constructor |
apluscw | 187:92cbb9eec47b | 22 | /* open ourselves */ |
apluscw | 187:92cbb9eec47b | 23 | char buf[12]; /* :0x12345678 + null byte */ |
apluscw | 187:92cbb9eec47b | 24 | std::sprintf(buf, ":%p", this); |
apluscw | 187:92cbb9eec47b | 25 | _file = std::fopen(buf, "w+"); |
apluscw | 187:92cbb9eec47b | 26 | mbed_set_unbuffered_stream(_file); |
apluscw | 187:92cbb9eec47b | 27 | } |
apluscw | 187:92cbb9eec47b | 28 | |
apluscw | 187:92cbb9eec47b | 29 | Stream::~Stream() { |
apluscw | 187:92cbb9eec47b | 30 | // No lock can be used in destructor |
apluscw | 187:92cbb9eec47b | 31 | fclose(_file); |
apluscw | 187:92cbb9eec47b | 32 | } |
apluscw | 187:92cbb9eec47b | 33 | |
apluscw | 187:92cbb9eec47b | 34 | int Stream::putc(int c) { |
apluscw | 187:92cbb9eec47b | 35 | lock(); |
apluscw | 187:92cbb9eec47b | 36 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 37 | int ret = std::fputc(c, _file); |
apluscw | 187:92cbb9eec47b | 38 | unlock(); |
apluscw | 187:92cbb9eec47b | 39 | return ret; |
apluscw | 187:92cbb9eec47b | 40 | } |
apluscw | 187:92cbb9eec47b | 41 | int Stream::puts(const char *s) { |
apluscw | 187:92cbb9eec47b | 42 | lock(); |
apluscw | 187:92cbb9eec47b | 43 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 44 | int ret = std::fputs(s, _file); |
apluscw | 187:92cbb9eec47b | 45 | unlock(); |
apluscw | 187:92cbb9eec47b | 46 | return ret; |
apluscw | 187:92cbb9eec47b | 47 | } |
apluscw | 187:92cbb9eec47b | 48 | int Stream::getc() { |
apluscw | 187:92cbb9eec47b | 49 | lock(); |
apluscw | 187:92cbb9eec47b | 50 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 51 | int ret = mbed_getc(_file); |
apluscw | 187:92cbb9eec47b | 52 | unlock(); |
apluscw | 187:92cbb9eec47b | 53 | return ret; |
apluscw | 187:92cbb9eec47b | 54 | } |
apluscw | 187:92cbb9eec47b | 55 | char* Stream::gets(char *s, int size) { |
apluscw | 187:92cbb9eec47b | 56 | lock(); |
apluscw | 187:92cbb9eec47b | 57 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 58 | char *ret = mbed_gets(s,size,_file); |
apluscw | 187:92cbb9eec47b | 59 | unlock(); |
apluscw | 187:92cbb9eec47b | 60 | return ret; |
apluscw | 187:92cbb9eec47b | 61 | } |
apluscw | 187:92cbb9eec47b | 62 | |
apluscw | 187:92cbb9eec47b | 63 | int Stream::close() { |
apluscw | 187:92cbb9eec47b | 64 | return 0; |
apluscw | 187:92cbb9eec47b | 65 | } |
apluscw | 187:92cbb9eec47b | 66 | |
apluscw | 187:92cbb9eec47b | 67 | ssize_t Stream::write(const void* buffer, size_t length) { |
apluscw | 187:92cbb9eec47b | 68 | const char* ptr = (const char*)buffer; |
apluscw | 187:92cbb9eec47b | 69 | const char* end = ptr + length; |
apluscw | 187:92cbb9eec47b | 70 | |
apluscw | 187:92cbb9eec47b | 71 | lock(); |
apluscw | 187:92cbb9eec47b | 72 | while (ptr != end) { |
apluscw | 187:92cbb9eec47b | 73 | if (_putc(*ptr++) == EOF) { |
apluscw | 187:92cbb9eec47b | 74 | break; |
apluscw | 187:92cbb9eec47b | 75 | } |
apluscw | 187:92cbb9eec47b | 76 | } |
apluscw | 187:92cbb9eec47b | 77 | unlock(); |
apluscw | 187:92cbb9eec47b | 78 | |
apluscw | 187:92cbb9eec47b | 79 | return ptr - (const char*)buffer; |
apluscw | 187:92cbb9eec47b | 80 | } |
apluscw | 187:92cbb9eec47b | 81 | |
apluscw | 187:92cbb9eec47b | 82 | ssize_t Stream::read(void* buffer, size_t length) { |
apluscw | 187:92cbb9eec47b | 83 | char* ptr = (char*)buffer; |
apluscw | 187:92cbb9eec47b | 84 | char* end = ptr + length; |
apluscw | 187:92cbb9eec47b | 85 | |
apluscw | 187:92cbb9eec47b | 86 | lock(); |
apluscw | 187:92cbb9eec47b | 87 | while (ptr != end) { |
apluscw | 187:92cbb9eec47b | 88 | int c = _getc(); |
apluscw | 187:92cbb9eec47b | 89 | if (c==EOF) break; |
apluscw | 187:92cbb9eec47b | 90 | *ptr++ = c; |
apluscw | 187:92cbb9eec47b | 91 | } |
apluscw | 187:92cbb9eec47b | 92 | unlock(); |
apluscw | 187:92cbb9eec47b | 93 | |
apluscw | 187:92cbb9eec47b | 94 | return ptr - (const char*)buffer; |
apluscw | 187:92cbb9eec47b | 95 | } |
apluscw | 187:92cbb9eec47b | 96 | |
apluscw | 187:92cbb9eec47b | 97 | off_t Stream::lseek(off_t offset, int whence) { |
apluscw | 187:92cbb9eec47b | 98 | return 0; |
apluscw | 187:92cbb9eec47b | 99 | } |
apluscw | 187:92cbb9eec47b | 100 | |
apluscw | 187:92cbb9eec47b | 101 | int Stream::isatty() { |
apluscw | 187:92cbb9eec47b | 102 | return 0; |
apluscw | 187:92cbb9eec47b | 103 | } |
apluscw | 187:92cbb9eec47b | 104 | |
apluscw | 187:92cbb9eec47b | 105 | int Stream::fsync() { |
apluscw | 187:92cbb9eec47b | 106 | return 0; |
apluscw | 187:92cbb9eec47b | 107 | } |
apluscw | 187:92cbb9eec47b | 108 | |
apluscw | 187:92cbb9eec47b | 109 | off_t Stream::flen() { |
apluscw | 187:92cbb9eec47b | 110 | return 0; |
apluscw | 187:92cbb9eec47b | 111 | } |
apluscw | 187:92cbb9eec47b | 112 | |
apluscw | 187:92cbb9eec47b | 113 | int Stream::printf(const char* format, ...) { |
apluscw | 187:92cbb9eec47b | 114 | lock(); |
apluscw | 187:92cbb9eec47b | 115 | std::va_list arg; |
apluscw | 187:92cbb9eec47b | 116 | va_start(arg, format); |
apluscw | 187:92cbb9eec47b | 117 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 118 | int r = vfprintf(_file, format, arg); |
apluscw | 187:92cbb9eec47b | 119 | va_end(arg); |
apluscw | 187:92cbb9eec47b | 120 | unlock(); |
apluscw | 187:92cbb9eec47b | 121 | return r; |
apluscw | 187:92cbb9eec47b | 122 | } |
apluscw | 187:92cbb9eec47b | 123 | |
apluscw | 187:92cbb9eec47b | 124 | int Stream::scanf(const char* format, ...) { |
apluscw | 187:92cbb9eec47b | 125 | lock(); |
apluscw | 187:92cbb9eec47b | 126 | std::va_list arg; |
apluscw | 187:92cbb9eec47b | 127 | va_start(arg, format); |
apluscw | 187:92cbb9eec47b | 128 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 129 | int r = vfscanf(_file, format, arg); |
apluscw | 187:92cbb9eec47b | 130 | va_end(arg); |
apluscw | 187:92cbb9eec47b | 131 | unlock(); |
apluscw | 187:92cbb9eec47b | 132 | return r; |
apluscw | 187:92cbb9eec47b | 133 | } |
apluscw | 187:92cbb9eec47b | 134 | |
apluscw | 187:92cbb9eec47b | 135 | int Stream::vprintf(const char* format, std::va_list args) { |
apluscw | 187:92cbb9eec47b | 136 | lock(); |
apluscw | 187:92cbb9eec47b | 137 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 138 | int r = vfprintf(_file, format, args); |
apluscw | 187:92cbb9eec47b | 139 | unlock(); |
apluscw | 187:92cbb9eec47b | 140 | return r; |
apluscw | 187:92cbb9eec47b | 141 | } |
apluscw | 187:92cbb9eec47b | 142 | |
apluscw | 187:92cbb9eec47b | 143 | int Stream::vscanf(const char* format, std::va_list args) { |
apluscw | 187:92cbb9eec47b | 144 | lock(); |
apluscw | 187:92cbb9eec47b | 145 | fflush(_file); |
apluscw | 187:92cbb9eec47b | 146 | int r = vfscanf(_file, format, args); |
apluscw | 187:92cbb9eec47b | 147 | unlock(); |
apluscw | 187:92cbb9eec47b | 148 | return r; |
apluscw | 187:92cbb9eec47b | 149 | } |
apluscw | 187:92cbb9eec47b | 150 | |
apluscw | 187:92cbb9eec47b | 151 | } // namespace mbed |