Files
brightstone/Common/std.h
T
2026-10-12 21:12:47 +03:00

89 lines
3.2 KiB
C

/*
Minimal subset of stdlib, none of which have to be actually implemented,
even memory mnagement.
*/
#ifndef COM_STD_H
#define COM_STD_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
#define COM_STD_INFD 0
#define COM_STD_OUTFD 1
#else
/* TODO: Make it more graceful, probably with static func. */
#include <unistd.h>
#define COM_STD_INFD (fdopen(STDIN_FILENO, "r"))
#define COM_STD_OUTFD (fdopen(STDOUT_FILENO, "w"))
#endif
/* Generic system interface, linux/bsd derived. */
typedef void *com_std_fd;
#define COM_STD_OPEN_READ (1 << 0)
#define COM_STD_OPEN_WRITE (1 << 1)
com_std_fd com_std_open(char const *path, uint32_t flags);
bool com_std_write(com_std_fd fd, void const *data, uint32_t bytes);
int32_t com_std_read(com_std_fd fd, void *data, uint32_t upto);
bool com_std_has_data(com_std_fd);
bool com_std_sfwd(com_std_fd fd, uint32_t bytes);
com_std_fd com_std_socket(bool isnetwork);
bool com_std_connect(com_std_fd sock, bool isnetwork, char const *address);
void com_std_exit(uint32_t status) __attribute__((noreturn));
void com_std_close(com_std_fd fd);
/* TODO: Remove, use buffer semantics instead. */
/* Instead have a mmap functions here. */
/* Note: Freeing is done by passing 0 size to pointer. */
void *com_std_alloc(void *ptr, size_t size);
/* Note: Functions listed below are only available for dev builds. */
/* Trigonometry functions are for reference testing of our own fixed point
* implementations. Do not use otherwise. */
#define com_std_assert(p_h) \
do { \
if (!(p_h)) { \
const char msg[] = \
"Assertion failed at "__FILE__ \
":" COM_DEF_STRLIT(__LINE__) ": " COM_DEF_STRLIT(p_h); \
com_std_print_str(msg, sizeof msg, true); \
com_std_exit(-1); \
} \
} while (0)
/*
Basic printing utility that doesn't utilize format string.
Instead a vector of arguments is passed and concatenated, based on type.
Newline is implied at the end.
Example: com_std_print("My unit32_t: ", 'u', value, 's', " is here");
Format specifiers:
'u' = uint32_t
'i' = int32_t
'd' = float (upcast to double with vargs)
'c' = null terminated C string
Our own types:
'f' = com_fix value
'p' = com_pnt pointer
'v' = com_vec pointer
's' = pointer to com_str structure
'b' = pointer to com_buf structure
'.' is placed at the end implicitly with macro,
it is needed to know when to end.
*/
void com_std_print_(int32_t start, ...);
#define com_std_print(...) com_std_print_(__VA_ARGS__, '.')
void com_std_print_str(char const *str, uint32_t len, bool newline);
double com_std_sqrt(double v);
double com_std_fabs(double v);
double com_std_sin(double v);
double com_std_cos(double v);
#endif