Opencv 3.1 project on GR-PEACH board
Fork of gr-peach-opencv-project by
platform/Stream.cpp@170:54ff26da7eb6, 2017-07-04 (annotated)
- Committer:
- thedo
- Date:
- Tue Jul 04 06:23:13 2017 +0000
- Revision:
- 170:54ff26da7eb6
- Parent:
- 166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thedo | 166:3a9487d57a5c | 1 | /* mbed Microcontroller Library |
thedo | 166:3a9487d57a5c | 2 | * Copyright (c) 2006-2013 ARM Limited |
thedo | 166:3a9487d57a5c | 3 | * |
thedo | 166:3a9487d57a5c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
thedo | 166:3a9487d57a5c | 5 | * you may not use this file except in compliance with the License. |
thedo | 166:3a9487d57a5c | 6 | * You may obtain a copy of the License at |
thedo | 166:3a9487d57a5c | 7 | * |
thedo | 166:3a9487d57a5c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
thedo | 166:3a9487d57a5c | 9 | * |
thedo | 166:3a9487d57a5c | 10 | * Unless required by applicable law or agreed to in writing, software |
thedo | 166:3a9487d57a5c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
thedo | 166:3a9487d57a5c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
thedo | 166:3a9487d57a5c | 13 | * See the License for the specific language governing permissions and |
thedo | 166:3a9487d57a5c | 14 | * limitations under the License. |
thedo | 166:3a9487d57a5c | 15 | */ |
thedo | 166:3a9487d57a5c | 16 | #include "platform/Stream.h" |
thedo | 166:3a9487d57a5c | 17 | #include "platform/mbed_error.h" |
thedo | 166:3a9487d57a5c | 18 | #include <errno.h> |
thedo | 166:3a9487d57a5c | 19 | |
thedo | 166:3a9487d57a5c | 20 | namespace mbed { |
thedo | 166:3a9487d57a5c | 21 | |
thedo | 166:3a9487d57a5c | 22 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
thedo | 166:3a9487d57a5c | 23 | // No lock needed in constructor |
thedo | 166:3a9487d57a5c | 24 | /* open ourselves */ |
thedo | 166:3a9487d57a5c | 25 | _file = fdopen(this, "w+"); |
thedo | 166:3a9487d57a5c | 26 | // fdopen() will make us buffered because Stream::isatty() |
thedo | 166:3a9487d57a5c | 27 | // wrongly returns zero which is not being changed for |
thedo | 166:3a9487d57a5c | 28 | // backward compatibility |
thedo | 166:3a9487d57a5c | 29 | if (_file) { |
thedo | 166:3a9487d57a5c | 30 | mbed_set_unbuffered_stream(_file); |
thedo | 166:3a9487d57a5c | 31 | } else { |
thedo | 166:3a9487d57a5c | 32 | error("Stream obj failure, errno=%d\r\n", errno); |
thedo | 166:3a9487d57a5c | 33 | } |
thedo | 166:3a9487d57a5c | 34 | } |
thedo | 166:3a9487d57a5c | 35 | |
thedo | 166:3a9487d57a5c | 36 | Stream::~Stream() { |
thedo | 166:3a9487d57a5c | 37 | // No lock can be used in destructor |
thedo | 166:3a9487d57a5c | 38 | fclose(_file); |
thedo | 166:3a9487d57a5c | 39 | } |
thedo | 166:3a9487d57a5c | 40 | |
thedo | 166:3a9487d57a5c | 41 | int Stream::putc(int c) { |
thedo | 166:3a9487d57a5c | 42 | lock(); |
thedo | 166:3a9487d57a5c | 43 | fflush(_file); |
thedo | 166:3a9487d57a5c | 44 | int ret = std::fputc(c, _file); |
thedo | 166:3a9487d57a5c | 45 | unlock(); |
thedo | 166:3a9487d57a5c | 46 | return ret; |
thedo | 166:3a9487d57a5c | 47 | } |
thedo | 166:3a9487d57a5c | 48 | int Stream::puts(const char *s) { |
thedo | 166:3a9487d57a5c | 49 | lock(); |
thedo | 166:3a9487d57a5c | 50 | fflush(_file); |
thedo | 166:3a9487d57a5c | 51 | int ret = std::fputs(s, _file); |
thedo | 166:3a9487d57a5c | 52 | unlock(); |
thedo | 166:3a9487d57a5c | 53 | return ret; |
thedo | 166:3a9487d57a5c | 54 | } |
thedo | 166:3a9487d57a5c | 55 | int Stream::getc() { |
thedo | 166:3a9487d57a5c | 56 | lock(); |
thedo | 166:3a9487d57a5c | 57 | fflush(_file); |
thedo | 166:3a9487d57a5c | 58 | int ret = mbed_getc(_file); |
thedo | 166:3a9487d57a5c | 59 | unlock(); |
thedo | 166:3a9487d57a5c | 60 | return ret; |
thedo | 166:3a9487d57a5c | 61 | } |
thedo | 166:3a9487d57a5c | 62 | char* Stream::gets(char *s, int size) { |
thedo | 166:3a9487d57a5c | 63 | lock(); |
thedo | 166:3a9487d57a5c | 64 | fflush(_file); |
thedo | 166:3a9487d57a5c | 65 | char *ret = mbed_gets(s,size,_file); |
thedo | 166:3a9487d57a5c | 66 | unlock(); |
thedo | 166:3a9487d57a5c | 67 | return ret; |
thedo | 166:3a9487d57a5c | 68 | } |
thedo | 166:3a9487d57a5c | 69 | |
thedo | 166:3a9487d57a5c | 70 | int Stream::close() { |
thedo | 166:3a9487d57a5c | 71 | return 0; |
thedo | 166:3a9487d57a5c | 72 | } |
thedo | 166:3a9487d57a5c | 73 | |
thedo | 166:3a9487d57a5c | 74 | ssize_t Stream::write(const void* buffer, size_t length) { |
thedo | 166:3a9487d57a5c | 75 | const char* ptr = (const char*)buffer; |
thedo | 166:3a9487d57a5c | 76 | const char* end = ptr + length; |
thedo | 166:3a9487d57a5c | 77 | |
thedo | 166:3a9487d57a5c | 78 | lock(); |
thedo | 166:3a9487d57a5c | 79 | while (ptr != end) { |
thedo | 166:3a9487d57a5c | 80 | if (_putc(*ptr++) == EOF) { |
thedo | 166:3a9487d57a5c | 81 | break; |
thedo | 166:3a9487d57a5c | 82 | } |
thedo | 166:3a9487d57a5c | 83 | } |
thedo | 166:3a9487d57a5c | 84 | unlock(); |
thedo | 166:3a9487d57a5c | 85 | |
thedo | 166:3a9487d57a5c | 86 | return ptr - (const char*)buffer; |
thedo | 166:3a9487d57a5c | 87 | } |
thedo | 166:3a9487d57a5c | 88 | |
thedo | 166:3a9487d57a5c | 89 | ssize_t Stream::read(void* buffer, size_t length) { |
thedo | 166:3a9487d57a5c | 90 | char* ptr = (char*)buffer; |
thedo | 166:3a9487d57a5c | 91 | char* end = ptr + length; |
thedo | 166:3a9487d57a5c | 92 | |
thedo | 166:3a9487d57a5c | 93 | lock(); |
thedo | 166:3a9487d57a5c | 94 | while (ptr != end) { |
thedo | 166:3a9487d57a5c | 95 | int c = _getc(); |
thedo | 166:3a9487d57a5c | 96 | if (c==EOF) break; |
thedo | 166:3a9487d57a5c | 97 | *ptr++ = c; |
thedo | 166:3a9487d57a5c | 98 | } |
thedo | 166:3a9487d57a5c | 99 | unlock(); |
thedo | 166:3a9487d57a5c | 100 | |
thedo | 166:3a9487d57a5c | 101 | return ptr - (const char*)buffer; |
thedo | 166:3a9487d57a5c | 102 | } |
thedo | 166:3a9487d57a5c | 103 | |
thedo | 166:3a9487d57a5c | 104 | off_t Stream::seek(off_t offset, int whence) { |
thedo | 166:3a9487d57a5c | 105 | return 0; |
thedo | 166:3a9487d57a5c | 106 | } |
thedo | 166:3a9487d57a5c | 107 | |
thedo | 166:3a9487d57a5c | 108 | off_t Stream::tell() { |
thedo | 166:3a9487d57a5c | 109 | return 0; |
thedo | 166:3a9487d57a5c | 110 | } |
thedo | 166:3a9487d57a5c | 111 | |
thedo | 166:3a9487d57a5c | 112 | void Stream::rewind() { |
thedo | 166:3a9487d57a5c | 113 | } |
thedo | 166:3a9487d57a5c | 114 | |
thedo | 166:3a9487d57a5c | 115 | int Stream::isatty() { |
thedo | 166:3a9487d57a5c | 116 | return 0; |
thedo | 166:3a9487d57a5c | 117 | } |
thedo | 166:3a9487d57a5c | 118 | |
thedo | 166:3a9487d57a5c | 119 | int Stream::sync() { |
thedo | 166:3a9487d57a5c | 120 | return 0; |
thedo | 166:3a9487d57a5c | 121 | } |
thedo | 166:3a9487d57a5c | 122 | |
thedo | 166:3a9487d57a5c | 123 | off_t Stream::size() { |
thedo | 166:3a9487d57a5c | 124 | return 0; |
thedo | 166:3a9487d57a5c | 125 | } |
thedo | 166:3a9487d57a5c | 126 | |
thedo | 166:3a9487d57a5c | 127 | int Stream::printf(const char* format, ...) { |
thedo | 166:3a9487d57a5c | 128 | lock(); |
thedo | 166:3a9487d57a5c | 129 | std::va_list arg; |
thedo | 166:3a9487d57a5c | 130 | va_start(arg, format); |
thedo | 166:3a9487d57a5c | 131 | fflush(_file); |
thedo | 166:3a9487d57a5c | 132 | int r = vfprintf(_file, format, arg); |
thedo | 166:3a9487d57a5c | 133 | va_end(arg); |
thedo | 166:3a9487d57a5c | 134 | unlock(); |
thedo | 166:3a9487d57a5c | 135 | return r; |
thedo | 166:3a9487d57a5c | 136 | } |
thedo | 166:3a9487d57a5c | 137 | |
thedo | 166:3a9487d57a5c | 138 | int Stream::scanf(const char* format, ...) { |
thedo | 166:3a9487d57a5c | 139 | lock(); |
thedo | 166:3a9487d57a5c | 140 | std::va_list arg; |
thedo | 166:3a9487d57a5c | 141 | va_start(arg, format); |
thedo | 166:3a9487d57a5c | 142 | fflush(_file); |
thedo | 166:3a9487d57a5c | 143 | int r = vfscanf(_file, format, arg); |
thedo | 166:3a9487d57a5c | 144 | va_end(arg); |
thedo | 166:3a9487d57a5c | 145 | unlock(); |
thedo | 166:3a9487d57a5c | 146 | return r; |
thedo | 166:3a9487d57a5c | 147 | } |
thedo | 166:3a9487d57a5c | 148 | |
thedo | 166:3a9487d57a5c | 149 | int Stream::vprintf(const char* format, std::va_list args) { |
thedo | 166:3a9487d57a5c | 150 | lock(); |
thedo | 166:3a9487d57a5c | 151 | fflush(_file); |
thedo | 166:3a9487d57a5c | 152 | int r = vfprintf(_file, format, args); |
thedo | 166:3a9487d57a5c | 153 | unlock(); |
thedo | 166:3a9487d57a5c | 154 | return r; |
thedo | 166:3a9487d57a5c | 155 | } |
thedo | 166:3a9487d57a5c | 156 | |
thedo | 166:3a9487d57a5c | 157 | int Stream::vscanf(const char* format, std::va_list args) { |
thedo | 166:3a9487d57a5c | 158 | lock(); |
thedo | 166:3a9487d57a5c | 159 | fflush(_file); |
thedo | 166:3a9487d57a5c | 160 | int r = vfscanf(_file, format, args); |
thedo | 166:3a9487d57a5c | 161 | unlock(); |
thedo | 166:3a9487d57a5c | 162 | return r; |
thedo | 166:3a9487d57a5c | 163 | } |
thedo | 166:3a9487d57a5c | 164 | |
thedo | 166:3a9487d57a5c | 165 | } // namespace mbed |
thedo | 166:3a9487d57a5c | 166 |