char* bh_path_get_full_name(char const* filename, bh_allocator a);
char* bh_path_get_parent(char const* filename, bh_allocator a);
+// This function returns a volatile pointer. Do not store it without copying!
+// `included_folders` is bh_arr(const char *).
+char* bh_lookup_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, const char ** included_folders, b32 search_included_folders);
+
#define bh_file_read_contents(allocator_, x) _Generic((x), \
bh_file*: bh_file_read_contents_bh_file, \
const char*: bh_file_read_contents_direct, \
}
// NOTE: This assumes the filename is the full path, not relative to anything else.
-char* bh_path_get_parent(char const* filename, bh_allocator a) {
#if defined(_BH_LINUX)
#define DIR_SEPARATOR '/'
#elif defined(_BH_WINDOWS)
#define DIR_SEPARATOR '\\'
#endif
+char* bh_path_get_parent(char const* filename, bh_allocator a) {
char* result = bh_strdup(a, (char *) filename);
char* end = result + strlen(result);
while (*end != DIR_SEPARATOR && end != result) *end-- = '\0';
return result;
+}
-#undef DIR_SEPARATOR
+// This function returns a volatile pointer. Do not store it without copying!
+char* bh_lookup_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, bh_arr(const char *) included_folders, b32 search_included_folders) {
+ assert(relative_to != NULL);
+
+ static char path[512];
+ fori (i, 0, 512) path[i] = 0;
+
+ static char fn[256];
+ fori (i, 0, 256) fn[i] = 0;
+
+ if (!bh_str_ends_with(filename, suffix) && add_suffix) {
+ bh_snprintf(fn, 256, "%s%s", filename, suffix);
+ } else {
+ bh_snprintf(fn, 256, "%s", filename);
+ }
+
+ fori (i, 0, 256) if (fn[i] == '/') fn[i] = DIR_SEPARATOR;
+
+ if (bh_str_starts_with(filename, "./")) {
+ if (relative_to[strlen(relative_to) - 1] != DIR_SEPARATOR)
+ bh_snprintf(path, 512, "%s%c%s", relative_to, DIR_SEPARATOR, fn + 2);
+ else
+ bh_snprintf(path, 512, "%s%s", relative_to, fn + 2);
+
+ if (bh_file_exists(path)) return bh_path_get_full_name(path, bh_heap_allocator());
+
+ return fn;
+ }
+
+ if (search_included_folders) {
+ bh_arr_each(const char *, folder, included_folders) {
+ if ((*folder)[strlen(*folder) - 1] != DIR_SEPARATOR)
+ bh_snprintf(path, 512, "%s%c%s", *folder, DIR_SEPARATOR, fn);
+ else
+ bh_snprintf(path, 512, "%s%s", *folder, fn);
+
+ if (bh_file_exists(path)) return bh_path_get_full_name(path, bh_heap_allocator());
+ }
+ }
+
+ return fn;
}
+#undef DIR_SEPARATOR
+
#endif // ifndef BH_NO_FILE
// Returns the length after processing the string.
i32 string_process_escape_seqs(char* dest, char* src, i32 len);
-// NOTE: This should not be called until immediately before using the return value.
-// This function can return a static variable which will change if this is called
-// another time. -brendanfh 2020/10/09
-// :RelativeFiles This should lookup for the file relative to "relative_to"
-char* lookup_included_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, bh_arr(const char *) included_folders, b32 search_included_folders);
-
u32 levenshtein_distance(const char *str1, const char *str2);
char *find_closest_symbol_in_node(AstNode *node, char *sym);
char* parent_folder = bh_path_get_parent(parent_file, global_scratch_allocator);
- char* filename = lookup_included_file(include->name, parent_folder, ".onyx", 1, context.options->included_folders, 1);
+ char* filename = bh_lookup_file(include->name, parent_folder, ".onyx", 1, context.options->included_folders, 1);
char* formatted_name = bh_strdup(global_heap_allocator, filename);
process_source_file(formatted_name, include->token->pos);
return total_len;
}
-char* lookup_included_file(char* filename, char* relative_to, char *suffix, b32 add_suffix, bh_arr(const char *) included_folders, b32 search_included_folders) {
- assert(relative_to != NULL);
-
- static char path[256];
- fori (i, 0, 256) path[i] = 0;
-
- static char fn[128];
- fori (i, 0, 128) fn[i] = 0;
-
- if (!bh_str_ends_with(filename, suffix) && add_suffix) {
- bh_snprintf(fn, 128, "%s%s", filename, suffix);
- } else {
- bh_snprintf(fn, 128, "%s", filename);
- }
-
-#if defined(_BH_LINUX)
- #define DIR_SEPARATOR '/'
-#elif defined(_BH_WINDOWS)
- #define DIR_SEPARATOR '\\'
-#endif
-
- fori (i, 0, 128) if (fn[i] == '/') fn[i] = DIR_SEPARATOR;
-
- if (bh_str_starts_with(filename, "./")) {
- if (relative_to[strlen(relative_to) - 1] != DIR_SEPARATOR)
- bh_snprintf(path, 256, "%s%c%s", relative_to, DIR_SEPARATOR, fn + 2);
- else
- bh_snprintf(path, 256, "%s%s", relative_to, fn + 2);
-
- if (bh_file_exists(path)) return bh_path_get_full_name(path, global_scratch_allocator);
-
- return fn;
- }
-
- if (search_included_folders) {
- bh_arr_each(const char *, folder, included_folders) {
- if ((*folder)[strlen(*folder) - 1] != DIR_SEPARATOR)
- bh_snprintf(path, 256, "%s%c%s", *folder, DIR_SEPARATOR, fn);
- else
- bh_snprintf(path, 256, "%s%s", *folder, fn);
-
- if (bh_file_exists(path)) return bh_path_get_full_name(path, global_scratch_allocator);
- }
- }
-
- return fn;
-
-#undef DIR_SEPARATOR
-}
-
u32 levenshtein_distance(const char *str1, const char *str2) {
i32 m = strlen(str1) + 1;
i32 n = strlen(str2) + 1;
char* temp_fn = bh_alloc_array(global_scratch_allocator, char, fc->filename_token->length);
i32 temp_fn_len = string_process_escape_seqs(temp_fn, fc->filename_token->text, fc->filename_token->length);
- char* filename = lookup_included_file(temp_fn, parent_folder, "", 0, NULL, 0);
+ char* filename = bh_lookup_file(temp_fn, parent_folder, "", 0, NULL, 0);
fc->filename = bh_strdup(global_heap_allocator, filename);
}
}
char *library_load_name_tmp = bh_bprintf("onyx_library_%s", library);
- char *library_load_name = alloca(strlen(library_load_name_tmp));
+ char *library_load_name = alloca(strlen(library_load_name_tmp) + 1);
strcpy(library_load_name, library_load_name_tmp);
LibraryLinker library_load;
#ifdef _BH_LINUX
- char *library_name = lookup_included_file(name, ".", ".so", 1, (const char **) library_paths, 1);
+ char *library_name = bh_lookup_file(name, ".", ".so", 1, (const char **) library_paths, 1);
void* handle = dlopen(library_name, RTLD_LAZY);
if (handle == NULL) {
printf("ERROR LOADING LIBRARY %s: %s\n", name, dlerror());
#endif
#ifdef _BH_WINDOWS
- char *library_name = lookup_included_file(name, ".", ".dll", 1, (const char **) library_paths, 1);
+ char *library_name = bh_lookup_file(name, ".", ".dll", 1, (const char **) library_paths, 1);
HMODULE handle = LoadLibraryA(library_name);
if (handle == NULL) {
printf("ERROR LOADING LIBRARY %s: %d\n", name, GetLastError());