ostream.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Copyright (c) 20011-2017 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef ostream_h
  26. #define ostream_h
  27. /**
  28. * \file
  29. * \brief \ref ostream class
  30. */
  31. #include "ios.h"
  32. //==============================================================================
  33. /**
  34. * \class ostream
  35. * \brief Output Stream
  36. */
  37. class ostream : public virtual ios {
  38. public:
  39. ostream() {}
  40. /** call manipulator
  41. * \param[in] pf function to call
  42. * \return the stream
  43. */
  44. ostream& operator<< (ostream& (*pf)(ostream& str)) {
  45. return pf(*this);
  46. }
  47. /** call manipulator
  48. * \param[in] pf function to call
  49. * \return the stream
  50. */
  51. ostream& operator<< (ios_base& (*pf)(ios_base& str)) {
  52. pf(*this);
  53. return *this;
  54. }
  55. /** Output bool
  56. * \param[in] arg value to output
  57. * \return the stream
  58. */
  59. ostream &operator<< (bool arg) {
  60. putBool(arg);
  61. return *this;
  62. }
  63. /** Output string
  64. * \param[in] arg string to output
  65. * \return the stream
  66. */
  67. ostream &operator<< (const char *arg) {
  68. putStr(arg);
  69. return *this;
  70. }
  71. /** Output string
  72. * \param[in] arg string to output
  73. * \return the stream
  74. */
  75. ostream &operator<< (const signed char *arg) {
  76. putStr((const char*)arg);
  77. return *this;
  78. }
  79. /** Output string
  80. * \param[in] arg string to output
  81. * \return the stream
  82. */
  83. ostream &operator<< (const unsigned char *arg) {
  84. putStr((const char*)arg);
  85. return *this;
  86. }
  87. /** Output character
  88. * \param[in] arg character to output
  89. * \return the stream
  90. */
  91. ostream &operator<< (char arg) {
  92. putChar(arg);
  93. return *this;
  94. }
  95. /** Output character
  96. * \param[in] arg character to output
  97. * \return the stream
  98. */
  99. ostream &operator<< (signed char arg) {
  100. putChar(static_cast<char>(arg));
  101. return *this;
  102. }
  103. /** Output character
  104. * \param[in] arg character to output
  105. * \return the stream
  106. */
  107. ostream &operator<< (unsigned char arg) {
  108. putChar(static_cast<char>(arg));
  109. return *this;
  110. }
  111. /** Output double
  112. * \param[in] arg value to output
  113. * \return the stream
  114. */
  115. ostream &operator<< (double arg) {
  116. putDouble(arg);
  117. return *this;
  118. }
  119. /** Output float
  120. * \param[in] arg value to output
  121. * \return the stream
  122. */
  123. ostream &operator<< (float arg) {
  124. putDouble(arg);
  125. return *this;
  126. }
  127. /** Output signed short
  128. * \param[in] arg value to output
  129. * \return the stream
  130. */
  131. ostream &operator<< (short arg) { // NOLINT
  132. putNum((int32_t)arg);
  133. return *this;
  134. }
  135. /** Output unsigned short
  136. * \param[in] arg value to output
  137. * \return the stream
  138. */
  139. ostream &operator<< (unsigned short arg) { // NOLINT
  140. putNum((uint32_t)arg);
  141. return *this;
  142. }
  143. /** Output signed int
  144. * \param[in] arg value to output
  145. * \return the stream
  146. */
  147. ostream &operator<< (int arg) {
  148. putNum((int32_t)arg);
  149. return *this;
  150. }
  151. /** Output unsigned int
  152. * \param[in] arg value to output
  153. * \return the stream
  154. */
  155. ostream &operator<< (unsigned int arg) {
  156. putNum((uint32_t)arg);
  157. return *this;
  158. }
  159. /** Output signed long
  160. * \param[in] arg value to output
  161. * \return the stream
  162. */
  163. ostream &operator<< (long arg) { // NOLINT
  164. putNum((int32_t)arg);
  165. return *this;
  166. }
  167. /** Output unsigned long
  168. * \param[in] arg value to output
  169. * \return the stream
  170. */
  171. ostream &operator<< (unsigned long arg) { // NOLINT
  172. putNum((uint32_t)arg);
  173. return *this;
  174. }
  175. /** Output pointer
  176. * \param[in] arg value to output
  177. * \return the stream
  178. */
  179. ostream& operator<< (const void* arg) {
  180. putNum(reinterpret_cast<uint32_t>(arg));
  181. return *this;
  182. }
  183. #if (defined(ARDUINO) && ENABLE_ARDUINO_FEATURES) || defined(DOXYGEN)
  184. /** Output a string from flash using the Arduino F() macro.
  185. * \param[in] arg pointing to flash string
  186. * \return the stream
  187. */
  188. ostream &operator<< (const __FlashStringHelper *arg) {
  189. putPgm(reinterpret_cast<const char*>(arg));
  190. return *this;
  191. }
  192. #endif // (defined(ARDUINO) && ENABLE_ARDUINO_FEATURES) || defined(DOXYGEN)
  193. /**
  194. * Puts a character in a stream.
  195. *
  196. * The unformatted output function inserts the element \a ch.
  197. * It returns *this.
  198. *
  199. * \param[in] ch The character
  200. * \return A reference to the ostream object.
  201. */
  202. ostream& put(char ch) {
  203. putch(ch);
  204. return *this;
  205. }
  206. // ostream& write(char *str, streamsize count);
  207. /**
  208. * Flushes the buffer associated with this stream. The flush function
  209. * calls the sync function of the associated file.
  210. * \return A reference to the ostream object.
  211. */
  212. ostream& flush() {
  213. if (!sync()) {
  214. setstate(badbit);
  215. }
  216. return *this;
  217. }
  218. /**
  219. * \return the stream position
  220. */
  221. pos_type tellp() {
  222. return tellpos();
  223. }
  224. /**
  225. * Set the stream position
  226. * \param[in] pos The absolute position in which to move the write pointer.
  227. * \return Is always *this. Failure is indicated by the state of *this.
  228. */
  229. ostream& seekp(pos_type pos) {
  230. if (!seekpos(pos)) {
  231. setstate(failbit);
  232. }
  233. return *this;
  234. }
  235. /**
  236. * Set the stream position.
  237. *
  238. * \param[in] off An offset to move the write pointer relative to way.
  239. * \a off is a signed 32-bit int so the offset is limited to +- 2GB.
  240. * \param[in] way One of ios::beg, ios::cur, or ios::end.
  241. * \return Is always *this. Failure is indicated by the state of *this.
  242. */
  243. ostream& seekp(off_type off, seekdir way) {
  244. if (!seekoff(off, way)) {
  245. setstate(failbit);
  246. }
  247. return *this;
  248. }
  249. protected:
  250. /// @cond SHOW_PROTECTED
  251. /** Put character with binary/text conversion
  252. * \param[in] ch character to write
  253. */
  254. virtual void putch(char ch) = 0;
  255. virtual void putstr(const char *str) = 0;
  256. virtual bool seekoff(off_type pos, seekdir way) = 0;
  257. virtual bool seekpos(pos_type pos) = 0;
  258. virtual bool sync() = 0;
  259. virtual pos_type tellpos() = 0;
  260. /// @endcond
  261. private:
  262. void do_fill(unsigned len);
  263. void fill_not_left(unsigned len);
  264. char* fmtNum(uint32_t n, char *ptr, uint8_t base);
  265. void putBool(bool b);
  266. void putChar(char c);
  267. void putDouble(double n);
  268. void putNum(uint32_t n, bool neg = false);
  269. void putNum(int32_t n);
  270. void putPgm(const char* str);
  271. void putStr(const char* str);
  272. };
  273. #endif // ostream_h