2 Commits

Author SHA1 Message Date
abdelrahman 2e5163ba33 Implement functions to get stdin, stdout & stderr
Release / release (push) Successful in 2s
2026-05-17 12:21:23 +01:00
abdelrahman 515493b963 Fix queue MSVC errors 2026-05-17 12:20:47 +01:00
6 changed files with 61 additions and 13 deletions
+25 -2
View File
@@ -30,7 +30,10 @@ GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue,
GenericQueue *output = queue;
u64 capacity = wapp_array_capacity(queue->items);
if (queue->count >= capacity) {
// NOTE (Abdelrahman): Extracted into variable to fix MSVC error
b8 queue_full = queue->count >= capacity;
if (queue_full) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(capacity * 2);
u64 array_size = _array_calc_alloc_size(new_capacity, item_size);
u64 alloc_size = sizeof(GenericQueue) + array_size;
@@ -51,7 +54,27 @@ GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue,
void *copy_boundary = (void *)((uptr)(queue->items) + (queue->front * item_size));
memcpy(output->items, copy_boundary, front_count * item_size);
if (back_count > 0) {
/**
* NOTE (Abdelrahman): Since this is a ring buffer, the elements at the beginning of the array
* aren't always the ones at the front of the queue. When that's the case, the memcpy above
* will only copy a subset of the elements. This is why we need to copy the remaining ones.
*
* Example: Take a queue that looks like this with a capacity of 5 elements
*
* 0 1 | 2 3 4
* ---------------|-----------------------
* | * | * | * | * | * |
* ---------------|-----------------------
* |
* queue_front = 2
* queue_back = 2
*
* In this case, the first memcpy will only copy elements 2-4. The memcpy below will be
* responsible for copying elements 0-1.
*/
b8 items_left_to_copy = back_count > 0;
if (items_left_to_copy) {
void *back_copy_dst = (void *)((uptr)(output->items) + (front_count * item_size));
memcpy(back_copy_dst, queue->items, back_count * item_size);
}
+3 -3
View File
@@ -12,9 +12,6 @@ BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP
typedef struct WFile WFile;
wapp_extern WFile *WF_STDIN;
wapp_extern WFile *WF_STDOUT;
wapp_extern WFile *WF_STDERR;
typedef enum {
WAPP_ACCESS_READ, // Equivalent to r
@@ -37,6 +34,9 @@ typedef enum {
FILE_SEEK_ORIGIN_COUNT,
} FileSeekOrigin;
wapp_extern WFile *wapp_file_stdin(void);
wapp_extern WFile *wapp_file_stdout(void);
wapp_extern WFile *wapp_file_stderr(void);
WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode);
i64 wapp_file_get_current_position(WFile *file);
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
+13 -6
View File
@@ -49,13 +49,20 @@ wapp_intern i32 file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_END] = SEEK_END,
};
wapp_intern WFile _STDIN = { .fd = STDIN_FILENO };
wapp_intern WFile _STDOUT = { .fd = STDOUT_FILENO };
wapp_intern WFile _STDERR = { .fd = STDERR_FILENO };
WFile *wapp_file_stdin(void) {
wapp_persist WFile _stdin = { .fd = STDIN_FILENO };
return &_stdin;
}
WFile *WF_STDIN = &_STDIN;
WFile *WF_STDOUT = &_STDOUT;
WFile *WF_STDERR = &_STDERR;
WFile *wapp_file_stdout(void) {
wapp_persist WFile _stdout = { .fd = STDOUT_FILENO };
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wapp_persist WFile _stderr = { .fd = STDERR_FILENO };
return &_stderr;
}
WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
+18
View File
@@ -54,6 +54,24 @@ wapp_intern DWORD file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_END] = FILE_END,
};
WFile *wapp_file_stdin(void) {
wapp_persist WFile _stdin = { .fh = INVALID_HANDLE_VALUE };
_stdin.fh = GetStdHandle(STD_INPUT_HANDLE);
return &_stdin;
}
WFile *wapp_file_stdout(void) {
wapp_persist WFile _stdout = { .fh = INVALID_HANDLE_VALUE };
_stdout.fh = GetStdHandle(STD_OUTPUT_HANDLE);
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wapp_persist WFile _stderr = { .fh = INVALID_HANDLE_VALUE };
_stderr.fh = GetStdHandle(STD_ERROR_HANDLE);
return &_stderr;
}
WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX);
+1 -1
View File
@@ -47,7 +47,7 @@ TestFuncResult test_queue_push_alloc(void) {
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
for (u64 i = 0; i < remaining; ++i) {
item = remaining + i;
item = (i32)(remaining + i);
wapp_queue_push(i32, &queue, &item);
}
+1 -1
View File
@@ -45,7 +45,7 @@ TestFuncResult test_queue_push_alloc(void) {
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
for (u64 i = 0; i < remaining; ++i) {
item = remaining + i;
item = (i32)(remaining + i);
wapp_queue_push(i32, &queue, &item);
}