summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/kit.inl.h137
1 files changed, 103 insertions, 34 deletions
diff --git a/include/kit.inl.h b/include/kit.inl.h
index cf5b696..50e5030 100644
--- a/include/kit.inl.h
+++ b/include/kit.inl.h
@@ -1,7 +1,7 @@
// ================================================================
//
// kit.inl.h
-// https://guattari.tech/kit.git
+// https://guattari.tech/git/kit
//
// Single header-only cross-platform utility library for C.
//
@@ -85,6 +85,7 @@ enum {
KIT_ERROR_UNMAP_FAILED = (1 << 18),
KIT_ERROR_SYNC_FAILED = (1 << 19),
KIT_ERROR_CLOSE_FAILED = (1 << 20),
+ KIT_ERROR_RESOURCE_UNAVAILABLE = (1 << 21),
KIT_ERROR_NOT_IMPLEMENTED = (1 << 30),
};
typedef i32 kit_status_t;
@@ -714,9 +715,51 @@ static char *kit_make_bs(kit_str_t s) {
// ================================================================
#ifndef KIT_STRING_BUILDER_H
#define KIT_STRING_BUILDER_H
+#include <assert.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
typedef KIT_DA(char) kit_str_builder_t;
+#ifdef __GNUC__
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wunused-function"
+# pragma GCC diagnostic ignored "-Wunknown-pragmas"
+# pragma GCC push_options
+# pragma GCC optimize("O3")
+#endif
+static kit_str_builder_t kit_str_build(kit_str_t s,
+ kit_allocator_t *alloc) {
+ kit_str_builder_t builder;
+ KIT_DA_INIT(builder, s.size, alloc);
+ assert(builder.size == s.size);
+ if (builder.size == s.size)
+ memcpy(builder.values, s.values, s.size);
+ return builder;
+}
+static kit_status_t kit_str_append(kit_str_builder_t *a,
+ kit_str_t b) {
+ assert(a != NULL);
+ if (a == NULL)
+ return KIT_ERROR_INVALID_ARGUMENT;
+ if (b.size <= 0)
+ return KIT_OK;
+ i64 n = a->size;
+ KIT_DA_RESIZE(*a, n + b.size);
+ if (a->size != n + b.size)
+ return KIT_ERROR_BAD_ALLOC;
+ memcpy(a->values + n, b.values, b.size);
+ return KIT_OK;
+}
+#ifdef __GNUC__
+# pragma GCC pop_options
+# pragma GCC diagnostic pop
+#endif
+#ifdef __cplusplus
+}
+#endif
#ifndef KIT_DISABLE_SHORT_NAMES
# define str_builder_t kit_str_builder_t
+# define str_append kit_str_append
#endif
#endif
// ================================================================
@@ -1587,6 +1630,15 @@ static void kit_async_function_dispatch(void *promise) {
#ifdef __cplusplus
extern "C" {
#endif
+#if defined(_WIN32) && !defined(__CYGWIN__)
+# define KIT_PATH_DELIM_C '\\'
+# define KIT_PATH_DELIM "\\"
+# define KIT_ENV_HOME "USERPROFILE"
+#else
+# define KIT_PATH_DELIM_C '/'
+# define KIT_PATH_DELIM "/"
+# define KIT_ENV_HOME "HOME"
+#endif
typedef enum {
KIT_PATH_NONE,
KIT_PATH_FILE,
@@ -1614,21 +1666,14 @@ typedef struct {
int _fd;
#endif
} kit_mapped_file_t;
-#if defined(_WIN32) && !defined(__CYGWIN__)
-# define KIT_PATH_DELIM_C '\\'
-# define KIT_PATH_DELIM "\\"
-# define KIT_ENV_HOME "USERPROFILE"
-#else
-# define KIT_PATH_DELIM_C '/'
-# define KIT_PATH_DELIM "/"
-# define KIT_ENV_HOME "HOME"
-#endif
+kit_str_builder_t kit_get_env(kit_str_t name, kit_allocator_t *alloc);
kit_str_builder_t kit_path_norm(kit_str_t path,
kit_allocator_t *alloc);
kit_str_builder_t kit_path_join(kit_str_t left, kit_str_t right,
kit_allocator_t *alloc);
kit_str_builder_t kit_path_user(kit_allocator_t *alloc);
kit_str_builder_t kit_path_cache(kit_allocator_t *alloc);
+kit_str_builder_t kit_path_data(kit_allocator_t *alloc);
kit_str_t kit_path_index(kit_str_t path, i64 index);
kit_str_t kit_path_take(kit_str_t path, i64 count);
kit_status_t kit_folder_create(kit_str_t path);
@@ -1649,10 +1694,16 @@ kit_status_t kit_file_unmap(kit_mapped_file_t *mf);
}
#endif
#ifndef KIT_DISABLE_SHORT_NAMES
+# define path_type_t kit_path_type_t
+# define file_info_t kit_file_info_t
+# define path_list_t kit_path_list_t
+# define mapped_file_t kit_mapped_file_t
+# define get_env kit_get_env
# define path_norm kit_path_norm
# define path_join kit_path_join
# define path_user kit_path_user
# define path_cache kit_path_cache
+# define path_data kit_path_data
# define path_index kit_path_index
# define path_take kit_path_take
# define folder_create kit_folder_create
@@ -1660,14 +1711,10 @@ kit_status_t kit_file_unmap(kit_mapped_file_t *mf);
# define file_remove kit_file_remove
# define folder_remove kit_folder_remove
# define file_remove_recursive kit_file_remove_recursive
-# define path_type_t kit_path_type_t
# define path_type kit_path_type
-# define file_info_t kit_file_info_t
# define file_info kit_file_info
-# define path_list_t kit_path_list_t
# define folder_enum kit_folder_enum
# define path_list_destroy kit_path_list_destroy
-# define mapped_file_t kit_mapped_file_t
# define file_map kit_file_map
# define file_sync kit_file_sync
# define file_unmap kit_file_unmap
@@ -1724,7 +1771,7 @@ u64 kit_mt64_generate(kit_mt64_state_t *state);
#ifdef __cplusplus
extern "C" {
#endif
-void kit_secure_random(i64 size, void *data);
+kit_status_t kit_secure_random(i64 size, void *data);
#ifdef __cplusplus
}
#endif
@@ -1813,6 +1860,7 @@ static i32 kit_socket_set_nonblocking(socket_t s) {
# include <sys/socket.h>
# include <sys/types.h>
# include <unistd.h>
+# include <netdb.h>
# define socket_t i32
# define closesocket close
# define INVALID_SOCKET -1
@@ -2986,11 +3034,6 @@ void kit_da_resize_exact(kit_da_void_t *array, i64 element_size,
}
// ================================================================
//
-// File: source/kit/string_builder.c
-//
-// ================================================================
-// ================================================================
-//
// File: source/kit/input_stream.c
//
// ================================================================
@@ -3383,9 +3426,9 @@ enum { PATH_BUF_SIZE = 4096 };
static i32 is_delim(char c) {
return c == '/' || c == '\\';
}
-static kit_str_builder_t kit_get_env_(char *name,
- kit_allocator_t *alloc) {
- char *val = getenv(name);
+kit_str_builder_t kit_get_env(kit_str_t name,
+ kit_allocator_t *alloc) {
+ char *val = getenv(BS(name));
i64 size = val != NULL ? (i64) strlen(val) : 0;
str_builder_t result;
DA_INIT(result, size, alloc);
@@ -3462,7 +3505,7 @@ kit_str_builder_t kit_path_join(kit_str_t left, kit_str_t right,
return joined;
}
kit_str_builder_t kit_path_user(kit_allocator_t *alloc) {
- kit_str_builder_t user = kit_get_env_(KIT_ENV_HOME, alloc);
+ kit_str_builder_t user = kit_get_env(SZ(KIT_ENV_HOME), alloc);
if (user.size == 0) {
DA_RESIZE(user, 1);
if (user.size == 1)
@@ -3472,16 +3515,16 @@ kit_str_builder_t kit_path_user(kit_allocator_t *alloc) {
}
kit_str_builder_t kit_path_cache(kit_allocator_t *alloc) {
kit_str_builder_t cache, user;
-#if defined(_WIN32) && !defined(__CYGWIN__)
- cache = kit_get_env_("LOCALAPPDATA", alloc);
+ cache = kit_get_env(SZ("XDG_CACHE_HOME"), alloc);
if (cache.size != 0)
return cache;
DA_DESTROY(cache);
-#endif
- cache = kit_get_env_("XDG_CACHE_HOME", alloc);
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ cache = kit_get_env(SZ("TEMP"), alloc);
if (cache.size != 0)
return cache;
DA_DESTROY(cache);
+#endif
user = kit_path_user(alloc);
cache =
#ifdef __APPLE__
@@ -3493,6 +3536,29 @@ kit_str_builder_t kit_path_cache(kit_allocator_t *alloc) {
DA_DESTROY(user);
return cache;
}
+kit_str_builder_t kit_path_data(kit_allocator_t *alloc) {
+ kit_str_builder_t data, user;
+ data = kit_get_env(SZ("XDG_DATA_HOME"), alloc);
+ if (data.size != 0)
+ return data;
+ DA_DESTROY(data);
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ data = kit_get_env(SZ("LOCALAPPDATA"), alloc);
+ if (data.size != 0)
+ return data;
+ DA_DESTROY(data);
+#endif
+ user = kit_path_user(alloc);
+ data =
+#ifdef __APPLE__
+ kit_path_join(WRAP_STR(user), SZ("Library"), alloc);
+#else
+ kit_path_join(WRAP_STR(user), SZ(".local" PATH_DELIM "share"),
+ alloc);
+#endif
+ DA_DESTROY(user);
+ return data;
+}
kit_str_t kit_path_index(kit_str_t path, i64 index) {
str_t s = { .size = 0, .values = NULL };
i64 i0 = 0;
@@ -3536,6 +3602,9 @@ kit_str_t kit_path_take(kit_str_t path, i64 count) {
s.size = i;
return s;
}
+// TODO
+// Long path support for Windows
+//
static void kit_prepare_path_(char *buf, kit_str_t path) {
assert(path.size == 0 || path.values != NULL);
assert(path.size + 1 < PATH_BUF_SIZE);
@@ -3958,27 +4027,28 @@ u64 kit_mt64_generate(kit_mt64_state_t *state) {
#else
# include <unistd.h>
#endif
-void kit_secure_random(i64 size, void *data) {
+kit_status_t kit_secure_random(i64 size, void *data) {
assert(size >= 0);
assert(data != NULL);
if (size <= 0 || data == NULL)
- return;
+ return KIT_ERROR_INVALID_ARGUMENT;
#if defined(_WIN32) && !defined(__CYGWIN__)
HCRYPTPROV prov = 0;
if (!CryptAcquireContextW(&prov, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT) ||
!CryptGenRandom(prov, (DWORD) size, (BYTE *) data) ||
!CryptReleaseContext(prov, 0))
- abort();
+ return KIT_ERROR_RESOURCE_UNAVAILABLE;
#else
FILE *f = fopen("/dev/urandom", "rb");
if (f == NULL)
- abort();
+ return KIT_ERROR_RESOURCE_UNAVAILABLE;
i64 n = (i64) fread(data, 1, size, f);
fclose(f);
if (n != size)
- abort();
+ return KIT_ERROR_RESOURCE_UNAVAILABLE;
#endif
+ return KIT_OK;
}
// ================================================================
//
@@ -4170,7 +4240,6 @@ kit_shared_memory_t kit_shared_memory_open(kit_str_t name, i64 size,
close(fd);
if (p == MAP_FAILED) {
shm_unlink(mem._name);
- assert(0);
mem.status = KIT_ERROR_MAP_FAILED;
return mem;
}