Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Windows: added unicode support. See discussion in �#9
Browse files Browse the repository at this point in the history
  • Loading branch information
eidheim committed Apr 18, 2016
1 parent b3d6108 commit 245e36b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
2 changes: 1 addition & 1 deletion process.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "process.hpp"

Process::Process(const std::string &command, const std::string &path,
Process::Process(const string_type &command, const string_type &path,
std::function<void(const char* bytes, size_t n)> read_stdout,
std::function<void(const char* bytes, size_t n)> read_stderr,
bool open_stdin, size_t buffer_size):
Expand Down
6 changes: 4 additions & 2 deletions process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class Process {
#ifdef _WIN32
typedef DWORD id_type; //Process id type
typedef HANDLE fd_type; //File descriptor type
typedef std::basic_string<TCHAR> string_type;
#else
typedef pid_t id_type;
typedef int fd_type;
typedef std::string string_type;
#endif
private:
class Data {
Expand All @@ -37,7 +39,7 @@ class Process {
#endif
};
public:
Process(const std::string &command, const std::string &path=std::string(),
Process(const string_type &command, const string_type &path=string_type(),
std::function<void(const char *bytes, size_t n)> read_stdout=nullptr,
std::function<void(const char *bytes, size_t n)> read_stderr=nullptr,
bool open_stdin=false,
Expand Down Expand Up @@ -73,7 +75,7 @@ class Process {

std::unique_ptr<fd_type> stdout_fd, stderr_fd, stdin_fd;

id_type open(const std::string &command, const std::string &path);
id_type open(const string_type &command, const string_type &path);
void async_read();
void close_fds();
};
Expand Down
36 changes: 10 additions & 26 deletions process_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace {
}

//Based on the example at https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx.
Process::id_type Process::open(const std::string &command, const std::string &path) {
Process::id_type Process::open(const string_type &command, const string_type &path) {
if(open_stdin)
stdin_fd=std::unique_ptr<fd_type>(new fd_type(NULL));
if(read_stdout)
Expand Down Expand Up @@ -86,40 +86,24 @@ Process::id_type Process::open(const std::string &command, const std::string &pa
if(stdin_fd || stdout_fd || stderr_fd)
startup_info.dwFlags |= STARTF_USESTDHANDLES;

char* path_cstr;
if(path=="")
path_cstr=NULL;
else {
path_cstr=new char[path.size()+1];
std::strcpy(path_cstr, path.c_str());
}

char* command_cstr;
string_type process_command=command;
#ifdef MSYS_PROCESS_USE_SH
size_t pos=0;
std::string sh_command=command;
while((pos=sh_command.find('\\', pos))!=std::string::npos) {
sh_command.replace(pos, 1, "\\\\\\\\");
while((pos=process_command.find('\\', pos))!=string_type::npos) {
process_command.replace(pos, 1, "\\\\\\\\");
pos+=4;
}
pos=0;
while((pos=sh_command.find('\"', pos))!=std::string::npos) {
sh_command.replace(pos, 1, "\\\"");
while((pos=process_command.find('\"', pos))!=string_type::npos) {
process_command.replace(pos, 1, "\\\"");
pos+=2;
}
sh_command.insert(0, "sh -c \"");
sh_command+="\"";
command_cstr=new char[sh_command.size()+1];
std::strcpy(command_cstr, sh_command.c_str());
#else
command_cstr=new char[command.size()+1];
std::strcpy(command_cstr, command.c_str());
process_command.insert(0, "sh -c \"");
process_command+="\"";
#endif

BOOL bSuccess = CreateProcess(NULL, command_cstr, NULL, NULL, TRUE, 0,
NULL, path_cstr, &startup_info, &process_info);
delete[] path_cstr;
delete[] command_cstr;
BOOL bSuccess = CreateProcess(NULL, process_command.empty()?NULL:&process_command[0], NULL, NULL, TRUE, 0,
NULL, path.empty()?NULL:path.c_str(), &startup_info, &process_info);

if(!bSuccess) {
CloseHandle(process_info.hProcess);
Expand Down

0 comments on commit 245e36b

Please sign in to comment.