You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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()) {
  233. setstate(badbit);
  234. }
  235. return *this;
  236. }
  237. /**
  238. * \return the stream position
  239. */
  240. pos_type tellp() {
  241. return tellpos();
  242. }
  243. /**
  244. * Set the stream position
  245. * \param[in] pos The absolute position in which to move the write pointer.
  246. * \return Is always *this. Failure is indicated by the state of *this.
  247. */
  248. ostream& seekp(pos_type pos) {
  249. if (!seekpos(pos)) {
  250. setstate(failbit);
  251. }
  252. return *this;
  253. }
  254. /**
  255. * Set the stream position.
  256. *
  257. * \param[in] off An offset to move the write pointer relative to way.
  258. * \a off is a signed 32-bit int so the offset is limited to +- 2GB.
  259. * \param[in] way One of ios::beg, ios::cur, or ios::end.
  260. * \return Is always *this. Failure is indicated by the state of *this.
  261. */
  262. ostream& seekp(off_type off, seekdir way) {
  263. if (!seekoff(off, way)) {
  264. setstate(failbit);
  265. }
  266. return *this;
  267. }
  268. protected:
  269. /// @cond SHOW_PROTECTED
  270. /** Put character with binary/text conversion
  271. * \param[in] ch character to write
  272. */
  273. virtual void putch(char ch) = 0;
  274. virtual void putstr(const char *str) = 0;
  275. virtual bool seekoff(off_type pos, seekdir way) = 0;
  276. virtual bool seekpos(pos_type pos) = 0;
  277. virtual bool sync() = 0;
  278. virtual pos_type tellpos() = 0;
  279. /// @endcond
  280. private:
  281. void do_fill(unsigned len);
  282. void fill_not_left(unsigned len);
  283. char* fmtNum(uint32_t n, char *ptr, uint8_t base);
  284. void putBool(bool b);
  285. void putChar(char c);
  286. void putDouble(double n);
  287. void putNum(uint32_t n, bool neg = false);
  288. void putNum(int32_t n);
  289. void putPgm(const char* str);
  290. void putStr(const char* str);
  291. };
  292. #endif // ostream_h