gs_byte_buffer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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
  11. {
  12. u8* buffer; // 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;
  17. // Generic "read" function for a byte buffer
  18. #define gs_byte_buffer_read( bb, T )\
  19. __gs_byte_buffer_read_##T( bb )
  20. // Generic "write" function for a byte buffer
  21. #define gs_byte_buffer_write( bb, T, val )\
  22. do {\
  23. gs_byte_buffer* _buffer = bb;\
  24. usize sz = sizeof( T );\
  25. usize total_write_size = _buffer->position + sz;\
  26. if ( total_write_size >= _buffer->capacity )\
  27. {\
  28. usize capacity = _buffer->capacity * 2;\
  29. while( capacity < total_write_size )\
  30. {\
  31. capacity *= 2;\
  32. }\
  33. gs_byte_buffer_resize( _buffer, capacity );\
  34. }\
  35. *( T* )( _buffer->buffer + _buffer->position ) = val;\
  36. _buffer->position += sz;\
  37. _buffer->size += sz;\
  38. } while( 0 )
  39. void gs_byte_buffer_init( gs_byte_buffer* buffer );
  40. gs_byte_buffer gs_byte_buffer_new();
  41. void gs_byte_buffer_free( gs_byte_buffer* buffer );
  42. void gs_byte_buffer_clear( gs_byte_buffer* buffer );
  43. void gs_byte_buffer_resize( gs_byte_buffer* buffer, usize sz );
  44. void gs_byte_buffer_seek_to_beg( gs_byte_buffer* buffer );
  45. void gs_byte_buffer_seek_to_end( gs_byte_buffer* buffer );
  46. void gs_byte_buffer_advance_position( gs_byte_buffer* buffer, usize sz );
  47. void gs_byte_buffer_write_str( gs_byte_buffer* buffer, const char* str ); // Expects a null terminated string
  48. void gs_byte_buffer_read_str( gs_byte_buffer* buffer, char* str ); // Expects an allocated string
  49. void gs_byte_buffer_bulk_write( gs_byte_buffer* buffer, void* src, u32 sz );
  50. void gs_byte_buffer_bulk_read( gs_byte_buffer* buffer, void* dst, u32 sz );
  51. gs_result gs_byte_buffer_write_to_file( gs_byte_buffer* buffer, const char* output_path ); // Assumes that the output directory exists
  52. gs_result gs_byte_buffer_read_from_file( gs_byte_buffer* buffer, const char* file_path ); // Assumes an allocated byte buffer
  53. // "Generic" read functions
  54. #define __gs_byte_buffer_func( T )\
  55. T __gs_byte_buffer_read_##T( gs_byte_buffer* bb )
  56. __gs_byte_buffer_func( usize );
  57. __gs_byte_buffer_func( s8 );
  58. __gs_byte_buffer_func( u8 );
  59. __gs_byte_buffer_func( s16 );
  60. __gs_byte_buffer_func( u16 );
  61. __gs_byte_buffer_func( s32 );
  62. __gs_byte_buffer_func( u32 );
  63. __gs_byte_buffer_func( b32 );
  64. __gs_byte_buffer_func( s64 );
  65. __gs_byte_buffer_func( u64 );
  66. __gs_byte_buffer_func( f32 );
  67. __gs_byte_buffer_func( f64 );
  68. __gs_byte_buffer_func( gs_vec2 );
  69. __gs_byte_buffer_func( gs_vec3 );
  70. __gs_byte_buffer_func( gs_vec4 );
  71. __gs_byte_buffer_func( gs_quat );
  72. __gs_byte_buffer_func( gs_mat4 );
  73. __gs_byte_buffer_func( gs_vqs );
  74. #ifdef __cplusplus
  75. }
  76. #endif // c++
  77. #endif // __GS_BYTE_BUFFER_H__