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