diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2016-09-15 21:49:24 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2016-09-16 12:57:09 +0300 |
commit | f4f6d906733027a7bd802e035b3e9852db7be967 (patch) | |
tree | 9ca4c5c72b2ca0c7bea49bf43b62bfa8adda1f38 /butl/process.cxx | |
parent | 60dfe51f11d247f8490f54b4441a4fda14d6c66a (diff) |
Add process::print()
Diffstat (limited to 'butl/process.cxx')
-rw-r--r-- | butl/process.cxx | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/butl/process.cxx b/butl/process.cxx index e13a8cd..38948a1 100644 --- a/butl/process.cxx +++ b/butl/process.cxx @@ -37,6 +37,7 @@ #include <cassert> #include <cstddef> // size_t #include <cstring> // strlen(), strchr() +#include <ostream> #include <butl/utility> // casecmp() #include <butl/fdstream> // fdnull(), fdclose() @@ -120,6 +121,43 @@ namespace butl return r; } + void process:: + print (ostream& o, const char* const args[], size_t n) + { + size_t m (0); + const char* const* p (args); + do + { + if (m != 0) + o << " |"; // Trailing space will be added inside the loop. + + for (m++; *p != nullptr; p++, m++) + { + if (p != args) + o << ' '; + + // Quote if empty or contains spaces. + // + bool q (**p == '\0' || strchr (*p, ' ') != nullptr); + + if (q) + o << '"'; + + o << *p; + + if (q) + o << '"'; + } + + if (m < n) // Can we examine the next element? + { + p++; + m++; + } + + } while (*p != nullptr); + } + #ifndef _WIN32 static process_path |