Mercurial > jwe > octave
changeset 21738:a99c2407f930
use namespace for lo-sysdep.h functions
* lo-sysdep.h, lo-sysdep.cc: Put octave_getcwd, octave_chdir, and
octave_popen2 in namespace octave::sys. Change all uses.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 19 May 2016 01:50:04 -0400 |
parents | ab6c639f0678 |
children | c4ab2e54f100 |
files | liboctave/system/lo-sysdep.cc liboctave/system/lo-sysdep.h liboctave/system/oct-env.cc liboctave/system/oct-syscalls.cc |
diffstat | 4 files changed, 120 insertions(+), 92 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/system/lo-sysdep.cc Thu May 19 01:33:06 2016 -0400 +++ b/liboctave/system/lo-sysdep.cc Thu May 19 01:50:04 2016 -0400 @@ -45,105 +45,111 @@ #include "str-vec.h" #include "oct-locbuf.h" -std::string -octave_getcwd (void) +namespace octave { - std::string retval; + namespace sys + { + std::string + getcwd (void) + { + std::string retval; - // Using the gnulib getcwd module ensures that we have a getcwd that - // will allocate a buffer as large as necessary if buf and size are - // both 0. - - char *tmp = gnulib::getcwd (0, 0); + // Using the gnulib getcwd module ensures that we have a getcwd that + // will allocate a buffer as large as necessary if buf and size are + // both 0. - if (! tmp) - (*current_liboctave_error_handler) ("unable to find current directory"); + char *tmp = gnulib::getcwd (0, 0); - retval = tmp; - free (tmp); + if (! tmp) + (*current_liboctave_error_handler) ("unable to find current directory"); + + retval = tmp; + free (tmp); - return retval; -} + return retval; + } -int -octave_chdir (const std::string& path_arg) -{ - std::string path = octave::sys::file_ops::tilde_expand (path_arg); + int + chdir (const std::string& path_arg) + { + std::string path = octave::sys::file_ops::tilde_expand (path_arg); #if defined (__WIN32__) && ! defined (__CYGWIN__) - if (path.length () == 2 && path[1] == ':') - path += "\\"; + if (path.length () == 2 && path[1] == ':') + path += "\\"; #endif - return gnulib::chdir (path.c_str ()); -} + return gnulib::chdir (path.c_str ()); + } #if defined (__WIN32__) && ! defined (__CYGWIN__) -pid_t -octave_popen2 (const std::string& cmd, const string_vector& args, - bool sync_mode, - int *fildes, std::string& msg) -{ - pid_t pid; - PROCESS_INFORMATION pi; - STARTUPINFO si; - std::string command = "\"" + cmd + "\""; - HANDLE hProcess = GetCurrentProcess (); - HANDLE childRead, childWrite, parentRead, parentWrite; - DWORD pipeMode; + pid_t + popen2 (const std::string& cmd, const string_vector& args, + bool sync_mode, int *fildes, std::string& msg) + { + pid_t pid; + PROCESS_INFORMATION pi; + STARTUPINFO si; + std::string command = "\"" + cmd + "\""; + HANDLE hProcess = GetCurrentProcess (); + HANDLE childRead, childWrite, parentRead, parentWrite; + DWORD pipeMode; + + ZeroMemory (&pi, sizeof (pi)); + ZeroMemory (&si, sizeof (si)); + si.cb = sizeof (si); - ZeroMemory (&pi, sizeof (pi)); - ZeroMemory (&si, sizeof (si)); - si.cb = sizeof (si); + if (! CreatePipe (&childRead, &parentWrite, 0, 0) + || ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, + 0, TRUE, + DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) + { + msg = "popen2: pipe creation failed"; + return -1; + } + if (! CreatePipe (&parentRead, &childWrite, 0, 0) + || ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, + 0, TRUE, + DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) + { + msg = "popen2: pipe creation failed"; + return -1; + } + if (! sync_mode) + { + pipeMode = PIPE_NOWAIT; + SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0); + } + fildes[1] = _open_osfhandle (reinterpret_cast<intptr_t> (parentRead), + _O_RDONLY | _O_BINARY); + fildes[0] = _open_osfhandle (reinterpret_cast<intptr_t> (parentWrite), + _O_WRONLY | _O_BINARY); + si.dwFlags |= STARTF_USESTDHANDLES; + si.hStdInput = childRead; + si.hStdOutput = childWrite; - if (! CreatePipe (&childRead, &parentWrite, 0, 0) - || ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, - 0, TRUE, - DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) - { - msg = "popen2: pipe creation failed"; - return -1; - } - if (! CreatePipe (&parentRead, &childWrite, 0, 0) - || ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, - 0, TRUE, - DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) - { - msg = "popen2: pipe creation failed"; - return -1; + // Ignore first arg as it is the command + for (int k=1; k<args.numel (); k++) + command += " \"" + args[k] + "\""; + OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1); + strcpy (c_command, command.c_str ()); + if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi)) + { + msg = "popen2: process creation failed"; + return -1; + } + pid = pi.dwProcessId; + + CloseHandle (childRead); + CloseHandle (childWrite); + CloseHandle (pi.hProcess); + CloseHandle (pi.hThread); + + return pid; } - if (! sync_mode) - { - pipeMode = PIPE_NOWAIT; - SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0); - } - fildes[1] = _open_osfhandle (reinterpret_cast<intptr_t> (parentRead), - _O_RDONLY | _O_BINARY); - fildes[0] = _open_osfhandle (reinterpret_cast<intptr_t> (parentWrite), - _O_WRONLY | _O_BINARY); - si.dwFlags |= STARTF_USESTDHANDLES; - si.hStdInput = childRead; - si.hStdOutput = childWrite; - - // Ignore first arg as it is the command - for (int k=1; k<args.numel (); k++) - command += " \"" + args[k] + "\""; - OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1); - strcpy (c_command, command.c_str ()); - if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi)) - { - msg = "popen2: process creation failed"; - return -1; - } - pid = pi.dwProcessId; - - CloseHandle (childRead); - CloseHandle (childWrite); - CloseHandle (pi.hProcess); - CloseHandle (pi.hThread); - - return pid; -} #endif + + } +}
--- a/liboctave/system/lo-sysdep.h Thu May 19 01:33:06 2016 -0400 +++ b/liboctave/system/lo-sysdep.h Thu May 19 01:50:04 2016 -0400 @@ -28,15 +28,37 @@ #include <string> #include "lo-ieee.h" + class string_vector; -extern std::string octave_getcwd (void); +namespace octave +{ + namespace sys + { + extern std::string getcwd (void); -extern int octave_chdir (const std::string&); + extern int chdir (const std::string&); #if defined (__WIN32__) && ! defined (__CYGWIN__) -extern pid_t octave_popen2 (const std::string&, const string_vector&, - bool, int *, std::string&); + extern pid_t popen2 (const std::string&, const string_vector&, + bool, int *, std::string&); +#endif + } +} + +#if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS) + +OCTAVE_DEPRECATED ("use octave::sys::getcwd instead") +const auto octave_getcwd = octave::sys::getcwd; + +OCTAVE_DEPRECATED ("use octave::sys::chdir instead") +const auto octave_chdir = octave::sys::chdir; + +#if defined (__WIN32__) && ! defined (__CYGWIN__) +OCTAVE_DEPRECATED ("use octave::sys:: instead") +const auto octave_popen2 = octave::sys::popen2; #endif #endif + +#endif
--- a/liboctave/system/oct-env.cc Thu May 19 01:33:06 2016 -0400 +++ b/liboctave/system/oct-env.cc Thu May 19 01:50:04 2016 -0400 @@ -456,7 +456,7 @@ current_directory = ""; if (verbatim_pwd || current_directory.empty ()) - current_directory = ::octave_getcwd (); + current_directory = octave::sys::getcwd (); return current_directory; } @@ -557,14 +557,14 @@ tmp.resize (len); } - if (! ::octave_chdir (tmp)) + if (! octave::sys::chdir (tmp)) { current_directory = tmp; retval = true; } } else - retval = (! ::octave_chdir (newdir)); + retval = (! octave::sys::chdir (newdir)); return retval; }
--- a/liboctave/system/oct-syscalls.cc Thu May 19 01:33:06 2016 -0400 +++ b/liboctave/system/oct-syscalls.cc Thu May 19 01:50:04 2016 -0400 @@ -322,7 +322,7 @@ bool &interactive) { #if defined (__WIN32__) && ! defined (__CYGWIN__) - return ::octave_popen2 (cmd, args, sync_mode, fildes, msg); + return octave::sys::popen2 (cmd, args, sync_mode, fildes, msg); #else pid_t pid; int child_stdin[2], child_stdout[2];