Example of code
#if PLATFORM_WINDOWS
#include <windows.h>
#endif
#ifndef _MSC_VER
#include <dirent.h>
#include <unistd.h>
#else
#include <fileapi.h>
#include <stringapiset.h>
uint16_t *win_utf8to16(const char *value UNUSED)
{
#if PLATFORM_WINDOWS
size_t len = strlen(value);
int needed = MultiByteToWideChar(CP_UTF8, 0, value, len + 1, NULL, 0);
if (needed <= 0)
{
error_exit("Failed to convert name '%s'.", value);
}
uint16_t *wide = malloc(needed * sizeof(uint16_t));
if (MultiByteToWideChar(CP_UTF8, 0, value, len + 1, wide, needed) <= 0)
{
error_exit("Failed to convert name '%s'.", value);
}
return wide;
#else
UNREACHABLE
#endif
}
bool dir_make(const char *path)
{
#if (_MSC_VER)
return CreateDirectoryW(win_utf8to16(path), NULL);
#else
return mkdir(path, 0755) == 0;
#endif
}
bool dir_make_recursive(char *path)
{
size_t len = strlen(path);
for (size_t i = len; i > 1; i--)
{
char c = path[i];
if (c == '\\' || c == '/')
{
path[i] = '\0';
dir_make_recursive(path);
path[i] = c;
break;
}
}
return dir_make(path);
}
FILE *file_open_read(const char *path)
{
#if (_MSC_VER)
return _wfopen(win_utf8to16(path), L"rb");
#else
return fopen(path, "rb");
#endif
}
const char *find_lib_dir(void)
{
char *lib_dir_env = getenv("C3C_LIB");
if (lib_dir_env && strlen(lib_dir_env) > 0)
{
INFO_LOG("Using stdlib library from env 'C3C_LIB': %s.", lib_dir_env);
if (!file_exists(lib_dir_env))
{
error_exit("Library path from 'C3C_LIB' environment variable: '%s', could not be resolved.", lib_dir_env);
}
return strdup(lib_dir_env);
}
char *path = find_executable_path();
INFO_LOG("Detected executable path at %s", path);
size_t strlen_path = strlen(path);
// Remove any last path slash
if (strlen_path > 1 && (path[strlen_path - 1] == '/' || path[strlen_path - 1] == '\\'))
{
path[strlen_path - 1] = '\0';
}
const char *lib_path = NULL;
if ((lib_path = lib_find(path, "/../lib/c3/"))) goto DONE;
if ((lib_path = lib_find(path, "/../lib/"))) goto DONE;
if ((lib_path = lib_find(path, "/lib/c3/"))) goto DONE;
if ((lib_path = lib_find(path, "/lib/"))) goto DONE;
if ((lib_path = lib_find(path, "/c3/"))) goto DONE;
if ((lib_path = lib_find(path, "/"))) goto DONE;
if ((lib_path = lib_find(path, "/../c3/"))) goto DONE;
if ((lib_path = lib_find(path, "/../"))) goto DONE;
if ((lib_path = lib_find(path, "/../../lib/c3/"))) goto DONE;
if ((lib_path = lib_find(path, "/../../lib/"))) goto DONE;
INFO_LOG("Could not find the standard library /lib/std/");
DONE:;
free(path);
return lib_path;
}