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 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 ios_h
  21. #define ios_h
  22. #include "FatFile.h"
  23. /**
  24. * \file
  25. * \brief \ref ios_base and \ref ios classes
  26. */
  27. //==============================================================================
  28. /**
  29. * \class ios_base
  30. * \brief Base class for all streams
  31. */
  32. class ios_base {
  33. public:
  34. /** typedef for iostate bitmask */
  35. typedef unsigned char iostate;
  36. // State flags.
  37. /** iostate for no flags */
  38. static const iostate goodbit = 0x00;
  39. /** iostate bad bit for a nonrecoverable error. */
  40. static const iostate badbit = 0X01;
  41. /** iostate bit for end of file reached */
  42. static const iostate eofbit = 0x02;
  43. /** iostate fail bit for nonfatal error */
  44. static const iostate failbit = 0X04;
  45. /**
  46. * unsigned size that can represent maximum file size.
  47. * (violates spec - should be signed)
  48. */
  49. typedef uint32_t streamsize;
  50. /** type for absolute seek position */
  51. typedef uint32_t pos_type;
  52. /** type for relative seek offset */
  53. typedef int32_t off_type;
  54. /** enumerated type for the direction of relative seeks */
  55. enum seekdir {
  56. /** seek relative to the beginning of the stream */
  57. beg,
  58. /** seek relative to the current stream position */
  59. cur,
  60. /** seek relative to the end of the stream */
  61. end
  62. };
  63. /** type for format flags */
  64. typedef unsigned int fmtflags;
  65. /** left adjust fields */
  66. static const fmtflags left = 0x0001;
  67. /** right adjust fields */
  68. static const fmtflags right = 0x0002;
  69. /** fill between sign/base prefix and number */
  70. static const fmtflags internal = 0x0004;
  71. /** base 10 flag*/
  72. static const fmtflags dec = 0x0008;
  73. /** base 16 flag */
  74. static const fmtflags hex = 0x0010;
  75. /** base 8 flag */
  76. static const fmtflags oct = 0x0020;
  77. // static const fmtflags fixed = 0x0040;
  78. // static const fmtflags scientific = 0x0080;
  79. /** use strings true/false for bool */
  80. static const fmtflags boolalpha = 0x0100;
  81. /** use prefix 0X for hex and 0 for oct */
  82. static const fmtflags showbase = 0x0200;
  83. /** always show '.' for floating numbers */
  84. static const fmtflags showpoint = 0x0400;
  85. /** show + sign for nonnegative numbers */
  86. static const fmtflags showpos = 0x0800;
  87. /** skip initial white space */
  88. static const fmtflags skipws = 0x1000;
  89. // static const fmtflags unitbuf = 0x2000;
  90. /** use uppercase letters in number representations */
  91. static const fmtflags uppercase = 0x4000;
  92. /** mask for adjustfield */
  93. static const fmtflags adjustfield = left | right | internal;
  94. /** mask for basefield */
  95. static const fmtflags basefield = dec | hex | oct;
  96. // static const fmtflags floatfield = scientific | fixed;
  97. //----------------------------------------------------------------------------
  98. /** typedef for iostream open mode */
  99. typedef uint8_t openmode;
  100. // Openmode flags.
  101. /** seek to end before each write */
  102. static const openmode app = 0X4;
  103. /** open and seek to end immediately after opening */
  104. static const openmode ate = 0X8;
  105. /** perform input and output in binary mode (as opposed to text mode) */
  106. static const openmode binary = 0X10;
  107. /** open for input */
  108. static const openmode in = 0X20;
  109. /** open for output */
  110. static const openmode out = 0X40;
  111. /** truncate an existing stream when opening */
  112. static const openmode trunc = 0X80;
  113. //----------------------------------------------------------------------------
  114. ios_base() : m_fill(' '), m_fmtflags(dec | right | skipws)
  115. , m_precision(2), m_width(0) {}
  116. /** \return fill character */
  117. char fill() {
  118. return m_fill;
  119. }
  120. /** Set fill character
  121. * \param[in] c new fill character
  122. * \return old fill character
  123. */
  124. char fill(char c) {
  125. char r = m_fill;
  126. m_fill = c;
  127. return r;
  128. }
  129. /** \return format flags */
  130. fmtflags flags() const {
  131. return m_fmtflags;
  132. }
  133. /** set format flags
  134. * \param[in] fl new flag
  135. * \return old flags
  136. */
  137. fmtflags flags(fmtflags fl) {
  138. fmtflags tmp = m_fmtflags;
  139. m_fmtflags = fl;
  140. return tmp;
  141. }
  142. /** \return precision */
  143. int precision() const {
  144. return m_precision;
  145. }
  146. /** set precision
  147. * \param[in] n new precision
  148. * \return old precision
  149. */
  150. int precision(unsigned int n) {
  151. int r = m_precision;
  152. m_precision = n;
  153. return r;
  154. }
  155. /** set format flags
  156. * \param[in] fl new flags to be or'ed in
  157. * \return old flags
  158. */
  159. fmtflags setf(fmtflags fl) {
  160. fmtflags r = m_fmtflags;
  161. m_fmtflags |= fl;
  162. return r;
  163. }
  164. /** modify format flags
  165. * \param[in] mask flags to be removed
  166. * \param[in] fl flags to be set after mask bits have been cleared
  167. * \return old flags
  168. */
  169. fmtflags setf(fmtflags fl, fmtflags mask) {
  170. fmtflags r = m_fmtflags;
  171. m_fmtflags &= ~mask;
  172. m_fmtflags |= fl;
  173. return r;
  174. }
  175. /** clear format flags
  176. * \param[in] fl flags to be cleared
  177. * \return old flags
  178. */
  179. void unsetf(fmtflags fl) {
  180. m_fmtflags &= ~fl;
  181. }
  182. /** \return width */
  183. unsigned width() {
  184. return m_width;
  185. }
  186. /** set width
  187. * \param[in] n new width
  188. * \return old width
  189. */
  190. unsigned width(unsigned n) {
  191. unsigned r = m_width;
  192. m_width = n;
  193. return r;
  194. }
  195. protected:
  196. /** \return current number base */
  197. uint8_t flagsToBase() {
  198. uint8_t f = flags() & basefield;
  199. return f == oct ? 8 : f != hex ? 10 : 16;
  200. }
  201. private:
  202. char m_fill;
  203. fmtflags m_fmtflags;
  204. unsigned char m_precision;
  205. unsigned int m_width;
  206. };
  207. //------------------------------------------------------------------------------
  208. /** function for boolalpha manipulator
  209. * \param[in] str The stream
  210. * \return The stream
  211. */
  212. inline ios_base& boolalpha(ios_base& str) {
  213. str.setf(ios_base::boolalpha);
  214. return str;
  215. }
  216. /** function for dec manipulator
  217. * \param[in] str The stream
  218. * \return The stream
  219. */
  220. inline ios_base& dec(ios_base& str) {
  221. str.setf(ios_base::dec, ios_base::basefield);
  222. return str;
  223. }
  224. /** function for hex manipulator
  225. * \param[in] str The stream
  226. * \return The stream
  227. */
  228. inline ios_base& hex(ios_base& str) {
  229. str.setf(ios_base::hex, ios_base::basefield);
  230. return str;
  231. }
  232. /** function for internal manipulator
  233. * \param[in] str The stream
  234. * \return The stream
  235. */
  236. inline ios_base& internal(ios_base& str) {
  237. str.setf(ios_base::internal, ios_base::adjustfield);
  238. return str;
  239. }
  240. /** function for left manipulator
  241. * \param[in] str The stream
  242. * \return The stream
  243. */
  244. inline ios_base& left(ios_base& str) {
  245. str.setf(ios_base::left, ios_base::adjustfield);
  246. return str;
  247. }
  248. /** function for noboolalpha manipulator
  249. * \param[in] str The stream
  250. * \return The stream
  251. */
  252. inline ios_base& noboolalpha(ios_base& str) {
  253. str.unsetf(ios_base::boolalpha);
  254. return str;
  255. }
  256. /** function for noshowbase manipulator
  257. * \param[in] str The stream
  258. * \return The stream
  259. */
  260. inline ios_base& noshowbase(ios_base& str) {
  261. str.unsetf(ios_base::showbase);
  262. return str;
  263. }
  264. /** function for noshowpoint manipulator
  265. * \param[in] str The stream
  266. * \return The stream
  267. */
  268. inline ios_base& noshowpoint(ios_base& str) {
  269. str.unsetf(ios_base::showpoint);
  270. return str;
  271. }
  272. /** function for noshowpos manipulator
  273. * \param[in] str The stream
  274. * \return The stream
  275. */
  276. inline ios_base& noshowpos(ios_base& str) {
  277. str.unsetf(ios_base::showpos);
  278. return str;
  279. }
  280. /** function for noskipws manipulator
  281. * \param[in] str The stream
  282. * \return The stream
  283. */
  284. inline ios_base& noskipws(ios_base& str) {
  285. str.unsetf(ios_base::skipws);
  286. return str;
  287. }
  288. /** function for nouppercase manipulator
  289. * \param[in] str The stream
  290. * \return The stream
  291. */
  292. inline ios_base& nouppercase(ios_base& str) {
  293. str.unsetf(ios_base::uppercase);
  294. return str;
  295. }
  296. /** function for oct manipulator
  297. * \param[in] str The stream
  298. * \return The stream
  299. */
  300. inline ios_base& oct(ios_base& str) {
  301. str.setf(ios_base::oct, ios_base::basefield);
  302. return str;
  303. }
  304. /** function for right manipulator
  305. * \param[in] str The stream
  306. * \return The stream
  307. */
  308. inline ios_base& right(ios_base& str) {
  309. str.setf(ios_base::right, ios_base::adjustfield);
  310. return str;
  311. }
  312. /** function for showbase manipulator
  313. * \param[in] str The stream
  314. * \return The stream
  315. */
  316. inline ios_base& showbase(ios_base& str) {
  317. str.setf(ios_base::showbase);
  318. return str;
  319. }
  320. /** function for showpos manipulator
  321. * \param[in] str The stream
  322. * \return The stream
  323. */
  324. inline ios_base& showpos(ios_base& str) {
  325. str.setf(ios_base::showpos);
  326. return str;
  327. }
  328. /** function for showpoint manipulator
  329. * \param[in] str The stream
  330. * \return The stream
  331. */
  332. inline ios_base& showpoint(ios_base& str) {
  333. str.setf(ios_base::showpoint);
  334. return str;
  335. }
  336. /** function for skipws manipulator
  337. * \param[in] str The stream
  338. * \return The stream
  339. */
  340. inline ios_base& skipws(ios_base& str) {
  341. str.setf(ios_base::skipws);
  342. return str;
  343. }
  344. /** function for uppercase manipulator
  345. * \param[in] str The stream
  346. * \return The stream
  347. */
  348. inline ios_base& uppercase(ios_base& str) {
  349. str.setf(ios_base::uppercase);
  350. return str;
  351. }
  352. //==============================================================================
  353. /**
  354. * \class ios
  355. * \brief Error and state information for all streams
  356. */
  357. class ios : public ios_base {
  358. public:
  359. /** Create ios with no error flags set */
  360. ios() : m_iostate(0) {}
  361. /** \return null pointer if fail() is true. */
  362. operator const void*() const {
  363. return !fail() ? reinterpret_cast<const void*>(this) : 0;
  364. }
  365. /** \return true if fail() else false. */
  366. bool operator!() const {
  367. return fail();
  368. }
  369. /** \return The iostate flags for this file. */
  370. iostate rdstate() const {
  371. return m_iostate;
  372. }
  373. /** \return True if no iostate flags are set else false. */
  374. bool good() const {
  375. return m_iostate == goodbit;
  376. }
  377. /** \return true if end of file has been reached else false.
  378. *
  379. * Warning: An empty file returns false before the first read.
  380. *
  381. * Moral: eof() is only useful in combination with fail(), to find out
  382. * whether EOF was the cause for failure
  383. */
  384. bool eof() const {
  385. return m_iostate & eofbit;
  386. }
  387. /** \return true if any iostate bit other than eof are set else false. */
  388. bool fail() const {
  389. return m_iostate & (failbit | badbit);
  390. }
  391. /** \return true if bad bit is set else false. */
  392. bool bad() const {
  393. return m_iostate & badbit;
  394. }
  395. /** Clear iostate bits.
  396. *
  397. * \param[in] state The flags you want to set after clearing all flags.
  398. **/
  399. void clear(iostate state = goodbit) {
  400. m_iostate = state;
  401. }
  402. /** Set iostate bits.
  403. *
  404. * \param[in] state Bitts to set.
  405. **/
  406. void setstate(iostate state) {
  407. m_iostate |= state;
  408. }
  409. private:
  410. iostate m_iostate;
  411. };
  412. #endif // ios_h