Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 fstream_h
  21. #define fstream_h
  22. /**
  23. * \file
  24. * \brief \ref fstream, \ref ifstream, and \ref ofstream classes
  25. */
  26. #include "FatFile.h"
  27. #include "iostream.h"
  28. //==============================================================================
  29. /**
  30. * \class FatStreamBase
  31. * \brief Base class for C++ style streams
  32. */
  33. class FatStreamBase : protected FatFile, virtual public ios {
  34. protected:
  35. /// @cond SHOW_PROTECTED
  36. int16_t getch();
  37. void putch(char c);
  38. void putstr(const char *str);
  39. void open(const char* path, ios::openmode mode);
  40. /** Internal do not use
  41. * \return mode
  42. */
  43. ios::openmode getmode() {
  44. return m_mode;
  45. }
  46. /** Internal do not use
  47. * \param[in] mode
  48. */
  49. void setmode(ios::openmode mode) {
  50. m_mode = mode;
  51. }
  52. bool seekoff(off_type off, seekdir way);
  53. bool seekpos(pos_type pos);
  54. int write(const void* buf, size_t n);
  55. void write(char c);
  56. /// @endcond
  57. private:
  58. ios::openmode m_mode;
  59. };
  60. //==============================================================================
  61. /**
  62. * \class fstream
  63. * \brief file input/output stream.
  64. */
  65. class fstream : public iostream, FatStreamBase {
  66. public:
  67. using iostream::peek;
  68. fstream() {}
  69. /** Constructor with open
  70. *
  71. * \param[in] path path to open
  72. * \param[in] mode open mode
  73. */
  74. explicit fstream(const char* path, openmode mode = in | out) {
  75. open(path, mode);
  76. }
  77. #if DESTRUCTOR_CLOSES_FILE
  78. ~fstream() {}
  79. #endif // DESTRUCTOR_CLOSES_FILE
  80. /** Clear state and writeError
  81. * \param[in] state new state for stream
  82. */
  83. void clear(iostate state = goodbit) {
  84. ios::clear(state);
  85. FatFile::clearWriteError();
  86. }
  87. /** Close a file and force cached data and directory information
  88. * to be written to the storage device.
  89. */
  90. void close() {
  91. FatFile::close();
  92. }
  93. /** Open a fstream
  94. * \param[in] path file to open
  95. * \param[in] mode open mode
  96. *
  97. * Valid open modes are (at end, ios::ate, and/or ios::binary may be added):
  98. *
  99. * ios::in - Open file for reading.
  100. *
  101. * ios::out or ios::out | ios::trunc - Truncate to 0 length, if existent,
  102. * or create a file for writing only.
  103. *
  104. * ios::app or ios::out | ios::app - Append; open or create file for
  105. * writing at end-of-file.
  106. *
  107. * ios::in | ios::out - Open file for update (reading and writing).
  108. *
  109. * ios::in | ios::out | ios::trunc - Truncate to zero length, if existent,
  110. * or create file for update.
  111. *
  112. * ios::in | ios::app or ios::in | ios::out | ios::app - Append; open or
  113. * create text file for update, writing at end of file.
  114. */
  115. void open(const char* path, openmode mode = in | out) {
  116. FatStreamBase::open(path, mode);
  117. }
  118. /** \return True if stream is open else false. */
  119. bool is_open() {
  120. return FatFile::isOpen();
  121. }
  122. protected:
  123. /// @cond SHOW_PROTECTED
  124. /** Internal - do not use
  125. * \return
  126. */
  127. int16_t getch() {
  128. return FatStreamBase::getch();
  129. }
  130. /** Internal - do not use
  131. * \param[out] pos
  132. */
  133. void getpos(FatPos_t* pos) {
  134. FatFile::getpos(pos);
  135. }
  136. /** Internal - do not use
  137. * \param[in] c
  138. */
  139. void putch(char c) {
  140. FatStreamBase::putch(c);
  141. }
  142. /** Internal - do not use
  143. * \param[in] str
  144. */
  145. void putstr(const char *str) {
  146. FatStreamBase::putstr(str);
  147. }
  148. /** Internal - do not use
  149. * \param[in] pos
  150. */
  151. bool seekoff(off_type off, seekdir way) {
  152. return FatStreamBase::seekoff(off, way);
  153. }
  154. bool seekpos(pos_type pos) {
  155. return FatStreamBase::seekpos(pos);
  156. }
  157. void setpos(FatPos_t* pos) {
  158. FatFile::setpos(pos);
  159. }
  160. bool sync() {
  161. return FatStreamBase::sync();
  162. }
  163. pos_type tellpos() {
  164. return FatStreamBase::curPosition();
  165. }
  166. /// @endcond
  167. };
  168. //==============================================================================
  169. /**
  170. * \class ifstream
  171. * \brief file input stream.
  172. */
  173. class ifstream : public istream, FatStreamBase {
  174. public:
  175. using istream::peek;
  176. ifstream() {}
  177. /** Constructor with open
  178. * \param[in] path file to open
  179. * \param[in] mode open mode
  180. */
  181. explicit ifstream(const char* path, openmode mode = in) {
  182. open(path, mode);
  183. }
  184. #if DESTRUCTOR_CLOSES_FILE
  185. ~ifstream() {}
  186. #endif // DESTRUCTOR_CLOSES_FILE
  187. /** Close a file and force cached data and directory information
  188. * to be written to the storage device.
  189. */
  190. void close() {
  191. FatFile::close();
  192. }
  193. /** \return True if stream is open else false. */
  194. bool is_open() {
  195. return FatFile::isOpen();
  196. }
  197. /** Open an ifstream
  198. * \param[in] path file to open
  199. * \param[in] mode open mode
  200. *
  201. * \a mode See fstream::open() for valid modes.
  202. */
  203. void open(const char* path, openmode mode = in) {
  204. FatStreamBase::open(path, mode | in);
  205. }
  206. protected:
  207. /// @cond SHOW_PROTECTED
  208. /** Internal - do not use
  209. * \return
  210. */
  211. int16_t getch() {
  212. return FatStreamBase::getch();
  213. }
  214. /** Internal - do not use
  215. * \param[out] pos
  216. */
  217. void getpos(FatPos_t* pos) {
  218. FatFile::getpos(pos);
  219. }
  220. /** Internal - do not use
  221. * \param[in] pos
  222. */
  223. bool seekoff(off_type off, seekdir way) {
  224. return FatStreamBase::seekoff(off, way);
  225. }
  226. bool seekpos(pos_type pos) {
  227. return FatStreamBase::seekpos(pos);
  228. }
  229. void setpos(FatPos_t* pos) {
  230. FatFile::setpos(pos);
  231. }
  232. pos_type tellpos() {
  233. return FatStreamBase::curPosition();
  234. }
  235. /// @endcond
  236. };
  237. //==============================================================================
  238. /**
  239. * \class ofstream
  240. * \brief file output stream.
  241. */
  242. class ofstream : public ostream, FatStreamBase {
  243. public:
  244. ofstream() {}
  245. /** Constructor with open
  246. * \param[in] path file to open
  247. * \param[in] mode open mode
  248. */
  249. explicit ofstream(const char* path, ios::openmode mode = out) {
  250. open(path, mode);
  251. }
  252. #if DESTRUCTOR_CLOSES_FILE
  253. ~ofstream() {}
  254. #endif // DESTRUCTOR_CLOSES_FILE
  255. /** Clear state and writeError
  256. * \param[in] state new state for stream
  257. */
  258. void clear(iostate state = goodbit) {
  259. ios::clear(state);
  260. FatFile::clearWriteError();
  261. }
  262. /** Close a file and force cached data and directory information
  263. * to be written to the storage device.
  264. */
  265. void close() {
  266. FatFile::close();
  267. }
  268. /** Open an ofstream
  269. * \param[in] path file to open
  270. * \param[in] mode open mode
  271. *
  272. * \a mode See fstream::open() for valid modes.
  273. */
  274. void open(const char* path, openmode mode = out) {
  275. FatStreamBase::open(path, mode | out);
  276. }
  277. /** \return True if stream is open else false. */
  278. bool is_open() {
  279. return FatFile::isOpen();
  280. }
  281. protected:
  282. /// @cond SHOW_PROTECTED
  283. /**
  284. * Internal do not use
  285. * \param[in] c
  286. */
  287. void putch(char c) {
  288. FatStreamBase::putch(c);
  289. }
  290. void putstr(const char* str) {
  291. FatStreamBase::putstr(str);
  292. }
  293. bool seekoff(off_type off, seekdir way) {
  294. return FatStreamBase::seekoff(off, way);
  295. }
  296. bool seekpos(pos_type pos) {
  297. return FatStreamBase::seekpos(pos);
  298. }
  299. /**
  300. * Internal do not use
  301. * \param[in] b
  302. */
  303. bool sync() {
  304. return FatStreamBase::sync();
  305. }
  306. pos_type tellpos() {
  307. return FatStreamBase::curPosition();
  308. }
  309. /// @endcond
  310. };
  311. //------------------------------------------------------------------------------
  312. #endif // fstream_h