]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/util.h
e9ff1218df6efe397ffd8a24c827b154102b4b90
[dragonblocks_alpha.git] / src / util.h
1 #ifndef _UTIL_H_
2 #define _UTIL_H_
3
4 #include <stdbool.h>
5 #ifdef WIN32
6 #include <winsock2.h>
7 #include <ws2tcpip.h>
8 #else
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
13 #endif
14 #include <dragontype/number.h>
15
16 #define ever (;;)                                                                                                                               // infinite for loop with style
17 #define INBRACES(str) (str) ? "(" : "", (str) ? (str) : "", (str) ? ")" : ""    // wrapper for printf to optionally add a message in braces if message is not NULL
18 #define CMPBOUNDS(x) ((x) == 0 ? 0 : (x) > 0 ? 1 : -1)                                                  // resolves to 1 if x > 0, 0 if x == 0 and -1 if x < 0
19 #define fallthrough __attribute__ ((fallthrough))                                                               // prevent compiler warning about implicit fallthrough with style
20 #define unused __attribute__ ((unused))
21 #define U32(x) (((u32) 1 << 31) + (x))
22
23 extern const char *program_name;        // this has to be set to program name on startup
24
25 void syscall_error(const char *err);                                                                                                                                                                            // print system call related error message and exit
26 void internal_error(const char *err);                                                                                                                                                                           // print general error message and exit
27 char *read_string(int fd, size_t bufsiz);                                                                                                                                                                       // read from fd until \0 or EOF terminator
28 char *address_string(struct sockaddr_in6 *addr);                                                                                                                                                        // convert IPv6 address to human readable, return allocated buffer
29 void my_compress(const void *uncompressed, size_t uncompressed_size, char **compressed, size_t *compressed_size);                       // compress data using ZLib and store result(buffer allocated by malloc) in compressed
30 bool my_decompress(const char *compressed, size_t compressed_size, void *decompressed, size_t expected_decompressed_size);      // decompress data and put result into decompressed, return false if decompressed size does not match expected_decompressed_size
31 bool within_simulation_distance(v3f64 player_pos, v3s32 block_pos, u32 simulation_distance);                                                            // return true if a player is close enough to a block to access it
32 f64 clamp(f64 v, f64 min, f64 max);
33 char *format_string(const char *format, ...);
34 void *buffer_read(unsigned char **buffer, size_t *bufsiz, size_t size);
35 void buffer_write(unsigned char **buffer, size_t *bufsiz, void *data, size_t size);
36
37 #endif