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