mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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