mbed library sources

Dependents:   bare

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Parent:
cpp/Stream.cpp@2:143cac498751
Child:
10:3bc89ef62ce7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

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