inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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