gs_byte_buffer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef __GS_BYTE_BUFFER_H__
  2. #define __GS_BYTE_BUFFER_H__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "common/gs_types.h"
  7. #include "math/gs_math.h"
  8. #define gs_byte_buffer_default_capacity 1024
  9. /* Byte buffer */
  10. typedef struct gs_byte_buffer_t
  11. {
  12. u8* data; // Buffer that actually holds all relevant byte data
  13. u32 size; // Current size of the stored buffer data
  14. u32 position; // Current read/write position in the buffer
  15. u32 capacity; // Current max capacity for the buffer
  16. } gs_byte_buffer_t;
  17. // Generic "write" function for a byte buffer
  18. #define gs_byte_buffer_write(bb, T, val)\
  19. do {\
  20. gs_byte_buffer_t* _buffer = bb;\
  21. usize sz = sizeof(T);\
  22. usize total_write_size = _buffer->position + sz;\
  23. if (total_write_size >= _buffer->capacity)\
  24. {\
  25. usize capacity = _buffer->capacity * 2;\
  26. while(capacity < total_write_size)\
  27. {\
  28. capacity *= 2;\
  29. }\
  30. gs_byte_buffer_resize(_buffer, capacity);\
  31. }\
  32. *(T*)(_buffer->data + _buffer->position) = val;\
  33. _buffer->position += sz;\
  34. _buffer->size += sz;\
  35. } while (0)
  36. // Generic "read" function
  37. #define gs_byte_buffer_read(_buffer, T, _val_p)\
  38. do {\
  39. T* _v = (T*)(_val_p);\
  40. gs_byte_buffer_t* _bb = (_buffer);\
  41. *(_v) = *(T*)(_bb->data + _bb->position);\
  42. _bb->position += sizeof(T);\
  43. } while (0)
  44. // Defines variable and sets value from buffer in place
  45. // Use to construct a new variable
  46. #define gs_byte_buffer_readc(_buffer, T, name)\
  47. T name = gs_default_val();\
  48. gs_byte_buffer_read((_buffer), T, &name);
  49. void gs_byte_buffer_init(gs_byte_buffer_t* buffer);
  50. gs_byte_buffer_t gs_byte_buffer_new();
  51. void gs_byte_buffer_free(gs_byte_buffer_t* buffer);
  52. void gs_byte_buffer_clear(gs_byte_buffer_t* buffer);
  53. void gs_byte_buffer_resize(gs_byte_buffer_t* buffer, usize sz);
  54. void gs_byte_buffer_seek_to_beg(gs_byte_buffer_t* buffer);
  55. void gs_byte_buffer_seek_to_end(gs_byte_buffer_t* buffer);
  56. void gs_byte_buffer_advance_position(gs_byte_buffer_t* buffer, usize sz);
  57. void gs_byte_buffer_write_str(gs_byte_buffer_t* buffer, const char* str); // Expects a null terminated string
  58. void gs_byte_buffer_read_str(gs_byte_buffer_t* buffer, char* str); // Expects an allocated string
  59. void gs_byte_buffer_bulk_write(gs_byte_buffer_t* buffer, void* src, u32 sz);
  60. void gs_byte_buffer_bulk_read(gs_byte_buffer_t* buffer, void* dst, u32 sz);
  61. gs_result gs_byte_buffer_write_to_file(gs_byte_buffer_t* buffer, const char* output_path); // Assumes that the output directory exists
  62. gs_result gs_byte_buffer_read_from_file(gs_byte_buffer_t* buffer, const char* file_path); // Assumes an allocated byte buffer
  63. #ifdef __cplusplus
  64. }
  65. #endif // c++
  66. #endif // __GS_BYTE_BUFFER_H__