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