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 @@...@@ -11,6 +11,7 @@
11#include "error.hpp"11#include "error.hpp"
12#include "util_base.hpp"12#include "util_base.hpp"
13#include <stdint.h>13#include <stdint.h>
14#include <stdio.h>
1415
15#if defined(_WIN32)16#if defined(_WIN32)
1617
...@@ -76,6 +77,7 @@ typedef SSIZE_T ssize_t;...@@ -76,6 +77,7 @@ typedef SSIZE_T ssize_t;
76#endif77#endif
7778
78#if defined(ZIG_OS_WINDOWS)79#if defined(ZIG_OS_WINDOWS)
80static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le);
79static uint64_t windows_perf_freq;81static uint64_t windows_perf_freq;
80#elif defined(__MACH__)82#elif defined(__MACH__)
81static clock_serv_t macos_calendar_clock;83static clock_serv_t macos_calendar_clock;
...@@ -272,11 +274,13 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {...@@ -272,11 +274,13 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
272274
273Error os_path_real(Buf *rel_path, Buf *out_abs_path) {275Error os_path_real(Buf *rel_path, Buf *out_abs_path) {
274#if defined(ZIG_OS_WINDOWS)276#if defined(ZIG_OS_WINDOWS)
275 buf_resize(out_abs_path, 4096);277 PathSpace rel_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(rel_path), buf_len(rel_path) });
276 if (_fullpath(buf_ptr(out_abs_path), buf_ptr(rel_path), buf_len(out_abs_path)) == nullptr) {278 PathSpace out_abs_path_space;
277 zig_panic("_fullpath failed");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");
278 }282 }
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]);
280 return ErrorNone;284 return ErrorNone;
281#elif defined(ZIG_OS_POSIX)285#elif defined(ZIG_OS_POSIX)
282 buf_resize(out_abs_path, PATH_MAX + 1);286 buf_resize(out_abs_path, PATH_MAX + 1);
...@@ -1234,11 +1238,11 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents) {...@@ -1234,11 +1238,11 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents) {
12341238
1235Error os_get_cwd(Buf *out_cwd) {1239Error os_get_cwd(Buf *out_cwd) {
1236#if defined(ZIG_OS_WINDOWS)1240#if defined(ZIG_OS_WINDOWS)
1237 char buf[4096];1241 PathSpace path_space;
1238 if (GetCurrentDirectory(4096, buf) == 0) {1242 if (GetCurrentDirectoryW(PATH_MAX_WIDE, &path_space.data.items[0]) == 0) {
1239 zig_panic("GetCurrentDirectory failed");1243 zig_panic("GetCurrentDirectory failed");
1240 }1244 }
1241 buf_init_from_str(out_cwd, buf);1245 utf16le_ptr_to_utf8(out_cwd, &path_space.data.items[0]);
1242 return ErrorNone;1246 return ErrorNone;
1243#elif defined(ZIG_OS_POSIX)1247#elif defined(ZIG_OS_POSIX)
1244 char buf[PATH_MAX];1248 char buf[PATH_MAX];
...@@ -1538,18 +1542,13 @@ int os_init(void) {...@@ -1538,18 +1542,13 @@ int os_init(void) {
15381542
1539Error os_self_exe_path(Buf *out_path) {1543Error os_self_exe_path(Buf *out_path) {
1540#if defined(ZIG_OS_WINDOWS)1544#if defined(ZIG_OS_WINDOWS)
1541 buf_resize(out_path, 256);1545 PathSpace path_space;
1542 for (;;) {1546 DWORD copied_amt = GetModuleFileNameW(nullptr, &path_space.data.items[0], PATH_MAX_WIDE);
1543 DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path));1547 if (copied_amt <= 0) {
1544 if (copied_amt <= 0) {1548 return ErrorFileNotFound;
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);
1552 }1549 }
1550 utf16le_ptr_to_utf8(out_path, &path_space.data.items[0]);
1551 return ErrorNone;
15531552
1554#elif defined(ZIG_OS_DARWIN)1553#elif defined(ZIG_OS_DARWIN)
1555 // How long is the executable's path?1554 // 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)...@@ -155,8 +155,10 @@ Error ATTRIBUTE_MUST_USE os_get_app_data_dir(Buf *out_path, const char *appname)
155155
156Error ATTRIBUTE_MUST_USE os_self_exe_shared_libs(ZigList<Buf *> &paths);156Error ATTRIBUTE_MUST_USE os_self_exe_shared_libs(ZigList<Buf *> &paths);
157157
158const size_t PATH_MAX_WIDE = 32767;
159
158struct PathSpace {160struct PathSpace {
159 Array<wchar_t, 32767> data;161 Array<wchar_t, PATH_MAX_WIDE> data;
160 size_t len;162 size_t len;
161};163};
162164