CFileInputStream.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * CFileInputStream.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "CInputStream.h"
  12. class CFileInfo;
  13. /**
  14. * A class which provides method definitions for reading a file from the filesystem.
  15. */
  16. class DLL_LINKAGE CFileInputStream : public CInputStream
  17. {
  18. public:
  19. /**
  20. * C-tor. Opens the specified file.
  21. *
  22. * @see CFileInputStream::open
  23. */
  24. CFileInputStream(const std::string & file, si64 start=0, si64 size=0);
  25. /**
  26. * C-tor. Opens the specified file.
  27. *
  28. * @see CFileInputStream::open
  29. */
  30. CFileInputStream(const CFileInfo & file, si64 start=0, si64 size=0);
  31. /**
  32. * D-tor. Calls the close method implicitely, if the file is still opened.
  33. */
  34. ~CFileInputStream();
  35. /**
  36. * Reads n bytes from the stream into the data buffer.
  37. *
  38. * @param data A pointer to the destination data array.
  39. * @param size The number of bytes to read.
  40. * @return the number of bytes read actually.
  41. */
  42. si64 read(ui8 * data, si64 size);
  43. /**
  44. * Seeks the internal read pointer to the specified position.
  45. *
  46. * @param position The read position from the beginning.
  47. * @return the position actually moved to, -1 on error.
  48. */
  49. si64 seek(si64 position);
  50. /**
  51. * Gets the current read position in the stream.
  52. *
  53. * @return the read position. -1 on failure or if the read pointer isn't in the valid range.
  54. */
  55. si64 tell();
  56. /**
  57. * Skips delta numbers of bytes.
  58. *
  59. * @param delta The count of bytes to skip.
  60. * @return the count of bytes skipped actually.
  61. */
  62. si64 skip(si64 delta);
  63. /**
  64. * Gets the length in bytes of the stream.
  65. *
  66. * @return the length in bytes of the stream.
  67. */
  68. si64 getSize();
  69. private:
  70. /**
  71. * Opens a file. If a file is currently opened, it will be closed.
  72. *
  73. * @param file Path to the file.
  74. * @param start - offset from file start where real data starts (e.g file on archive)
  75. * @param size - size of real data in file (e.g file on archive) or 0 to use whole file
  76. *
  77. * @throws std::runtime_error if file wasn't found
  78. */
  79. void open(const std::string & file, si64 start, si64 size);
  80. si64 dataStart;
  81. si64 dataSize;
  82. /** Native c++ input file stream object. */
  83. std::ifstream fileStream;
  84. };