4545

Dependents:   LSS_Rev_1

Fork of mbed-dev by Umar Naeem

Committer:
iftaziz
Date:
Wed Aug 23 10:32:38 2017 +0000
Revision:
166:33361e55dd8c
Parent:
160:d5399cc887bb
r1

Who changed what in which revision?

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