mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
emilmont
Date:
Fri Jun 14 17:49:17 2013 +0100
Revision:
10:3bc89ef62ce7
Parent:
9:0ce32e54c9a7
Child:
13:0645d8841f51
Unify mbed library sources

Who changed what in which revision?

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