Skip to content

Commit

Permalink
ExternalInterface: fix conversion of std::wstring to std:string on no…
Browse files Browse the repository at this point in the history
…n-Windows systems for file dialog functions (closes #1622)
  • Loading branch information
joshtynjala committed Mar 28, 2023
1 parent 3b55882 commit 95411ac
Showing 1 changed file with 61 additions and 28 deletions.
89 changes: 61 additions & 28 deletions project/src/ExternalInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,54 @@ namespace lime {
}


value wstring_to_value (std::wstring* val) {

if (val) {

#ifdef HX_WINDOWS
return alloc_wstring (val->c_str ());
#else
std::string _val = std::string (val->begin (), val->end ());
return alloc_string (_val.c_str ());
#endif

} else {

return 0;

}

}


vbyte* wstring_to_vbytes (std::wstring* val) {

if (val) {

#ifdef HX_WINDOWS
int size = std::wcslen (val->c_str ());
char* result = (char*)malloc (size + 1);
std::wcstombs (result, val->c_str (), size);
result[size] = '\0';
return (vbyte*)result;
#else
std::string _val = std::string (val->begin (), val->end ());
int size = std::strlen (_val.c_str ());
char* result = (char*)malloc (size + 1);
std::strncpy (result, _val.c_str (), size);
result[size] = '\0';
return (vbyte*)result;
#endif

} else {

return 0;

}

}


value lime_application_create () {

Application* application = CreateApplication ();
Expand Down Expand Up @@ -687,7 +735,7 @@ namespace lime {

if (path) {

value _path = alloc_wstring (path->c_str ());
value _path = wstring_to_value (path);
delete path;
return _path;

Expand Down Expand Up @@ -720,13 +768,9 @@ namespace lime {

if (path) {

int size = std::wcslen (path->c_str ());
char* result = (char*)malloc (size + 1);
std::wcstombs (result, path->c_str (), size);
result[size] = '\0';
vbyte* _path = wstring_to_vbytes (path);
delete path;

return (vbyte*)result;
return _path;

} else {

Expand Down Expand Up @@ -757,7 +801,7 @@ namespace lime {

if (path) {

value _path = alloc_wstring (path->c_str ());
value _path = wstring_to_value (path);
delete path;
return _path;

Expand Down Expand Up @@ -790,13 +834,9 @@ namespace lime {

if (path) {

int size = std::wcslen (path->c_str ());
char* result = (char*)malloc (size + 1);
std::wcstombs (result, path->c_str (), size);
result[size] = '\0';
vbyte* _path = wstring_to_vbytes (path);
delete path;

return (vbyte*)result;
return _path;

} else {

Expand Down Expand Up @@ -830,7 +870,8 @@ namespace lime {

for (int i = 0; i < files.size (); i++) {

val_array_set_i (result, i, alloc_wstring (files[i]->c_str ()));
value _file = wstring_to_value (files[i]);
val_array_set_i (result, i, _file);
delete files[i];

}
Expand Down Expand Up @@ -864,12 +905,8 @@ namespace lime {

for (int i = 0; i < files.size (); i++) {

int size = std::wcslen (files[i]->c_str ());
char* _file = (char*)malloc (size + 1);
std::wcstombs (_file, files[i]->c_str (), size);
_file[size] = '\0';

*resultData++ = (vbyte*)_file;
vbyte* _file = wstring_to_vbytes (files[i]);
*resultData++ = _file;
delete files[i];

}
Expand Down Expand Up @@ -899,7 +936,7 @@ namespace lime {

if (path) {

value _path = alloc_wstring (path->c_str ());
value _path = wstring_to_value (path);
delete path;
return _path;

Expand Down Expand Up @@ -932,13 +969,9 @@ namespace lime {

if (path) {

int size = std::wcslen (path->c_str ());
char* result = (char*)malloc (size + 1);
std::wcstombs (result, path->c_str (), size);
result[size] = '\0';
vbyte* _path = wstring_to_vbytes (path);
delete path;

return (vbyte*)result;
return _path;

} else {

Expand Down

0 comments on commit 95411ac

Please sign in to comment.