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