SIZE_FMT gets the size of a cstr with the given sprintf formatted string and arguments using `snprintf`. vec_append_fmt formats a cstr with the given sprintf arguments then appends it to the given vector. This simplifies a little bit of the wordy implementation of the assembler.
60 lines
1.3 KiB
C
60 lines
1.3 KiB
C
#ifndef LIB_H
|
|
#define LIB_H
|
|
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
typedef uint8_t u8;
|
|
typedef uint16_t u16;
|
|
typedef uint32_t u32;
|
|
typedef uint64_t u64;
|
|
|
|
typedef int8_t i8;
|
|
typedef int16_t i16;
|
|
typedef int32_t i32;
|
|
typedef int64_t i64;
|
|
|
|
#define MAX(A, B) ((A) < (B) ? (B) : (A))
|
|
#define MIN(A, B) ((A) > (B) ? (A) : (B))
|
|
#define MEMORY_DEFAULT 30000
|
|
|
|
bool usable_character(char c);
|
|
char *fread_all(FILE *fp);
|
|
void print_error(const char *handle, size_t row, size_t column,
|
|
const char *reason);
|
|
|
|
#define SIZE_FMT(FMT, ...) (snprintf(NULL, 0, (FMT), __VA_ARGS__))
|
|
|
|
typedef struct Buffer
|
|
{
|
|
const char *name;
|
|
u64 size;
|
|
u8 data[];
|
|
} buffer_t;
|
|
|
|
buffer_t *buffer_init_str(const char *name, const char *str, size_t str_size);
|
|
|
|
typedef struct
|
|
{
|
|
u64 size, capacity;
|
|
u8 *data;
|
|
} vec_t;
|
|
|
|
void vec_append(vec_t *vec, const void *const ptr, u64 size);
|
|
void vec_ensure(vec_t *vec, u64 abs_size);
|
|
void vec_ensure_free(vec_t *vec, u64 rel_size);
|
|
void vec_free(vec_t *vec);
|
|
|
|
#define vec_append_fmt(VEC, FMT, ...) \
|
|
do \
|
|
{ \
|
|
char f[SIZE_FMT(FMT, __VA_ARGS__) + 1]; \
|
|
snprintf(f, sizeof(f), (FMT), __VA_ARGS__); \
|
|
vec_append((VEC), f, sizeof(f) - 1); \
|
|
} while (0)
|
|
|
|
#endif
|