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