Fix MSVC errors and warnings

This commit is contained in:
Abdelrahman Said 2024-10-05 20:07:48 +01:00
parent 1ddc5610f9
commit ffb99a3644
9 changed files with 77 additions and 51 deletions

View File

@ -10,13 +10,10 @@
#define CMD_BUF_LEN 8192
#define OUT_BUF_LEN 4096
internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
va_list args);
internal inline CMDResult execute_command(const char *cmd,
CMDOutHandling out_handling,
internal inline CMDError build_command_from_args(char *cmd, u64 buf_len, va_list args);
internal inline CMDResult execute_command(const char *cmd, CMDOutHandling out_handling,
char *out_buf, u64 buf_size);
internal inline CMDError get_command_output(FILE *fp,
CMDOutHandling out_handling,
internal inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling,
char *out_buf, u64 buf_size);
internal inline CMDError get_output_status(FILE *fp, i32 *status_out);
@ -42,8 +39,8 @@ internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
u64 size = 0;
u64 arg_len = 0;
const char *arg;
while ((arg = va_arg(args, const char *))) {
const char *arg = va_arg(args, const char *);
while (arg) {
arg_len = strlen(arg);
if (arg_len >= buf_len - size) {
return SHELL_ERR_CMD_BUF_FULL;
@ -53,6 +50,8 @@ internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
cmd[size + arg_len] = ' ';
size += arg_len + 1;
arg = va_arg(args, const char *);
}
return SHELL_ERR_NO_ERROR;
@ -66,24 +65,35 @@ internal inline CMDResult execute_command(const char *cmd,
return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL);
}
CMDResult output;
CMDError err = get_command_output(fp, out_handling, out_buf, buf_size);
if (err > SHELL_ERR_NO_ERROR) {
// Ensure process is closed on failure
wapp_shell_utils_pclose(fp);
return CMD_NO_EXIT(err);
output = CMD_NO_EXIT(err);
goto EXECUTE_COMMAND_CLOSE;
}
i32 st = EXIT_SUCCESS;
err = get_output_status(fp, &st);
if (err > SHELL_ERR_NO_ERROR) {
return CMD_NO_EXIT(err);
output = CMD_NO_EXIT(err);
goto EXECUTE_COMMAND_CLOSE;
}
return (CMDResult){
// Process is already closed in get_output_status
fp = NULL;
output = (CMDResult){
.exited = true,
.exit_code = st,
.error = SHELL_ERR_NO_ERROR,
};
EXECUTE_COMMAND_CLOSE:
if (fp) {
wapp_shell_utils_pclose(fp);
}
return output;
}
internal inline CMDError get_command_output(FILE *fp,
@ -93,7 +103,7 @@ internal inline CMDError get_command_output(FILE *fp,
u64 max_out_length = OUT_BUF_LEN - 1;
u64 buf_filled = 0;
while (fgets(out, max_out_length, fp)) {
while (fgets(out, (i32)max_out_length, fp)) {
if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
buf_filled += strlen(out);
if (buf_filled >= buf_size) {

View File

@ -2,6 +2,7 @@
#define COMMANDER_H
#include "aliases.h"
#include "platform.h"
#include <stdbool.h>
#include <stdlib.h>
@ -30,9 +31,14 @@ typedef enum {
typedef struct commander_result CMDResult;
struct commander_result {
bool exited;
int exit_code;
i32 exit_code;
CMDError error;
bool exited;
#ifdef WAPP_PLATFORM_WINDOWS
#include "misc_utils.h"
wapp_misc_utils_padding_size(sizeof(bool) + sizeof(i32) + sizeof(CMDError));
#endif // !WAPP_PLATFORM_WINDOWS
};
// clang-format off

View File

@ -20,7 +20,7 @@ struct termcolour_data {
wapp_misc_utils_padding_size(sizeof(HANDLE) + sizeof(WORD) + sizeof(WORD));
};
internal TermcolourData init_data(void);
internal void init_data(TermcolourData *data);
internal WORD colours[COUNT_TERM_COLOUR] = {
[WAPP_TERM_COLOUR_FG_BLACK] = 0,
@ -42,31 +42,30 @@ internal WORD colours[COUNT_TERM_COLOUR] = {
};
void print_coloured_text(const char *text, TerminalColour colour) {
persistent TermcolourData data = init_data();
if (color == WAPP_TERM_COLOUR_CLEAR) {
data->current_colour = data->default_colour;
} else {
data->current_colour = colours[colour];
persistent TermcolourData data = {0};
if (data.handle == 0) {
init_data(&data);
}
SetConsoleTextAttribute(data->handle, data->current_colour);
if (colour == WAPP_TERM_COLOUR_CLEAR) {
data.current_colour = data.default_colour;
} else {
data.current_colour = colours[colour];
}
SetConsoleTextAttribute(data.handle, data.current_colour);
printf("%s", text);
}
internal TermcolourData init_data(void) {
TermcolourData data = {0};
internal void init_data(TermcolourData *data) {
// create handle
data.handle = GetStdHandle(STD_OUTPUT_HANDLE);
data->handle = GetStdHandle(STD_OUTPUT_HANDLE);
// get console colour information
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(data.handle, &csbi);
data.default_colour = csbi.wAttributes;
data.current_colour = data.default_colour;
return data;
GetConsoleScreenBufferInfo(data->handle, &csbi);
data->default_colour = csbi.wAttributes;
data->current_colour = data->default_colour;
}
#endif // !WAPP_PLATFORM_WINDOWS

View File

@ -2,15 +2,14 @@
#define SHELL_UTILS_H
#include "platform.h"
#include <stdio.h>
// clang-format off
#ifdef WAPP_PLATFORM_WINDOWS
#define wapp_shell_utils_popen _popen
#define wapp_shell_utils_popen _popen
#define wapp_shell_utils_pclose _pclose
#else
#define wapp_shell_utils_popen popen
#define wapp_shell_utils_popen popen
#define wapp_shell_utils_pclose pclose
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
// clang-format on
#endif // !SHELL_UTILS_H

View File

@ -1,6 +1,7 @@
#include "mem_arena.h"
#include "aliases.h"
#include "mem_utils.h"
#include "misc_utils.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

View File

@ -24,12 +24,15 @@ Allocator wapp_mem_libc_allocator(void) {
}
internal inline void *mem_libc_alloc(u64 size, void *alloc_obj) {
(void)alloc_obj; // Silence unused warnings
return calloc(1, size);
}
#if 0
internal inline void *mem_libc_alloc_aligned(u64 size, u64 alignment,
void *alloc_obj) {
(void)alloc_obj; // Silence unused warnings
void *output = aligned_alloc(alignment, size);
if (output) {
memset(output, 0, size);
@ -40,10 +43,16 @@ internal inline void *mem_libc_alloc_aligned(u64 size, u64 alignment,
#endif
internal inline void *mem_libc_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj) {
// Silence unused warnings
(void)alloc_obj;
(void)new_size;
return realloc(ptr, old_size);
}
internal inline void mem_libc_free(void **ptr, void *alloc_obj) {
(void)alloc_obj; // Silence unused warnings
if (!ptr || !(*ptr)) {
return;
}

View File

@ -26,7 +26,7 @@ TestFuncResult test_arena_allocator(void) {
Allocator allocator = wapp_mem_arena_allocator_init(4096);
bool result = allocator.obj != NULL && allocator.alloc != NULL &&
allocator.alloc_aligned != NULL &&
allocator.realloc != NULL & allocator.realloc_aligned != NULL &&
allocator.realloc != NULL && allocator.realloc_aligned != NULL &&
allocator.free == NULL;
void *ptr = wapp_mem_allocator_alloc(&allocator, 20);
result = result && (ptr != NULL);

View File

@ -48,21 +48,21 @@ TestFuncResult test_arena_alloc_fails_when_over_capacity(void) {
}
TestFuncResult test_arena_realloc_bigger_size(void) {
u64 count = 10;
u64 old_count = 10;
u64 new_count = 20;
i32 *bytes = wapp_mem_arena_alloc(arena, count * sizeof(i32));
i32 *bytes = wapp_mem_arena_alloc(arena, old_count * sizeof(i32));
for (u64 i = 0; i < count; ++i) {
bytes[i] = i;
for (u64 i = 0; i < old_count; ++i) {
bytes[i] = (i32)i;
}
i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, count * sizeof(i32), new_count * sizeof(i32));
i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32));
if (!new_bytes) {
return wapp_tester_result(false);
}
for (u64 i = 0; i < new_count; ++i) {
if (i < count && new_bytes[i] != bytes[i]) {
if (i < old_count && new_bytes[i] != bytes[i]) {
return wapp_tester_result(false);
}
}
@ -71,15 +71,15 @@ TestFuncResult test_arena_realloc_bigger_size(void) {
}
TestFuncResult test_arena_realloc_smaller_size(void) {
u64 count = 10;
u64 old_count = 10;
u64 new_count = 5;
i32 *bytes = wapp_mem_arena_alloc(arena, count * sizeof(i32));
i32 *bytes = wapp_mem_arena_alloc(arena, old_count * sizeof(i32));
for (u64 i = 0; i < count; ++i) {
bytes[i] = i;
for (u64 i = 0; i < old_count; ++i) {
bytes[i] = (i32)i;
}
i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, count * sizeof(i32), new_count * sizeof(i32));
i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32));
if (!new_bytes) {
return wapp_tester_result(false);
}

View File

@ -26,12 +26,13 @@ TestFuncResult test_commander_cmd_out_buf_success(void) {
char buf[64] = {0};
char expected_output[64] = {0};
const char *msg = "hello world";
u64 length = strlen(msg);
sprintf(expected_output, "%s\n", msg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg);
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR &&
strcmp(buf, expected_output) == 0;
strncmp(buf, expected_output, length) == 0;
return wapp_tester_result(succeeded);
}
@ -39,9 +40,10 @@ TestFuncResult test_commander_cmd_out_buf_success(void) {
TestFuncResult test_commander_cmd_out_buf_failure(void) {
char buf[4] = {0};
const char *msg = "hello world";
u64 length = strlen(msg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 4, "echo", msg);
bool failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && strcmp(buf, msg) != 0;
result.error == SHELL_ERR_OUT_BUF_FULL && strncmp(buf, msg, length) != 0;
return wapp_tester_result(failed);
}