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