Stream.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "platform/Stream.h"
  17. #include "platform/mbed_error.h"
  18. #include <errno.h>
  19. namespace mbed {
  20. Stream::Stream(const char *name) : FileLike(name), _file(NULL)
  21. {
  22. // No lock needed in constructor
  23. /* open ourselves */
  24. _file = fdopen(this, "w+");
  25. // fdopen() will make us buffered because Stream::isatty()
  26. // wrongly returns zero which is not being changed for
  27. // backward compatibility
  28. if (_file) {
  29. mbed_set_unbuffered_stream(_file);
  30. } else {
  31. MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
  32. }
  33. }
  34. Stream::~Stream()
  35. {
  36. // No lock can be used in destructor
  37. fclose(_file);
  38. }
  39. int Stream::putc(int c)
  40. {
  41. lock();
  42. fflush(_file);
  43. int ret = std::fputc(c, _file);
  44. unlock();
  45. return ret;
  46. }
  47. int Stream::puts(const char *s)
  48. {
  49. lock();
  50. fflush(_file);
  51. int ret = std::fputs(s, _file);
  52. unlock();
  53. return ret;
  54. }
  55. int Stream::getc()
  56. {
  57. lock();
  58. fflush(_file);
  59. int ret = mbed_getc(_file);
  60. unlock();
  61. return ret;
  62. }
  63. char *Stream::gets(char *s, int size)
  64. {
  65. lock();
  66. fflush(_file);
  67. char *ret = mbed_gets(s, size, _file);
  68. unlock();
  69. return ret;
  70. }
  71. int Stream::close()
  72. {
  73. return 0;
  74. }
  75. ssize_t Stream::write(const void *buffer, size_t length)
  76. {
  77. const char *ptr = (const char *)buffer;
  78. const char *end = ptr + length;
  79. lock();
  80. while (ptr != end) {
  81. if (_putc(*ptr++) == EOF) {
  82. break;
  83. }
  84. }
  85. unlock();
  86. return ptr - (const char *)buffer;
  87. }
  88. ssize_t Stream::read(void *buffer, size_t length)
  89. {
  90. char *ptr = (char *)buffer;
  91. char *end = ptr + length;
  92. lock();
  93. while (ptr != end) {
  94. int c = _getc();
  95. if (c == EOF) {
  96. break;
  97. }
  98. *ptr++ = c;
  99. }
  100. unlock();
  101. return ptr - (const char *)buffer;
  102. }
  103. off_t Stream::seek(off_t offset, int whence)
  104. {
  105. return 0;
  106. }
  107. off_t Stream::tell()
  108. {
  109. return 0;
  110. }
  111. void Stream::rewind()
  112. {
  113. }
  114. int Stream::isatty()
  115. {
  116. return 0;
  117. }
  118. int Stream::sync()
  119. {
  120. return 0;
  121. }
  122. off_t Stream::size()
  123. {
  124. return 0;
  125. }
  126. int Stream::printf(const char *format, ...)
  127. {
  128. lock();
  129. std::va_list arg;
  130. va_start(arg, format);
  131. fflush(_file);
  132. int r = vfprintf(_file, format, arg);
  133. va_end(arg);
  134. unlock();
  135. return r;
  136. }
  137. int Stream::scanf(const char *format, ...)
  138. {
  139. lock();
  140. std::va_list arg;
  141. va_start(arg, format);
  142. fflush(_file);
  143. int r = vfscanf(_file, format, arg);
  144. va_end(arg);
  145. unlock();
  146. return r;
  147. }
  148. int Stream::vprintf(const char *format, std::va_list args)
  149. {
  150. lock();
  151. fflush(_file);
  152. int r = vfprintf(_file, format, args);
  153. unlock();
  154. return r;
  155. }
  156. int Stream::vscanf(const char *format, std::va_list args)
  157. {
  158. lock();
  159. fflush(_file);
  160. int r = vfscanf(_file, format, args);
  161. unlock();
  162. return r;
  163. }
  164. } // namespace mbed