Skip to content

Commit

Permalink
Fix several C++ type casting quirks
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Aug 4, 2019
1 parent a146e8b commit 2c4cbe9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
17 changes: 10 additions & 7 deletions code/zpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
https://github.com/zpl-c/zpl

Version History:
9.8.1 - Fix several C++ type casting quirks
9.8.0 - Incorporated OpenGL into ZPL core as an optional module
9.7.0 - Added co-routine module
9.6.0 - Added process module for creation and manipulation
Expand Down Expand Up @@ -11581,11 +11582,13 @@ zpl_inline const char *zpl_get_env_buf(const char *name) {
zpl_local_persist wchar_t wbuffer[32767] = {0};
zpl_local_persist char buffer[32767] = {0};

if (!GetEnvironmentVariableW(cast(LPCWSTR)zpl_utf8_to_ucs2_buf(name), cast(LPWSTR)wbuffer, 32767)) {
if (!GetEnvironmentVariableW(
cast(LPCWSTR)zpl_utf8_to_ucs2_buf(cast(const zpl_u8 *)name),
cast(LPWSTR)wbuffer, 32767)) {
return NULL;
}

zpl_ucs2_to_utf8(buffer, 32767, cast(wchar_t*)wbuffer);
zpl_ucs2_to_utf8(cast(zpl_u8*)buffer, 32767, cast(const zpl_u16*)wbuffer);

return (const char *)buffer;
#else
Expand Down Expand Up @@ -12884,12 +12887,12 @@ zpl_inline zpl_i32 zpl_pr_join(zpl_pr *process) {

#ifdef ZPL_SYSTEM_WINDOWS
if (process->f_stdin) {
fclose(process->f_stdin);
fclose(cast(FILE *)process->f_stdin);
}

WaitForSingleObject(process->win32_handle, INFINITE);

if (!GetExitCodeProcess(process->win32_handle, &ret_code)) {
if (!GetExitCodeProcess(process->win32_handle, cast(LPDWORD)&ret_code)) {
zpl_pr_destroy(process);
return -1;
}
Expand All @@ -12908,13 +12911,13 @@ zpl_inline void zpl_pr_destroy(zpl_pr *process) {

#ifdef ZPL_SYSTEM_WINDOWS
if (process->f_stdin) {
fclose(process->f_stdin);
fclose(cast(FILE *)process->f_stdin);
}

fclose(process->f_stdout);
fclose(cast(FILE *)process->f_stdout);

if (process->f_stderr != process->f_stdout) {
fclose(process->f_stderr);
fclose(cast(FILE *)process->f_stderr);
}

CloseHandle(process->win32_handle);
Expand Down
1 change: 1 addition & 0 deletions code/zpl/__main__.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
https://github.com/zpl-c/zpl
Version History:
9.8.1 - Fix several C++ type casting quirks
9.8.0 - Incorporated OpenGL into ZPL core as an optional module
9.7.0 - Added co-routine module
9.6.0 - Added process module for creation and manipulation
Expand Down
6 changes: 4 additions & 2 deletions code/zpl/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ zpl_inline const char *zpl_get_env_buf(const char *name) {
zpl_local_persist wchar_t wbuffer[32767] = {0};
zpl_local_persist char buffer[32767] = {0};

if (!GetEnvironmentVariableW(cast(LPCWSTR)zpl_utf8_to_ucs2_buf(name), cast(LPWSTR)wbuffer, 32767)) {
if (!GetEnvironmentVariableW(
cast(LPCWSTR)zpl_utf8_to_ucs2_buf(cast(const zpl_u8 *)name),
cast(LPWSTR)wbuffer, 32767)) {
return NULL;
}

zpl_ucs2_to_utf8(buffer, 32767, cast(wchar_t*)wbuffer);
zpl_ucs2_to_utf8(cast(zpl_u8*)buffer, 32767, cast(const zpl_u16*)wbuffer);

return (const char *)buffer;
#else
Expand Down
10 changes: 5 additions & 5 deletions code/zpl/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ zpl_inline zpl_i32 zpl_pr_join(zpl_pr *process) {

#ifdef ZPL_SYSTEM_WINDOWS
if (process->f_stdin) {
fclose(process->f_stdin);
fclose(cast(FILE *)process->f_stdin);
}

WaitForSingleObject(process->win32_handle, INFINITE);

if (!GetExitCodeProcess(process->win32_handle, &ret_code)) {
if (!GetExitCodeProcess(process->win32_handle, cast(LPDWORD)&ret_code)) {
zpl_pr_destroy(process);
return -1;
}
Expand All @@ -233,13 +233,13 @@ zpl_inline void zpl_pr_destroy(zpl_pr *process) {

#ifdef ZPL_SYSTEM_WINDOWS
if (process->f_stdin) {
fclose(process->f_stdin);
fclose(cast(FILE *)process->f_stdin);
}

fclose(process->f_stdout);
fclose(cast(FILE *)process->f_stdout);

if (process->f_stderr != process->f_stdout) {
fclose(process->f_stderr);
fclose(cast(FILE *)process->f_stderr);
}

CloseHandle(process->win32_handle);
Expand Down
4 changes: 2 additions & 2 deletions test/coroutines.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void send_req(zpl_co *co) {

// wait until main thread resumes the execution
zpl_co_yield(co);

some_data *r = cast(some_data*)co->data;
printf_safe("step 2: no data, we pass some back\n");

Expand Down Expand Up @@ -60,7 +60,7 @@ int main (void) {
zpl_co_resume(&w1, cast(void*)&r);

do {} while (!zpl_co_waiting(&w1));

// step 3 - resume its execution again, pass data
printf_safe("resume step 3\n");
zpl_co_resume(&w1, cast(void*)&d2);
Expand Down

0 comments on commit 2c4cbe9

Please sign in to comment.