stream_cache.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include "stream.h"
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. typedef struct StreamCache StreamCache;
  7. /**
  8. * Allocate stream cache.
  9. * @return StreamCache* pointer to a StreamCache instance
  10. */
  11. StreamCache* stream_cache_alloc();
  12. /**
  13. * Free stream cache.
  14. * @param cache Pointer to a StreamCache instance
  15. */
  16. void stream_cache_free(StreamCache* cache);
  17. /**
  18. * Drop the cache contents and set it to initial state.
  19. * @param cache Pointer to a StreamCache instance
  20. */
  21. void stream_cache_drop(StreamCache* cache);
  22. /**
  23. * Determine if the internal cursor is at end the end of cached data.
  24. * @param cache Pointer to a StreamCache instance
  25. * @return True if cursor is at end, otherwise false.
  26. */
  27. bool stream_cache_at_end(StreamCache* cache);
  28. /**
  29. * Get the current size of cached data.
  30. * @param cache Pointer to a StreamCache instance
  31. * @return Size of cached data.
  32. */
  33. size_t stream_cache_size(StreamCache* cache);
  34. /**
  35. * Get the internal cursor position.
  36. * @param cache Pointer to a StreamCache instance
  37. * @return Cursor position inside the cache.
  38. */
  39. size_t stream_cache_pos(StreamCache* cache);
  40. /**
  41. * Load the cache with new data from a stream.
  42. * @param cache Pointer to a StreamCache instance
  43. * @param stream Pointer to a Stream instance
  44. * @return Size of newly cached data.
  45. */
  46. size_t stream_cache_fill(StreamCache* cache, Stream* stream);
  47. /**
  48. * Write as much cached data as possible to a stream.
  49. * @param cache Pointer to a StreamCache instance
  50. * @param stream Pointer to a Stream instance
  51. * @return True on success, False on failure.
  52. */
  53. bool stream_cache_flush(StreamCache* cache, Stream* stream);
  54. /**
  55. * Read cached data and advance the internal cursor.
  56. * @param cache Pointer to a StreamCache instance.
  57. * @param data Pointer to a data buffer. Must be initialized.
  58. * @param size Maximum size in bytes to read from the cache.
  59. * @return Actual size that was read.
  60. */
  61. size_t stream_cache_read(StreamCache* cache, uint8_t* data, size_t size);
  62. /**
  63. * Write to cached data and advance the internal cursor.
  64. * @param cache Pointer to a StreamCache instance.
  65. * @param data Pointer to a data buffer.
  66. * @param size Maximum size in bytes to write to the cache.
  67. * @return Actual size that was written.
  68. */
  69. size_t stream_cache_write(StreamCache* cache, const uint8_t* data, size_t size);
  70. /**
  71. * Move the internal cursor relatively to its current position.
  72. * @param cache Pointer to a StreamCache instance.
  73. * @param offset Cursor offset.
  74. * @return Actual cursor offset. Equal to offset parameter on hit.
  75. */
  76. int32_t stream_cache_seek(StreamCache* cache, int32_t offset);
  77. #ifdef __cplusplus
  78. }
  79. #endif