aboutsummaryrefslogtreecommitdiff
path: root/src/common.cpp
diff options
context:
space:
mode:
authorGinger Bill <bill@gingerbill.org>2016-09-30 23:34:32 +0100
committerGinger Bill <bill@gingerbill.org>2016-09-30 23:34:32 +0100
commit17ab23f1f06ed11602c883dd5fce406fd10637db (patch)
tree821059c168ebb59f853a86f77d471128c1c14138 /src/common.cpp
parentc6aac264fa8001ff5e55e5ac6f56289ff0a755ee (diff)
Const Aggregate Literals for IR; Module path fix
Diffstat (limited to 'src/common.cpp')
-rw-r--r--src/common.cpp33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/common.cpp b/src/common.cpp
index f5fe5b50c..73f11b172 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -4,20 +4,39 @@
#include "string.cpp"
-String get_module_dir(gbAllocator a) {
- isize len = GetModuleFileNameW(NULL, NULL, 0);
- if (len == 0) {
- return make_string(NULL, 0);
+gb_global String global_module_path = {};
+gb_global b32 global_module_path_set = false;
+
+String get_module_dir() {
+ if (global_module_path_set) {
+ return global_module_path;
}
+
+ gbArray(wchar_t) path_buf;
+ gb_array_init_reserve(path_buf, gb_heap_allocator(), 300);
+ defer (gb_array_free(path_buf));
+ gb_array_resize(path_buf, 300);
+
+ isize len = 0;
+ for (;;) {
+ len = GetModuleFileNameW(NULL, path_buf, gb_array_count(path_buf));
+ if (len == 0) {
+ return make_string(NULL, 0);
+ }
+ if (len < gb_array_count(path_buf)) {
+ break;
+ }
+ gb_array_resize(path_buf, 2*gb_array_count(path_buf) + 300);
+ }
+
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&string_buffer_arena);
defer (gb_temp_arena_memory_end(tmp));
-
wchar_t *text = gb_alloc_array(string_buffer_allocator, wchar_t, len+1);
String16 str = {text, len};
GetModuleFileNameW(NULL, text, len);
- String path = string16_to_string(a, str);
+ String path = string16_to_string(gb_heap_allocator(), str);
for (isize i = path.len-1; i >= 0; i--) {
u8 c = path.text[i];
if (c == '/' || c == '\\') {
@@ -26,6 +45,8 @@ String get_module_dir(gbAllocator a) {
path.len--;
}
+ global_module_path = path;
+ global_module_path_set = true;
return path;
}