diff --git a/libraries/mbed/api/Stream.h b/libraries/mbed/api/Stream.h index 637fb45961..a57053e67c 100644 --- a/libraries/mbed/api/Stream.h +++ b/libraries/mbed/api/Stream.h @@ -21,6 +21,10 @@ namespace mbed { +extern void mbed_set_unbuffered_stream(FILE *_file); +extern int mbed_getc(FILE *_file); +extern char* mbed_gets(char *s, int size, FILE *_file); + class Stream : public FileLike { public: diff --git a/libraries/mbed/common/Stream.cpp b/libraries/mbed/common/Stream.cpp index 92f3e8dd04..6d3a33526f 100644 --- a/libraries/mbed/common/Stream.cpp +++ b/libraries/mbed/common/Stream.cpp @@ -24,11 +24,7 @@ Stream::Stream(const char *name) : FileLike(name), _file(NULL) { char buf[12]; /* :0x12345678 + null byte */ std::sprintf(buf, ":%p", this); _file = std::fopen(buf, "w+"); -#if defined (__ICCARM__) - std::setvbuf(_file,buf,_IONBF,NULL); -#else - setbuf(_file, NULL); -#endif + mbed_set_unbuffered_stream(_file); } Stream::~Stream() { @@ -45,31 +41,11 @@ int Stream::puts(const char *s) { } int Stream::getc() { fflush(_file); -#if defined (__ICCARM__) - int res = std::fgetc(_file); - if (res>=0){ - _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */ - _file->_Rend = _file->_Wend; - _file->_Next = _file->_Wend; - } - return res; -#else - return std::fgetc(_file); -#endif + return mbed_getc(_file); } char* Stream::gets(char *s, int size) { fflush(_file); -#if defined (__ICCARM__) - char *str = fgets(s,size,_file); - if (str!=NULL){ - _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */ - _file->_Rend = _file->_Wend; - _file->_Next = _file->_Wend; - } - return str; -#else - return std::fgets(s,size,_file); -#endif + return mbed_gets(s,size,_file); } int Stream::close() { diff --git a/libraries/mbed/common/retarget.cpp b/libraries/mbed/common/retarget.cpp index 96f78d93a4..a2ba7c86ce 100644 --- a/libraries/mbed/common/retarget.cpp +++ b/libraries/mbed/common/retarget.cpp @@ -480,3 +480,55 @@ extern "C" caddr_t _sbrk(int incr) { return (caddr_t) prev_heap; } #endif + + +namespace mbed { + +void mbed_set_unbuffered_stream(FILE *_file) { +#if defined (__ICCARM__) + char buf[2]; + std::setvbuf(_file,buf,_IONBF,NULL); +#else + setbuf(_file, NULL); +#endif +} + +int mbed_getc(FILE *_file){ +#if defined (__ICCARM__) + /*This is only valid for unbuffered streams*/ + int res = std::fgetc(_file); + if (res>=0){ + _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */ + _file->_Rend = _file->_Wend; + _file->_Next = _file->_Wend; + } + return res; +#else + return std::fgetc(_file); +#endif +} + +char* mbed_gets(char*s, int size, FILE *_file){ +#if defined (__ICCARM__) + /*This is only valid for unbuffered streams*/ + char *str = fgets(s,size,_file); + if (str!=NULL){ + _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */ + _file->_Rend = _file->_Wend; + _file->_Next = _file->_Wend; + } + return str; +#else + return std::fgets(s,size,_file); +#endif +} + +} // namespace mbed + + + + + + + +