diff options
author | bonmas14 <bonmas14@gmail.com> | 2025-09-28 17:53:03 +0000 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2025-09-28 17:53:03 +0000 |
commit | 30751aece04ba8954513bbb7aca996244e6fa684 (patch) | |
tree | c40dfac95dd3c0766656324c45465f0375061b5d | |
parent | 3ad95773ae4f963435003c260278177f1e22577e (diff) | |
download | ungrateful-30751aece04ba8954513bbb7aca996244e6fa684.tar.gz ungrateful-30751aece04ba8954513bbb7aca996244e6fa684.zip |
changes
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | src/cynic.h | 8 | ||||
-rw-r--r-- | src/d_linux.c | 56 | ||||
-rw-r--r-- | src/d_udp_win.c | 9 | ||||
-rw-r--r-- | src/d_win.c (renamed from src/d_tcp_win.c) | 24 | ||||
-rw-r--r-- | src/disgrace.c | 44 | ||||
-rw-r--r-- | src/disgrace.h | 2 | ||||
-rw-r--r-- | src/ungrateful.c | 5 | ||||
-rw-r--r-- | src/ungrateful.h | 4 | ||||
-rw-r--r-- | tests/cyn/file.c | 1 | ||||
-rw-r--r-- | tests/cyn/log.c | 2 | ||||
-rw-r--r-- | tests/d/test.c | 6 | ||||
-rw-r--r-- | tests/un/allocs.c | 2 | ||||
-rw-r--r-- | tests/un/math.c | 2 | ||||
-rw-r--r-- | tests/un/mathd.c | 1 | ||||
-rw-r--r-- | tests/un/mathf.c | 1 | ||||
-rw-r--r-- | tests/un/matrix.c | 1 | ||||
-rw-r--r-- | tests/un/strings.c | 2 | ||||
-rw-r--r-- | tests/un/win_wstr.c | 3 |
19 files changed, 118 insertions, 58 deletions
@@ -30,8 +30,8 @@ Types, Strings, basic memory allocators (platform specific), and so on. Features + easing; + vecs; + splines (qubic); -+ ~ Memory debugging; @todo, we have wrapper but none of the debug functionality. + complex numbers; ++ Wide strings (internal); Todo: - matrix; @@ -40,7 +40,6 @@ Todo: - Random; - Noises; - Compression; -- Wide strings (internal); - Sorting; # Cynic diff --git a/src/cynic.h b/src/cynic.h index 5380b84..ef56555 100644 --- a/src/cynic.h +++ b/src/cynic.h @@ -34,6 +34,8 @@ extern "C" { #endif +extern void cyn_init(void); + /* ---- Library loading API ---- */ /* ------- Threading API ------- */ /* -------- Logging API -------- */ @@ -111,15 +113,13 @@ typedef enum { CYN_FILE_OPEN_EXISTING = 1 << 9, } File_Open_Flags; -typedef struct File_Handle File_Handle; - // extern File_Handle *cyn_file_open(String path, File_Open_Flags flags, File_Status *status); -extern File_Handle *cyn_file_open(String path, File_Status *status); +extern struct File_Handle *cyn_file_open(String path, File_Status *status); /* path - could be partial or full, should be always with '/' * status - is optional, * returns NULL on error and writes error into a `status`. * */ -extern void cyn_file_close(File_Handle *handle); +extern void cyn_file_close(struct File_Handle *handle); /* --------- Config API -------- */ diff --git a/src/d_linux.c b/src/d_linux.c new file mode 100644 index 0000000..5ff6abd --- /dev/null +++ b/src/d_linux.c @@ -0,0 +1,56 @@ +typedef struct DHandle { + u64 type; +} DHandle; + +b32 d_init(void) { + return true; +} + +String d_hostname_ip_get(String host, Allocator alloc) { + char *host_cstr; + Allocator talloc; + int err; + String ip; + + talloc = un_alloc_temp_get(); + host_cstr = (char *) un_string_to_cstring(host, talloc); + + // @note, host_info can return multiple ip addresses? + struct hostent *host_info = gethostbyname(host_cstr); + + if (!host_info) { + err = h_errno; + + switch (err) { + case TRY_AGAIN: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (TRY_AGAIN)."); + break; + case HOST_NOT_FOUND: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (HOST_NOT_FOUND)."); + break; + case NO_DATA: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: no host data (NO_DATA)."); + break; + default: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: unspecified error."); + break; + } + + return CLITERAL(String) { 0 }; + } + + if (host_info) { + assert(host_info->h_addrtype == AF_INET); + + assert(host_info->h_length >= (s32)sizeof(struct in_addr)); + ip = un_string_from_cstring(inet_ntoa(*(struct in_addr *)*host_info->h_addr_list)); + + return un_string_copy(ip, alloc); + } + + return CLITERAL(String) { 0 }; +} + +DHandle* d_tcp_connect(String ip, u16 port) { + +} diff --git a/src/d_udp_win.c b/src/d_udp_win.c deleted file mode 100644 index e3c0074..0000000 --- a/src/d_udp_win.c +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/src/d_tcp_win.c b/src/d_win.c index e033153..3375ee9 100644 --- a/src/d_tcp_win.c +++ b/src/d_win.c @@ -1,5 +1,21 @@ +typedef struct DHandle { + u64 type; +} DHandle; -extern String d_get_hostname_ip(String host, Allocator alloc) { +b32 d_init(void) { + WSADATA data; + int error; + + if (WSAStartup(MAKEWORD(2, 2), &data)) { + error = WSAGetLastError(); + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "WsaStartup failed, code: %d.", (s64)error); + return false; + } + + return true; +} + +extern String d_hostname_ip_get(String host, Allocator alloc) { char *host_cstr; Allocator talloc; int err; @@ -12,8 +28,6 @@ extern String d_get_hostname_ip(String host, Allocator alloc) { HOSTENT *host_info = gethostbyname(host_cstr); if (!host_info) { - - err = WSAGetLastError(); switch (err) { @@ -40,7 +54,7 @@ extern String d_get_hostname_ip(String host, Allocator alloc) { break; } - return CLITERAL(String) {}; + return CLITERAL(String) { 0 }; } if (host_info) { @@ -52,7 +66,7 @@ extern String d_get_hostname_ip(String host, Allocator alloc) { return un_string_copy(ip, alloc); } - return CLITERAL(String) {}; + return CLITERAL(String) { 0 }; } diff --git a/src/disgrace.c b/src/disgrace.c index b5e5666..424ca5b 100644 --- a/src/disgrace.c +++ b/src/disgrace.c @@ -1,53 +1,35 @@ #include "disgrace.h" +enum { + D_TYPE_TCP, + D_TYPE_UDP +}; + #if defined(OS_WINDOWS) #include <winsock2.h> +#include <ws2tcpip.h> #include <windows.h> #if defined(MSVC_COMPILER) #pragma comment(lib, "Ws2_32.lib") #endif -enum { - D_TYPE_TCP, - D_TYPE_UDP -}; - -typedef struct DHandle { - u64 type; -} DHandle; - -b32 d_init(void) { - WSADATA data; - int error; - - if (WSAStartup(MAKEWORD(2, 2), &data)) { - error = WSAGetLastError(); - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "WsaStartup failed, code: %d.", (s64)error); - return false; - } - - return true; -} - - -#include "d_tcp_win.c" -#include "d_udp_win.c" +#include "d_win.c" #elif defined(OS_LINUX) -b32 d_init(void) { - return true; -} +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <netinet/in.h> +#include <arpa/inet.h> -// #error "Linux todo" -// #include "d_udp_linux.c" +#include "d_linux.c" #else #error "unknown platform" - #endif diff --git a/src/disgrace.h b/src/disgrace.h index 43f78fb..d129938 100644 --- a/src/disgrace.h +++ b/src/disgrace.h @@ -38,7 +38,7 @@ struct DHandle; b32 d_init(void); -extern String d_get_hostname_ip(String host, Allocator alloc); +extern String d_hostname_ip_get(String host, Allocator alloc); extern struct DHandle* d_tcp_connect(String ip, u16 port); extern struct DHandle* d_tcp_listen(); diff --git a/src/ungrateful.c b/src/ungrateful.c index 5031a45..96c9648 100644 --- a/src/ungrateful.c +++ b/src/ungrateful.c @@ -14,3 +14,8 @@ #include "un_vec.c" #include "un_vecd.c" + + +void un_init(u64 size_of_temp_mem) { + un_alloc_temp_init(size_of_temp_mem); +} diff --git a/src/ungrateful.h b/src/ungrateful.h index f9b2234..a3918aa 100644 --- a/src/ungrateful.h +++ b/src/ungrateful.h @@ -171,6 +171,10 @@ typedef struct double4 { double x, y, z, w; } double4; #define un_vec_unwrap3(vec) (vec).x, (vec).y, (vec).z #define un_vec_unwrap4(vec) (vec).x, (vec).y, (vec).z, (vec).w +/* ---- Default ---- */ + +extern void un_init(u64 size_of_temp_mem); + /* ---- Memory Allocators API ---- */ typedef enum { diff --git a/tests/cyn/file.c b/tests/cyn/file.c index 7767f7a..a847c1d 100644 --- a/tests/cyn/file.c +++ b/tests/cyn/file.c @@ -1,6 +1,7 @@ #include <cynic.h> int main(void) { + un_init(UN_KB(4)); // cyn_log_write(CYN_LOG_INFO, UN_STR("Size of it is: %l"), sizeof(File_Handle)); File_Handle file = cyn_file_open(UN_STR("Ligma.png")); } diff --git a/tests/cyn/log.c b/tests/cyn/log.c index 067d557..9a61bef 100644 --- a/tests/cyn/log.c +++ b/tests/cyn/log.c @@ -1,7 +1,7 @@ #include <cynic.h> int main(void) { - un_alloc_temp_init(UN_KB(4)); + un_init(UN_KB(4)); cyn_log_write(CYN_LOG_INFO, UN_STR("Hello %u %d %s!"), (u64)100, (s64)-400, UN_STR("World")); } diff --git a/tests/d/test.c b/tests/d/test.c index 554926c..6e6442b 100644 --- a/tests/d/test.c +++ b/tests/d/test.c @@ -2,11 +2,13 @@ int main(void) { - un_alloc_temp_init(UN_KB(4)); + un_init(UN_KB(4)); d_init(); - cyn_log_write(CYN_LOG_INFO, d_get_hostname_ip(UN_STR("f-tier-games.ru"), un_alloc_temp_get())); + String host = UN_STR("f-tier-games.ru"); + + cyn_log_write(CYN_LOG_INFO, UN_STR("%s: %s"), host, d_hostname_ip_get(host, un_alloc_temp_get())); return 0; } diff --git a/tests/un/allocs.c b/tests/un/allocs.c index 220c2c8..36e75fa 100644 --- a/tests/un/allocs.c +++ b/tests/un/allocs.c @@ -4,7 +4,7 @@ int main(void) { u64 i, size, *value; size = UN_KB(1); - un_alloc_temp_init(UN_KB(4)); + un_init(UN_MB(4)); { // std Allocator std = un_alloc_std_get(); diff --git a/tests/un/math.c b/tests/un/math.c index 87bea6b..94d3b11 100644 --- a/tests/un/math.c +++ b/tests/un/math.c @@ -1,6 +1,8 @@ #include <ungrateful.h> int main() { + un_init(UN_MB(4)); + // Lerping { double d; diff --git a/tests/un/mathd.c b/tests/un/mathd.c index 10ef24b..879cb89 100644 --- a/tests/un/mathd.c +++ b/tests/un/mathd.c @@ -1,6 +1,7 @@ #include <ungrateful.h> int main() { + un_init(UN_MB(4)); /* 2d */ { double2 a = {1.0, 2.0}; diff --git a/tests/un/mathf.c b/tests/un/mathf.c index 5b8d4c1..3d736af 100644 --- a/tests/un/mathf.c +++ b/tests/un/mathf.c @@ -1,6 +1,7 @@ #include <ungrateful.h> int main() { + un_init(UN_MB(4)); /* 2d */ { float2 a = { 1.0f, 2.0f }; diff --git a/tests/un/matrix.c b/tests/un/matrix.c index 60fd9a5..d5b3e22 100644 --- a/tests/un/matrix.c +++ b/tests/un/matrix.c @@ -1,5 +1,6 @@ #include <ungrateful.h> int main() { + un_init(UN_MB(4)); return 0; } diff --git a/tests/un/strings.c b/tests/un/strings.c index 72d84ad..90365b6 100644 --- a/tests/un/strings.c +++ b/tests/un/strings.c @@ -5,7 +5,7 @@ int main(void) { Allocator alloc; String result; - un_alloc_temp_init(UN_MB(4)); + un_init(UN_MB(4)); alloc = un_alloc_temp_get(); diff --git a/tests/un/win_wstr.c b/tests/un/win_wstr.c index 51fc820..1bc78bf 100644 --- a/tests/un/win_wstr.c +++ b/tests/un/win_wstr.c @@ -1,4 +1,5 @@ #include <ungrateful.h> + #if !defined(OS_WINDOWS) int main(void) { return 0; @@ -11,7 +12,7 @@ int main(void) { int main(void) { wchar *caption, *text; - un_alloc_temp_init(UN_KB(4)); + un_init(UN_MB(4)); Allocator talloc = un_alloc_temp_get(); |