authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-06-15 18:38:41+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-06-15 18:38:41+03:00
logc34bdff4bbba88feefeca6491730526b2cc9f72a
treefea6355cce825a0dafb9e600581e01fca42d5e89
parent3b0b56b81a26d9ba4d42812843cf2e9407d6d2bc

Use more wide functions on windows


2 files changed, 20 insertions(+), 19 deletions(-)

src/os.cpp+17-18
......@@ -11,6 +11,7 @@
1111#include "error.hpp"
1212#include "util_base.hpp"
1313#include <stdint.h>
14#include <stdio.h>
1415
1516#if defined(_WIN32)
1617
......@@ -76,6 +77,7 @@ typedef SSIZE_T ssize_t;
7677#endif
7778
7879#if defined(ZIG_OS_WINDOWS)
80static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le);
7981static uint64_t windows_perf_freq;
8082#elif defined(__MACH__)
8183static clock_serv_t macos_calendar_clock;
......@@ -272,11 +274,13 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
272274
273275Error os_path_real(Buf *rel_path, Buf *out_abs_path) {
274276#if defined(ZIG_OS_WINDOWS)
275 buf_resize(out_abs_path, 4096);
276 if (_fullpath(buf_ptr(out_abs_path), buf_ptr(rel_path), buf_len(out_abs_path)) == nullptr) {
277 zig_panic("_fullpath failed");
277 PathSpace rel_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(rel_path), buf_len(rel_path) });
278 PathSpace out_abs_path_space;
279
280 if (_wfullpath(&out_abs_path_space.data.items[0], &rel_path_space.data.items[0], PATH_MAX_WIDE) == nullptr) {
281 zig_panic("_wfullpath failed");
278282 }
279 buf_resize(out_abs_path, strlen(buf_ptr(out_abs_path)));
283 utf16le_ptr_to_utf8(out_abs_path, &out_abs_path_space.data.items[0]);
280284 return ErrorNone;
281285#elif defined(ZIG_OS_POSIX)
282286 buf_resize(out_abs_path, PATH_MAX + 1);
......@@ -1234,11 +1238,11 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents) {
12341238
12351239Error os_get_cwd(Buf *out_cwd) {
12361240#if defined(ZIG_OS_WINDOWS)
1237 char buf[4096];
1238 if (GetCurrentDirectory(4096, buf) == 0) {
1241 PathSpace path_space;
1242 if (GetCurrentDirectoryW(PATH_MAX_WIDE, &path_space.data.items[0]) == 0) {
12391243 zig_panic("GetCurrentDirectory failed");
12401244 }
1241 buf_init_from_str(out_cwd, buf);
1245 utf16le_ptr_to_utf8(out_cwd, &path_space.data.items[0]);
12421246 return ErrorNone;
12431247#elif defined(ZIG_OS_POSIX)
12441248 char buf[PATH_MAX];
......@@ -1538,18 +1542,13 @@ int os_init(void) {
15381542
15391543Error os_self_exe_path(Buf *out_path) {
15401544#if defined(ZIG_OS_WINDOWS)
1541 buf_resize(out_path, 256);
1542 for (;;) {
1543 DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path));
1544 if (copied_amt <= 0) {
1545 return ErrorFileNotFound;
1546 }
1547 if (copied_amt < buf_len(out_path)) {
1548 buf_resize(out_path, copied_amt);
1549 return ErrorNone;
1550 }
1551 buf_resize(out_path, buf_len(out_path) * 2);
1545 PathSpace path_space;
1546 DWORD copied_amt = GetModuleFileNameW(nullptr, &path_space.data.items[0], PATH_MAX_WIDE);
1547 if (copied_amt <= 0) {
1548 return ErrorFileNotFound;
15521549 }
1550 utf16le_ptr_to_utf8(out_path, &path_space.data.items[0]);
1551 return ErrorNone;
15531552
15541553#elif defined(ZIG_OS_DARWIN)
15551554 // How long is the executable's path?
src/os.hpp+3-1
......@@ -155,8 +155,10 @@ Error ATTRIBUTE_MUST_USE os_get_app_data_dir(Buf *out_path, const char *appname)
155155
156156Error ATTRIBUTE_MUST_USE os_self_exe_shared_libs(ZigList<Buf *> &paths);
157157
158const size_t PATH_MAX_WIDE = 32767;
159
158160struct PathSpace {
159 Array<wchar_t, 32767> data;
161 Array<wchar_t, PATH_MAX_WIDE> data;
160162 size_t len;
161163};
162164