mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
mbed_official
Date:
Thu Nov 26 10:15:10 2015 +0000
Revision:
28:8e1f0b362990
Parent:
0:9b334a45a8ff
Synchronized with git revision f4b1d30ff45529ee5c6aa689a35962e8d4a2ad9a

Full URL: https://github.com/mbedmicro/mbed/commit/f4b1d30ff45529ee5c6aa689a35962e8d4a2ad9a/

Enhance stream class

Who changed what in which revision?

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