Stream.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef Stream_H_
  2. #define Stream_H_
  3. namespace gameplay
  4. {
  5. /**
  6. * Defines a stream for reading and writing a sequence of bytes.
  7. *
  8. * Use FileSystem::open() to create a stream.
  9. *
  10. * @script{ignore}
  11. */
  12. class Stream
  13. {
  14. public:
  15. /**
  16. * Destructor. The stream should be closed when it is destroyed.
  17. */
  18. virtual ~Stream() {};
  19. /**
  20. * Returns true if this stream can perform read operations.
  21. *
  22. * @return True if the stream can read, false otherwise.
  23. */
  24. virtual bool canRead() = 0;
  25. /**
  26. * Returns true if this stream can perform write operations.
  27. *
  28. * @return True if the stream can write, false otherwise.
  29. */
  30. virtual bool canWrite() = 0;
  31. /**
  32. * Returns true if this stream can seek.
  33. *
  34. * @return True if the stream can seek, false otherwise.
  35. */
  36. virtual bool canSeek() = 0;
  37. /**
  38. * Closes this stream.
  39. */
  40. virtual void close() = 0;
  41. /**
  42. * Reads an array of <code>count</code> elements, each of size <code>size</code>.
  43. *
  44. * \code
  45. * int numbers[3];
  46. * if (stream->read(numbers, sizeof(int), 3) != 3)
  47. * print("Error reading from file");
  48. * \endcode
  49. *
  50. * @param ptr The pointer to the memory to copy into.
  51. * The available size should be at least (<code>size * count</code>) bytes.
  52. * @param size The size of each element to be read, in bytes.
  53. * @param count The number of elements to read.
  54. *
  55. * @return The number of elements read.
  56. *
  57. * @see canRead()
  58. */
  59. virtual size_t read(void* ptr, size_t size, size_t count) = 0;
  60. /**
  61. * Reads a line from the stream.
  62. *
  63. * A new line is denoted by by either "\n", "\r" or "\r\n".
  64. * The line break character is included in the string.
  65. * The terminating null character is added to the end of the string.
  66. *
  67. * @param str The array of chars to copy the string to.
  68. * @param num The maximum number of characters to be copied.
  69. *
  70. * @return On success, str is returned. On error, NULL is returned.
  71. *
  72. * @see canRead()
  73. */
  74. virtual char* readLine(char* str, int num) = 0;
  75. /**
  76. * Writes an array of <code>count</code> elements, each of size <code>size</code>.
  77. *
  78. * \code
  79. * int numbers[] = {1, 2, 3};
  80. * if (stream->write(numbers, sizeof(int), 3) != 3)
  81. * print("Error writing to file");
  82. * \endcode
  83. *
  84. * @param ptr The pointer to the array of elements to be written.
  85. * @param size The size of each element to be written, in bytes.
  86. * @param count The number of elements to write.
  87. *
  88. * @return The number of elements written.
  89. *
  90. * @see canWrite()
  91. */
  92. virtual size_t write(const void* ptr, size_t size, size_t count) = 0;
  93. /**
  94. * Returns true if the end of the stream has been reached.
  95. *
  96. * @return True if end of stream reached, false otherwise.
  97. */
  98. virtual bool eof() = 0;
  99. /**
  100. * Returns the length of the stream in bytes.
  101. *
  102. * Zero is returned if the length of the stream is unknown and/or it cannot be seeked.
  103. *
  104. * Example: The length of a network stream is unknown and cannot be seeked.
  105. *
  106. * @return The length of the stream in bytes.
  107. */
  108. virtual size_t length() = 0;
  109. /**
  110. * Returns the position of the file pointer. Zero is the start of the stream.
  111. *
  112. * @return The file indicator offset in bytes.
  113. */
  114. virtual long int position() = 0;
  115. /**
  116. * Sets the position of the file pointer.
  117. *
  118. * Use canSeek() to determine if this method is supported.
  119. *
  120. * @param offset The number of bytes to offset from origin.
  121. * @param origin The position used as a reference for offset.
  122. * The supported values are the same as fseek().
  123. * - <code>SEEK_SET</code> relative to the beginning of the file.
  124. * - <code>SEEK_CUR</code> relative to the current position of the file pointer.
  125. * - <code>SEEK_END</code> relative to the end of file.
  126. *
  127. * @return True if successful, false otherwise.
  128. *
  129. * @see canSeek()
  130. */
  131. virtual bool seek(long int offset, int origin) = 0;
  132. /**
  133. * Moves the file pointer to the start of the file.
  134. *
  135. * Use canSeek() to determine if this method is supported.
  136. *
  137. * @return True if successful, false otherwise.
  138. *
  139. * @see canSeek()
  140. */
  141. virtual bool rewind() = 0;
  142. protected:
  143. Stream() {};
  144. private:
  145. Stream(const Stream&); // Hidden copy constructor.
  146. Stream& operator=(const Stream&); // Hidden copy assignment operator.
  147. };
  148. }
  149. #endif