mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stream.cpp Source File

Stream.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "Stream.h"
00017 
00018 namespace mbed {
00019 
00020 Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
00021     /* open ourselves */
00022     char buf[12]; /* :0x12345678 + null byte */
00023     std::sprintf(buf, ":%p", this);
00024     _file = std::fopen(buf, "w+");
00025     mbed_set_unbuffered_stream(_file);
00026 }
00027 
00028 Stream::~Stream() {
00029     fclose(_file);
00030 }
00031 
00032 int Stream::putc(int c) {
00033     fflush(_file);
00034     return std::fputc(c, _file);
00035 }
00036 int Stream::puts(const char *s) {
00037     fflush(_file);
00038     return std::fputs(s, _file);
00039 }
00040 int Stream::getc() {
00041     fflush(_file);
00042     return mbed_getc(_file);   
00043 }
00044 char* Stream::gets(char *s, int size) {
00045     fflush(_file);
00046     return mbed_gets(s,size,_file);
00047 }
00048 
00049 int Stream::close() {
00050     return 0;
00051 }
00052 
00053 ssize_t Stream::write(const void* buffer, size_t length) {
00054     const char* ptr = (const char*)buffer;
00055     const char* end = ptr + length;
00056     while (ptr != end) {
00057         if (_putc(*ptr++) == EOF) {
00058             break;
00059         }
00060     }
00061     return ptr - (const char*)buffer;
00062 }
00063 
00064 ssize_t Stream::read(void* buffer, size_t length) {
00065     char* ptr = (char*)buffer;
00066     char* end = ptr + length;
00067     while (ptr != end) {
00068         int c = _getc();
00069         if (c==EOF) break;
00070         *ptr++ = c;
00071     }
00072     return ptr - (const char*)buffer;
00073 }
00074 
00075 off_t Stream::lseek(off_t offset, int whence) {
00076     return 0;
00077 }
00078 
00079 int Stream::isatty() {
00080     return 0;
00081 }
00082 
00083 int Stream::fsync() {
00084     return 0;
00085 }
00086 
00087 off_t Stream::flen() {
00088     return 0;
00089 }
00090 
00091 int Stream::printf(const char* format, ...) {
00092     std::va_list arg;
00093     va_start(arg, format);
00094     fflush(_file);
00095     int r = vfprintf(_file, format, arg);
00096     va_end(arg);
00097     return r;
00098 }
00099 
00100 int Stream::scanf(const char* format, ...) {
00101     std::va_list arg;
00102     va_start(arg, format);
00103     fflush(_file);
00104     int r = vfscanf(_file, format, arg);
00105     va_end(arg);
00106     return r;
00107 }
00108 
00109 int Stream::vprintf(const char* format, std::va_list args) {
00110     fflush(_file);
00111     int r = vfprintf(_file, format, args);
00112     return r;
00113 }
00114 
00115 int Stream::vscanf(const char* format, std::va_list args) {
00116     fflush(_file);
00117     int r = vfscanf(_file, format, args);
00118     return r;
00119 }
00120 
00121 } // namespace mbed