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