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