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