init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:12:37 2018 -0700
Revision:
9:d58e77ebd769
add mbed-os library

Who changed what in which revision?

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