Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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