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