Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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