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