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