CMemoryStream.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * CMemoryStream.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. /**
  13. * A class which provides method definitions for reading from memory.
  14. */
  15. class DLL_LINKAGE CMemoryStream : public CInputStream
  16. {
  17. public:
  18. /**
  19. * Default c-tor.
  20. */
  21. CMemoryStream();
  22. /**
  23. * C-tor. The data buffer will be freed by the stream's destructor.
  24. *
  25. * @param data A pointer to the data array.
  26. * @param size The size in bytes of the array.
  27. * @param freeData Flag which specifies if the data array should be freed in the memory stream's destructor.
  28. */
  29. CMemoryStream(const ui8 * data, si64 size, bool freeData = false);
  30. /**
  31. * D-tor. Frees the data array if the freeData flag was set to true.
  32. */
  33. ~CMemoryStream();
  34. /**
  35. * Opens the stream with the given data array. If a stream is currently loaded, it will be freed
  36. * if neccessary.
  37. *
  38. * @param data A pointer to the data array.
  39. * @param size The size in bytes of the array.
  40. * @param freeData Flag which specifies if the data array should be freed in the memory stream's destructor.
  41. */
  42. void open(const ui8 * data, si64 size, bool freeData = false);
  43. /**
  44. * Reads n bytes from the stream into the data buffer.
  45. *
  46. * @param data A pointer to the destination data array.
  47. * @param size The number of bytes to read.
  48. * @return the number of bytes read actually.
  49. */
  50. si64 read(ui8 * data, si64 size);
  51. /**
  52. * Seeks the internal read pointer to the specified position.
  53. *
  54. * @param position The read position from the beginning.
  55. * @return the position actually moved to, -1 on error.
  56. */
  57. si64 seek(si64 position);
  58. /**
  59. * Gets the current read position in the stream.
  60. *
  61. * @return the read position.
  62. */
  63. si64 tell();
  64. /**
  65. * Skips delta numbers of bytes.
  66. *
  67. * @param delta The count of bytes to skip.
  68. * @return the count of bytes skipped actually.
  69. */
  70. si64 skip(si64 delta);
  71. /**
  72. * Gets the length in bytes of the stream.
  73. *
  74. * @return the length in bytes of the stream.
  75. */
  76. si64 getSize();
  77. /**
  78. * Closes the stream and releases any system resources associated with the stream explicitely.
  79. */
  80. void close();
  81. private:
  82. /** A pointer to the data array. */
  83. const ui8 * data;
  84. /** The size in bytes of the array. */
  85. si64 size;
  86. /** Current reading position of the stream. */
  87. si64 position;
  88. /** Flag which specifies if the data array should be freed in the memory stream's destructor. */
  89. bool freeData;
  90. };