mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stream.cpp Source File

Stream.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "platform/Stream.h"
00018 #include "platform/mbed_error.h"
00019 #include <errno.h>
00020 
00021 namespace mbed {
00022 
00023 Stream::Stream(const char *name) : FileLike(name), _file(NULL)
00024 {
00025     // No lock needed in constructor
00026     /* open ourselves */
00027     _file = fdopen(this, "w+");
00028     // fdopen() will make us buffered because Stream::isatty()
00029     // wrongly returns zero which is not being changed for
00030     // backward compatibility
00031     if (_file) {
00032         mbed_set_unbuffered_stream(_file);
00033     } else {
00034         MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
00035     }
00036 }
00037 
00038 Stream::~Stream()
00039 {
00040     // No lock can be used in destructor
00041     fclose(_file);
00042 }
00043 
00044 int Stream::putc(int c)
00045 {
00046     lock();
00047     std::fseek(_file, 0, SEEK_CUR);
00048     int ret = std::fputc(c, _file);
00049     unlock();
00050     return ret;
00051 }
00052 int Stream::puts(const char *s)
00053 {
00054     lock();
00055     std::fseek(_file, 0, SEEK_CUR);
00056     int ret = std::fputs(s, _file);
00057     unlock();
00058     return ret;
00059 }
00060 int Stream::getc()
00061 {
00062     lock();
00063     fflush(_file);
00064     int ret = std::fgetc(_file);
00065     unlock();
00066     return ret;
00067 }
00068 char *Stream::gets(char *s, int size)
00069 {
00070     lock();
00071     fflush(_file);
00072     char *ret = std::fgets(s, size, _file);
00073     unlock();
00074     return ret;
00075 }
00076 
00077 int Stream::close()
00078 {
00079     return 0;
00080 }
00081 
00082 ssize_t Stream::write(const void *buffer, size_t length)
00083 {
00084     const char *ptr = (const char *)buffer;
00085     const char *end = ptr + length;
00086 
00087     lock();
00088     while (ptr != end) {
00089         if (_putc(*ptr++) == EOF) {
00090             break;
00091         }
00092     }
00093     unlock();
00094 
00095     return ptr - (const char *)buffer;
00096 }
00097 
00098 ssize_t Stream::read(void *buffer, size_t length)
00099 {
00100     char *ptr = (char *)buffer;
00101     char *end = ptr + length;
00102 
00103     lock();
00104     while (ptr != end) {
00105         int c = _getc();
00106         if (c == EOF) {
00107             break;
00108         }
00109         *ptr++ = c;
00110     }
00111     unlock();
00112 
00113     return ptr - (const char *)buffer;
00114 }
00115 
00116 off_t Stream::seek(off_t offset, int whence)
00117 {
00118     return 0;
00119 }
00120 
00121 off_t Stream::tell()
00122 {
00123     return 0;
00124 }
00125 
00126 void Stream::rewind()
00127 {
00128 }
00129 
00130 int Stream::isatty()
00131 {
00132     return 0;
00133 }
00134 
00135 int Stream::sync()
00136 {
00137     return 0;
00138 }
00139 
00140 off_t Stream::size()
00141 {
00142     return 0;
00143 }
00144 
00145 int Stream::printf(const char *format, ...)
00146 {
00147     lock();
00148     std::va_list arg;
00149     va_start(arg, format);
00150     std::fseek(_file, 0, SEEK_CUR);
00151     int r = vfprintf(_file, format, arg);
00152     va_end(arg);
00153     unlock();
00154     return r;
00155 }
00156 
00157 int Stream::scanf(const char *format, ...)
00158 {
00159     lock();
00160     std::va_list arg;
00161     va_start(arg, format);
00162     fflush(_file);
00163     int r = vfscanf(_file, format, arg);
00164     va_end(arg);
00165     unlock();
00166     return r;
00167 }
00168 
00169 int Stream::vprintf(const char *format, std::va_list args)
00170 {
00171     lock();
00172     std::fseek(_file, 0, SEEK_CUR);
00173     int r = vfprintf(_file, format, args);
00174     unlock();
00175     return r;
00176 }
00177 
00178 int Stream::vscanf(const char *format, std::va_list args)
00179 {
00180     lock();
00181     fflush(_file);
00182     int r = vfscanf(_file, format, args);
00183     unlock();
00184     return r;
00185 }
00186 
00187 } // namespace mbed