2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-03-07 16:40:37 +00:00
|
|
|
#include "platform/Stream.h"
|
2017-05-02 15:18:24 +00:00
|
|
|
#include "platform/mbed_error.h"
|
|
|
|
#include <errno.h>
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
Stream::Stream(const char *name) : FileLike(name), _file(NULL)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// No lock needed in constructor
|
2013-02-18 15:32:11 +00:00
|
|
|
/* open ourselves */
|
2017-02-16 00:36:00 +00:00
|
|
|
_file = fdopen(this, "w+");
|
|
|
|
// fdopen() will make us buffered because Stream::isatty()
|
|
|
|
// wrongly returns zero which is not being changed for
|
2018-06-27 14:09:15 +00:00
|
|
|
// backward compatibility
|
2017-05-02 15:18:24 +00:00
|
|
|
if (_file) {
|
|
|
|
mbed_set_unbuffered_stream(_file);
|
|
|
|
} else {
|
2018-05-23 17:19:09 +00:00
|
|
|
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
|
2017-05-02 15:18:24 +00:00
|
|
|
}
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
Stream::~Stream()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
// No lock can be used in destructor
|
2013-02-18 15:32:11 +00:00
|
|
|
fclose(_file);
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::putc(int c)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2018-09-28 21:57:35 +00:00
|
|
|
std::fseek(_file, 0, SEEK_CUR);
|
2016-05-31 22:57:41 +00:00
|
|
|
int ret = std::fputc(c, _file);
|
|
|
|
unlock();
|
|
|
|
return ret;
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::puts(const char *s)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2018-09-28 21:57:35 +00:00
|
|
|
std::fseek(_file, 0, SEEK_CUR);
|
2016-05-31 22:57:41 +00:00
|
|
|
int ret = std::fputs(s, _file);
|
|
|
|
unlock();
|
|
|
|
return ret;
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::getc()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2013-02-18 15:32:11 +00:00
|
|
|
fflush(_file);
|
2018-09-28 21:57:35 +00:00
|
|
|
int ret = std::fgetc(_file);
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
|
|
|
return ret;
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
2018-06-27 14:09:15 +00:00
|
|
|
char *Stream::gets(char *s, int size)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2013-02-18 15:32:11 +00:00
|
|
|
fflush(_file);
|
2018-09-28 21:57:35 +00:00
|
|
|
char *ret = std::fgets(s, size, _file);
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
|
|
|
return ret;
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::close()
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
ssize_t Stream::write(const void *buffer, size_t length)
|
|
|
|
{
|
|
|
|
const char *ptr = (const char *)buffer;
|
|
|
|
const char *end = ptr + length;
|
2016-05-31 22:57:41 +00:00
|
|
|
|
|
|
|
lock();
|
2013-02-18 15:32:11 +00:00
|
|
|
while (ptr != end) {
|
|
|
|
if (_putc(*ptr++) == EOF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
return ptr - (const char *)buffer;
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
ssize_t Stream::read(void *buffer, size_t length)
|
|
|
|
{
|
|
|
|
char *ptr = (char *)buffer;
|
|
|
|
char *end = ptr + length;
|
2016-05-31 22:57:41 +00:00
|
|
|
|
|
|
|
lock();
|
2013-02-18 15:32:11 +00:00
|
|
|
while (ptr != end) {
|
|
|
|
int c = _getc();
|
2018-06-27 14:09:15 +00:00
|
|
|
if (c == EOF) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-18 15:32:11 +00:00
|
|
|
*ptr++ = c;
|
|
|
|
}
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
return ptr - (const char *)buffer;
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
off_t Stream::seek(off_t offset, int whence)
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
off_t Stream::tell()
|
|
|
|
{
|
2017-02-13 19:19:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
void Stream::rewind()
|
|
|
|
{
|
2017-02-13 19:19:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::isatty()
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::sync()
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
off_t Stream::size()
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::printf(const char *format, ...)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2015-11-23 16:08:51 +00:00
|
|
|
std::va_list arg;
|
2013-02-18 15:32:11 +00:00
|
|
|
va_start(arg, format);
|
|
|
|
fflush(_file);
|
|
|
|
int r = vfprintf(_file, format, arg);
|
|
|
|
va_end(arg);
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
2013-02-18 15:32:11 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::scanf(const char *format, ...)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2015-11-23 16:08:51 +00:00
|
|
|
std::va_list arg;
|
2013-02-18 15:32:11 +00:00
|
|
|
va_start(arg, format);
|
|
|
|
fflush(_file);
|
|
|
|
int r = vfscanf(_file, format, arg);
|
|
|
|
va_end(arg);
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
2013-02-18 15:32:11 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::vprintf(const char *format, std::va_list args)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2015-11-17 14:35:45 +00:00
|
|
|
fflush(_file);
|
|
|
|
int r = vfprintf(_file, format, args);
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
2015-11-17 14:35:45 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:09:15 +00:00
|
|
|
int Stream::vscanf(const char *format, std::va_list args)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
lock();
|
2015-11-17 14:35:45 +00:00
|
|
|
fflush(_file);
|
|
|
|
int r = vfscanf(_file, format, args);
|
2016-05-31 22:57:41 +00:00
|
|
|
unlock();
|
2015-11-17 14:35:45 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
} // namespace mbed
|