I don't know
Fork of mbed-src by
common/Stream.cpp@431:255afbe6270c, 2014-12-09 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Dec 09 14:45:08 2014 +0000
- Revision:
- 431:255afbe6270c
- Parent:
- 212:34d62c0b2af6
Synchronized with git revision ea49132428ba76f828a3398a05088186c036de90
Full URL: https://github.com/mbedmicro/mbed/commit/ea49132428ba76f828a3398a05088186c036de90/
Fix IAR serial fgets fgetc
Taken from PR #770:
setbuf(_file, NULL), and std::setvbuf(_file,NULL,_IONBF,NULL) should both give an unbuffered stream (the data is directly written to the input buffer). IAR sets a buffer anyway of size 512 bytes for these calls. Calling setvbuff(_file,buf,_IONBF,NULL) with a buffer that is not a NULL pointer sets the buffer to size one. Which means that as soon as a char is read it is written to the real buffer. If people are interested in looking at this further they can look at the files under ARM/src/dlib: fgets.c, fflush.c, xfrpep.c and xfwprep.c
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+"); |
mbed_official | 431:255afbe6270c | 27 | mbed_set_unbuffered_stream(_file); |
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); |
mbed_official | 431:255afbe6270c | 44 | return mbed_getc(_file); |
bogdanm | 13:0645d8841f51 | 45 | } |
bogdanm | 13:0645d8841f51 | 46 | char* Stream::gets(char *s, int size) { |
bogdanm | 13:0645d8841f51 | 47 | fflush(_file); |
mbed_official | 431:255afbe6270c | 48 | return mbed_gets(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 |