authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-06-15 21:51:53+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-06-15 21:51:53+03:00
log242246f79312f3106290b9b1aea6dfd5a18368b0
treef8110edf645ab8c5c5105a9126dbbe11aa018591
parentc34bdff4bbba88feefeca6491730526b2cc9f72a

UTF16 create process, utf8->utf16 fix


1 files changed, 32 insertions(+), 28 deletions(-)

src/os.cpp+32-28
...@@ -7,6 +7,7 @@...@@ -7,6 +7,7 @@
77
8#include "os.hpp"8#include "os.hpp"
9#include "buffer.hpp"9#include "buffer.hpp"
10#include "heap.hpp"
10#include "util.hpp"11#include "util.hpp"
11#include "error.hpp"12#include "error.hpp"
12#include "util_base.hpp"13#include "util_base.hpp"
...@@ -78,6 +79,7 @@ typedef SSIZE_T ssize_t;...@@ -78,6 +79,7 @@ typedef SSIZE_T ssize_t;
7879
79#if defined(ZIG_OS_WINDOWS)80#if defined(ZIG_OS_WINDOWS)
80static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le);81static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le);
82static size_t utf8_to_utf16le(WCHAR *utf16_le, Slice<uint8_t> utf8);
81static uint64_t windows_perf_freq;83static uint64_t windows_perf_freq;
82#elif defined(__MACH__)84#elif defined(__MACH__)
83static clock_serv_t macos_calendar_clock;85static clock_serv_t macos_calendar_clock;
...@@ -153,15 +155,21 @@ static void os_spawn_process_windows(ZigList<const char *> &args, Termination *t...@@ -153,15 +155,21 @@ static void os_spawn_process_windows(ZigList<const char *> &args, Termination *t
153 os_windows_create_command_line(&command_line, args);155 os_windows_create_command_line(&command_line, args);
154156
155 PROCESS_INFORMATION piProcInfo = {0};157 PROCESS_INFORMATION piProcInfo = {0};
156 STARTUPINFO siStartInfo = {0};158 STARTUPINFOW siStartInfo = {0};
157 siStartInfo.cb = sizeof(STARTUPINFO);159 siStartInfo.cb = sizeof(STARTUPINFOW);
158160
159 const char *exe = args.at(0);161 Slice<uint8_t> exe_slice = str(args.at(0));
160 BOOL success = CreateProcessA(exe, buf_ptr(&command_line), nullptr, nullptr, TRUE, 0, nullptr, nullptr,162 auto exe_utf16_slice = Slice<WCHAR>::alloc(exe_slice.len + 1);
163 exe_utf16_slice.ptr[utf8_to_utf16le(exe_utf16_slice.ptr, exe_slice)] = 0;
164
165 auto command_line_utf16 = Slice<WCHAR>::alloc(buf_len(&command_line) + 1);
166 command_line_utf16.ptr[utf8_to_utf16le(command_line_utf16.ptr, buf_to_slice(&command_line))] = 0;
167
168 BOOL success = CreateProcessW(exe_utf16_slice.ptr, command_line_utf16.ptr, nullptr, nullptr, TRUE, CREATE_UNICODE_ENVIRONMENT, nullptr, nullptr,
161 &siStartInfo, &piProcInfo);169 &siStartInfo, &piProcInfo);
162170
163 if (!success) {171 if (!success) {
164 zig_panic("CreateProcess failed. exe: %s command_line: %s", exe, buf_ptr(&command_line));172 zig_panic("CreateProcess failed. exe: %s command_line: %s", args.at(0), buf_ptr(&command_line));
165 }173 }
166174
167 WaitForSingleObject(piProcInfo.hProcess, INFINITE);175 WaitForSingleObject(piProcInfo.hProcess, INFINITE);
...@@ -274,7 +282,7 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {...@@ -274,7 +282,7 @@ void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
274282
275Error os_path_real(Buf *rel_path, Buf *out_abs_path) {283Error os_path_real(Buf *rel_path, Buf *out_abs_path) {
276#if defined(ZIG_OS_WINDOWS)284#if defined(ZIG_OS_WINDOWS)
277 PathSpace rel_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(rel_path), buf_len(rel_path) });285 PathSpace rel_path_space = slice_to_prefixed_file_w(buf_to_slice(rel_path));
278 PathSpace out_abs_path_space;286 PathSpace out_abs_path_space;
279287
280 if (_wfullpath(&out_abs_path_space.data.items[0], &rel_path_space.data.items[0], PATH_MAX_WIDE) == nullptr) {288 if (_wfullpath(&out_abs_path_space.data.items[0], &rel_path_space.data.items[0], PATH_MAX_WIDE) == nullptr) {
...@@ -780,7 +788,7 @@ Error os_fetch_file(FILE *f, Buf *out_buf) {...@@ -780,7 +788,7 @@ Error os_fetch_file(FILE *f, Buf *out_buf) {
780788
781Error os_file_exists(Buf *full_path, bool *result) {789Error os_file_exists(Buf *full_path, bool *result) {
782#if defined(ZIG_OS_WINDOWS)790#if defined(ZIG_OS_WINDOWS)
783 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(full_path), buf_len(full_path) });791 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(full_path));
784 *result = GetFileAttributesW(&path_space.data.items[0]) != INVALID_FILE_ATTRIBUTES;792 *result = GetFileAttributesW(&path_space.data.items[0]) != INVALID_FILE_ATTRIBUTES;
785 return ErrorNone;793 return ErrorNone;
786#else794#else
...@@ -1338,8 +1346,8 @@ Error os_rename(Buf *src_path, Buf *dest_path) {...@@ -1338,8 +1346,8 @@ Error os_rename(Buf *src_path, Buf *dest_path) {
1338 return ErrorNone;1346 return ErrorNone;
1339 }1347 }
1340#if defined(ZIG_OS_WINDOWS)1348#if defined(ZIG_OS_WINDOWS)
1341 PathSpace src_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(src_path), buf_len(src_path) });1349 PathSpace src_path_space = slice_to_prefixed_file_w(buf_to_slice(src_path));
1342 PathSpace dest_path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(dest_path), buf_len(dest_path) });1350 PathSpace dest_path_space = slice_to_prefixed_file_w(buf_to_slice(dest_path));
1343 if (!MoveFileExW(&src_path_space.data.items[0], &dest_path_space.data.items[0], MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {1351 if (!MoveFileExW(&src_path_space.data.items[0], &dest_path_space.data.items[0], MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
1344 return ErrorFileSystem;1352 return ErrorFileSystem;
1345 }1353 }
...@@ -1436,7 +1444,14 @@ Error os_make_path(Buf *path) {...@@ -1436,7 +1444,14 @@ Error os_make_path(Buf *path) {
14361444
1437Error os_make_dir(Buf *path) {1445Error os_make_dir(Buf *path) {
1438#if defined(ZIG_OS_WINDOWS)1446#if defined(ZIG_OS_WINDOWS)
1439 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(path), buf_len(path) });1447 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(path));
1448 if (memEql(buf_to_slice(path), str("C:\\dev\\tést"))) {
1449 for (size_t i = 0; i < path_space.len; i++) {
1450 fprintf(stderr, "%d ", path_space.data.items[i]);
1451 }
1452 fprintf(stderr, "\n");
1453 }
1454
1440 if (!CreateDirectoryW(&path_space.data.items[0], NULL)) {1455 if (!CreateDirectoryW(&path_space.data.items[0], NULL)) {
1441 if (GetLastError() == ERROR_ALREADY_EXISTS)1456 if (GetLastError() == ERROR_ALREADY_EXISTS)
1442 return ErrorPathAlreadyExists;1457 return ErrorPathAlreadyExists;
...@@ -1727,22 +1742,11 @@ static uint8_t utf8CodepointSequenceLength(uint32_t c) {...@@ -1727,22 +1742,11 @@ static uint8_t utf8CodepointSequenceLength(uint32_t c) {
17271742
1728// Ported from std.unicode.utf8ByteSequenceLength1743// Ported from std.unicode.utf8ByteSequenceLength
1729static uint8_t utf8ByteSequenceLength(uint8_t first_byte) {1744static uint8_t utf8ByteSequenceLength(uint8_t first_byte) {
1730 switch (clzll(~first_byte)) {1745 if (first_byte < 0b10000000) return 1;
1731 case 0:1746 if ((first_byte & 0b11100000) == 0b11000000) return 2;
1732 return 1;1747 if ((first_byte & 0b11110000) == 0b11100000) return 3;
1733 break;1748 if ((first_byte & 0b11111000) == 0b11110000) return 4;
1734 case 2:1749 zig_unreachable();
1735 return 2;
1736 break;
1737 case 3:
1738 return 3;
1739 break;
1740 case 4:
1741 return 4;
1742 break;
1743 default:
1744 zig_unreachable();
1745 }
1746}1750}
17471751
1748// Ported from std/unicode.zig1752// Ported from std/unicode.zig
...@@ -2016,7 +2020,7 @@ Error os_self_exe_shared_libs(ZigList<Buf *> &paths) {...@@ -2016,7 +2020,7 @@ Error os_self_exe_shared_libs(ZigList<Buf *> &paths) {
20162020
2017Error os_file_open_rw(Buf *full_path, OsFile *out_file, OsFileAttr *attr, bool need_write, uint32_t mode) {2021Error os_file_open_rw(Buf *full_path, OsFile *out_file, OsFileAttr *attr, bool need_write, uint32_t mode) {
2018#if defined(ZIG_OS_WINDOWS)2022#if defined(ZIG_OS_WINDOWS)
2019 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(full_path), buf_len(full_path) });2023 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(full_path));
2020 HANDLE result = CreateFileW(&path_space.data.items[0],2024 HANDLE result = CreateFileW(&path_space.data.items[0],
2021 need_write ? (GENERIC_READ|GENERIC_WRITE) : GENERIC_READ,2025 need_write ? (GENERIC_READ|GENERIC_WRITE) : GENERIC_READ,
2022 need_write ? 0 : FILE_SHARE_READ,2026 need_write ? 0 : FILE_SHARE_READ,
...@@ -2121,7 +2125,7 @@ Error os_file_open_w(Buf *full_path, OsFile *out_file, OsFileAttr *attr, uint32_...@@ -2121,7 +2125,7 @@ Error os_file_open_w(Buf *full_path, OsFile *out_file, OsFileAttr *attr, uint32_
21212125
2122Error os_file_open_lock_rw(Buf *full_path, OsFile *out_file) {2126Error os_file_open_lock_rw(Buf *full_path, OsFile *out_file) {
2123#if defined(ZIG_OS_WINDOWS)2127#if defined(ZIG_OS_WINDOWS)
2124 PathSpace path_space = slice_to_prefixed_file_w({ (uint8_t*)buf_ptr(full_path), buf_len(full_path) });2128 PathSpace path_space = slice_to_prefixed_file_w(buf_to_slice(full_path));
2125 for (;;) {2129 for (;;) {
2126 HANDLE result = CreateFileW(&path_space.data.items[0], GENERIC_READ | GENERIC_WRITE,2130 HANDLE result = CreateFileW(&path_space.data.items[0], GENERIC_READ | GENERIC_WRITE,
2127 0, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);2131 0, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);