Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

288 lines
7.3KB

  1. /* FatLib Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the FatLib Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the FatLib Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef ostream_h
  21. #define ostream_h
  22. /**
  23. * \file
  24. * \brief \ref ostream class
  25. */
  26. #include "ios.h"
  27. //------------------------------------------------------------------------------
  28. /** macro for flash inserter */
  29. #define pstr(str) pgm(PSTR(str))
  30. /** \struct pgm
  31. * \brief type for string in flash
  32. */
  33. struct pgm {
  34. /** Pointer to flash string */
  35. char *ptr;
  36. /** constructor
  37. * \param[in] str initializer for pointer.
  38. */
  39. explicit pgm(char* str) : ptr(str) {}
  40. /** constructor
  41. * \param[in] str initializer for pointer.
  42. */
  43. explicit pgm(const char *str) : ptr(const_cast<char*>(str)) {}
  44. };
  45. //==============================================================================
  46. /**
  47. * \class ostream
  48. * \brief Output Stream
  49. */
  50. class ostream : public virtual ios {
  51. public:
  52. ostream() {}
  53. /** call manipulator
  54. * \param[in] pf function to call
  55. * \return the stream
  56. */
  57. ostream& operator<< (ostream& (*pf)(ostream& str)) {
  58. return pf(*this);
  59. }
  60. /** call manipulator
  61. * \param[in] pf function to call
  62. * \return the stream
  63. */
  64. ostream& operator<< (ios_base& (*pf)(ios_base& str)) {
  65. pf(*this);
  66. return *this;
  67. }
  68. /** Output bool
  69. * \param[in] arg value to output
  70. * \return the stream
  71. */
  72. ostream &operator<< (bool arg) {
  73. putBool(arg);
  74. return *this;
  75. }
  76. /** Output string
  77. * \param[in] arg string to output
  78. * \return the stream
  79. */
  80. ostream &operator<< (const char *arg) {
  81. putStr(arg);
  82. return *this;
  83. }
  84. /** Output string
  85. * \param[in] arg string to output
  86. * \return the stream
  87. */
  88. ostream &operator<< (const signed char *arg) {
  89. putStr((const char*)arg);
  90. return *this;
  91. }
  92. /** Output string
  93. * \param[in] arg string to output
  94. * \return the stream
  95. */
  96. ostream &operator<< (const unsigned char *arg) {
  97. putStr((const char*)arg);
  98. return *this;
  99. }
  100. /** Output character
  101. * \param[in] arg character to output
  102. * \return the stream
  103. */
  104. ostream &operator<< (char arg) {
  105. putChar(arg);
  106. return *this;
  107. }
  108. /** Output character
  109. * \param[in] arg character to output
  110. * \return the stream
  111. */
  112. ostream &operator<< (signed char arg) {
  113. putChar(static_cast<char>(arg));
  114. return *this;
  115. }
  116. /** Output character
  117. * \param[in] arg character to output
  118. * \return the stream
  119. */
  120. ostream &operator<< (unsigned char arg) {
  121. putChar(static_cast<char>(arg));
  122. return *this;
  123. }
  124. /** Output double
  125. * \param[in] arg value to output
  126. * \return the stream
  127. */
  128. ostream &operator<< (double arg) {
  129. putDouble(arg);
  130. return *this;
  131. }
  132. /** Output float
  133. * \param[in] arg value to output
  134. * \return the stream
  135. */
  136. ostream &operator<< (float arg) {
  137. putDouble(arg);
  138. return *this;
  139. }
  140. /** Output signed short
  141. * \param[in] arg value to output
  142. * \return the stream
  143. */
  144. ostream &operator<< (short arg) { // NOLINT
  145. putNum((int32_t)arg);
  146. return *this;
  147. }
  148. /** Output unsigned short
  149. * \param[in] arg value to output
  150. * \return the stream
  151. */
  152. ostream &operator<< (unsigned short arg) { // NOLINT
  153. putNum((uint32_t)arg);
  154. return *this;
  155. }
  156. /** Output signed int
  157. * \param[in] arg value to output
  158. * \return the stream
  159. */
  160. ostream &operator<< (int arg) {
  161. putNum((int32_t)arg);
  162. return *this;
  163. }
  164. /** Output unsigned int
  165. * \param[in] arg value to output
  166. * \return the stream
  167. */
  168. ostream &operator<< (unsigned int arg) {
  169. putNum((uint32_t)arg);
  170. return *this;
  171. }
  172. /** Output signed long
  173. * \param[in] arg value to output
  174. * \return the stream
  175. */
  176. ostream &operator<< (long arg) { // NOLINT
  177. putNum((int32_t)arg);
  178. return *this;
  179. }
  180. /** Output unsigned long
  181. * \param[in] arg value to output
  182. * \return the stream
  183. */
  184. ostream &operator<< (unsigned long arg) { // NOLINT
  185. putNum((uint32_t)arg);
  186. return *this;
  187. }
  188. /** Output pointer
  189. * \param[in] arg value to output
  190. * \return the stream
  191. */
  192. ostream& operator<< (const void* arg) {
  193. putNum(reinterpret_cast<uint32_t>(arg));
  194. return *this;
  195. }
  196. /** Output a string from flash using the pstr() macro
  197. * \param[in] arg pgm struct pointing to string
  198. * \return the stream
  199. */
  200. ostream &operator<< (pgm arg) {
  201. putPgm(arg.ptr);
  202. return *this;
  203. }
  204. /** Output a string from flash using the Arduino F() macro.
  205. * \param[in] arg pointing to flash string
  206. * \return the stream
  207. */
  208. ostream &operator<< (const __FlashStringHelper *arg) {
  209. putPgm(reinterpret_cast<const char*>(arg));
  210. return *this;
  211. }
  212. /**
  213. * Puts a character in a stream.
  214. *
  215. * The unformatted output function inserts the element \a ch.
  216. * It returns *this.
  217. *
  218. * \param[in] ch The character
  219. * \return A reference to the ostream object.
  220. */
  221. ostream& put(char ch) {
  222. putch(ch);
  223. return *this;
  224. }
  225. // ostream& write(char *str, streamsize count);
  226. /**
  227. * Flushes the buffer associated with this stream. The flush function
  228. * calls the sync function of the associated file.
  229. * \return A reference to the ostream object.
  230. */
  231. ostream& flush() {
  232. if (!sync()) setstate(badbit);
  233. return *this;
  234. }
  235. /**
  236. * \return the stream position
  237. */
  238. pos_type tellp() {return tellpos();}
  239. /**
  240. * Set the stream position
  241. * \param[in] pos The absolute position in which to move the write pointer.
  242. * \return Is always *this. Failure is indicated by the state of *this.
  243. */
  244. ostream& seekp(pos_type pos) {
  245. if (!seekpos(pos)) setstate(failbit);
  246. return *this;
  247. }
  248. /**
  249. * Set the stream position.
  250. *
  251. * \param[in] off An offset to move the write pointer relative to way.
  252. * \a off is a signed 32-bit int so the offset is limited to +- 2GB.
  253. * \param[in] way One of ios::beg, ios::cur, or ios::end.
  254. * \return Is always *this. Failure is indicated by the state of *this.
  255. */
  256. ostream& seekp(off_type off, seekdir way) {
  257. if (!seekoff(off, way)) setstate(failbit);
  258. return *this;
  259. }
  260. protected:
  261. /// @cond SHOW_PROTECTED
  262. /** Put character with binary/text conversion
  263. * \param[in] ch character to write
  264. */
  265. virtual void putch(char ch) = 0;
  266. virtual void putstr(const char *str) = 0;
  267. virtual bool seekoff(off_type pos, seekdir way) = 0;
  268. virtual bool seekpos(pos_type pos) = 0;
  269. virtual bool sync() = 0;
  270. virtual pos_type tellpos() = 0;
  271. /// @endcond
  272. private:
  273. void do_fill(unsigned len);
  274. void fill_not_left(unsigned len);
  275. char* fmtNum(uint32_t n, char *ptr, uint8_t base);
  276. void putBool(bool b);
  277. void putChar(char c);
  278. void putDouble(double n);
  279. void putNum(uint32_t n, bool neg = false);
  280. void putNum(int32_t n);
  281. void putPgm(const char* str);
  282. void putStr(const char* str);
  283. };
  284. #endif // ostream_h