Stream.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef MBED_STREAM_H
  17. #define MBED_STREAM_H
  18. #include "platform/platform.h"
  19. #include "platform/FileLike.h"
  20. #include "platform/FileHandle.h"
  21. #include "platform/NonCopyable.h"
  22. #include <cstdio>
  23. #include <cstdarg>
  24. namespace mbed {
  25. /** \addtogroup platform */
  26. /** @{*/
  27. /**
  28. * \defgroup platform_Stream Stream class
  29. * @{
  30. */
  31. extern void mbed_set_unbuffered_stream(std::FILE *_file);
  32. extern int mbed_getc(std::FILE *_file);
  33. extern char *mbed_gets(char *s, int size, std::FILE *_file);
  34. /** File stream
  35. *
  36. * @note Synchronization level: Set by subclass
  37. */
  38. class Stream : public FileLike, private NonCopyable<Stream> {
  39. public:
  40. Stream(const char *name = NULL);
  41. virtual ~Stream();
  42. int putc(int c);
  43. int puts(const char *s);
  44. int getc();
  45. char *gets(char *s, int size);
  46. int printf(const char *format, ...);
  47. int scanf(const char *format, ...);
  48. int vprintf(const char *format, std::va_list args);
  49. int vscanf(const char *format, std::va_list args);
  50. operator std::FILE *()
  51. {
  52. return _file;
  53. }
  54. protected:
  55. virtual int close();
  56. virtual ssize_t write(const void *buffer, size_t length);
  57. virtual ssize_t read(void *buffer, size_t length);
  58. virtual off_t seek(off_t offset, int whence);
  59. virtual off_t tell();
  60. virtual void rewind();
  61. virtual int isatty();
  62. virtual int sync();
  63. virtual off_t size();
  64. virtual int _putc(int c) = 0;
  65. virtual int _getc() = 0;
  66. std::FILE *_file;
  67. /** Acquire exclusive access to this object.
  68. */
  69. virtual void lock()
  70. {
  71. // Stub
  72. }
  73. /** Release exclusive access to this object.
  74. */
  75. virtual void unlock()
  76. {
  77. // Stub
  78. }
  79. };
  80. /**@}*/
  81. /**@}*/
  82. } // namespace mbed
  83. #endif