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