BA / Mbed OS BaBoRo1
Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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