hs

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | LICENSE

hs_util.h (3579B)


      1 #ifndef HS_UTIL_H_
      2 #define HS_UTIL_H_
      3 
      4 #include <stdlib.h>
      5 #include <stdint.h>
      6 #include <assert.h>
      7 #include <string.h>
      8 #include <stdio.h>
      9 
     10 typedef struct {
     11         size_t cap, len;
     12         void* data;
     13 } hs_dynarr;
     14 
     15 #define hs_dynarr_init(_type, _cap) _hs_dynarr_init_sz(sizeof(_type), _cap)
     16 #define hs_dynarr_zero(_arr, _type) memset(_arr.data, 0, sizeof(_type) * _arr.cap)
     17 #define hs_dynarr_data(_arr, _type) ((_type*)_arr.data)
     18 #define hs_dynarr_idx(_arr, _type, _index) hs_dynarr_data(_arr, _type)[_index]
     19 
     20 #define hs_dynarr_resize(_arr, _type, _size) _hs_dynarr_resize(&_arr, sizeof(_type), _size)
     21 #define hs_dynarr_clear(_arr) _arr.len = 0
     22 #define hs_dynarr_free(_arr)                    \
     23         free(_arr.data);                        \
     24         _arr = (hs_dynarr){0}
     25 
     26 #define hs_dynarr_pop(_arr) if(_arr.len > 0) _arr.len--
     27 #define hs_dynarr_push(_arr, _type, _value)                             \
     28         if (++_arr.len >= _arr.cap)                                     \
     29                 _hs_dynarr_resize(&_arr, sizeof(_type), _arr.cap * 2);  \
     30         hs_dynarr_idx(_arr, _type, _arr.len - 1) = _value
     31 
     32 #define hs_dynarr_front(_arr, _type) hs_dynarr_idx(_arr, _type, _arr.len - 1)
     33 #define hs_dynarr_back(_arr, _type) hs_dynarr_idx(_arr, _type, 0)
     34 
     35 static inline hs_dynarr
     36 _hs_dynarr_init_sz(size_t type_size, const size_t capacity)
     37 {
     38         void* data = malloc(type_size * capacity);
     39         assert(data);
     40 
     41         return (hs_dynarr){
     42                 .cap  = capacity,
     43                 .data = data,
     44         };
     45 }
     46 
     47 static inline void
     48 _hs_dynarr_resize(hs_dynarr* dynarr, const size_t type_size, const size_t new_cap)
     49 {
     50         if (new_cap > dynarr->cap) {
     51                 void* new_data = malloc(type_size * new_cap);
     52                 assert(new_data);
     53 
     54                 memcpy(new_data, dynarr->data, dynarr->cap * type_size);
     55                 free(dynarr->data);
     56                 dynarr->data = new_data;
     57         }
     58         dynarr->cap = new_cap;
     59 }
     60 
     61 extern void hs_memsetv(void* restrict dst, const size_t num, void* restrict src, const size_t sz);
     62 
     63 extern char*    hs_file_read_null_term(const char *file_path);
     64 extern uint8_t* hs_file_read(const char *file_path);
     65 
     66 #ifdef HS_IMPL
     67 #define HS_UTIL_IMPL
     68 #endif // HS_IMPL
     69 
     70 #ifdef HS_UTIL_IMPL
     71 
     72 void
     73 hs_memsetv(void* restrict dst, const size_t num, void* restrict src, const size_t sz)
     74 {
     75         const size_t dst_end = (size_t)dst + num * sz;
     76         for(; (size_t)dst < dst_end; dst += sz)
     77                 memcpy(dst, src, sz);
     78 }
     79 
     80 char*
     81 hs_file_read_null_term(const char *file_path)
     82 {
     83         FILE *file = fopen(file_path, "r");
     84         if (!file) {
     85                 fprintf(stderr, "---error reading file \"%s\"---\n", file_path);
     86                 assert(file);
     87         }
     88 
     89         fseek(file, 0L, SEEK_END);
     90         uint32_t readsize = ftell(file);
     91         rewind(file);
     92 
     93         char* buffer = malloc(readsize + 1);
     94         assert(buffer);
     95 
     96         fread(buffer, 1, readsize, file);
     97         buffer[readsize] = '\0';
     98 
     99         fclose(file);
    100         return buffer;
    101 }
    102 
    103 uint8_t*
    104 hs_file_read_(const char *file_path)
    105 {
    106         FILE *file = fopen(file_path, "r");
    107         if (!file) {
    108                 fprintf(stderr, "---error reading file \"%s\"---\n", file_path);
    109                 assert(file);
    110         }
    111 
    112         fseek(file, 0L, SEEK_END);
    113         uint32_t readsize = ftell(file);
    114         rewind(file);
    115 
    116         uint8_t* buffer = malloc(readsize);
    117         assert(buffer);
    118 
    119         fread(buffer, 1, readsize, file);
    120 
    121         fclose(file);
    122         return buffer;
    123 }
    124 
    125 #undef HS_UTIL_IMPL
    126 #endif // HS_UTIL_IMPL
    127 
    128 #endif // HS_UTIL_H_