[PATCH xorg-gtest 4/4] process: add Start(program, vector<char*>)
Peter Hutterer
peter.hutterer at who-t.net
Tue Jul 10 18:51:10 PDT 2012
Same thing as the va_list but takes a std::vector of arguments instead.
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
include/xorg/gtest/xorg-gtest-process.h | 16 ++++++++++++++++
src/process.cpp | 25 +++++++++++++++++++------
2 files changed, 35 insertions(+), 6 deletions(-)
diff --git a/include/xorg/gtest/xorg-gtest-process.h b/include/xorg/gtest/xorg-gtest-process.h
index 47aefcf..0b721ce 100644
--- a/include/xorg/gtest/xorg-gtest-process.h
+++ b/include/xorg/gtest/xorg-gtest-process.h
@@ -93,6 +93,22 @@ class Process {
/**
* Starts a program as a child process.
*
+ * See 'man execvp' for further information on the elements in
+ * the vector.
+ *
+ * @param program The program to start.
+ * @param args Vector of arguments passed to the program.
+ *
+ * @throws std::runtime_error on failure.
+ *
+ * @post If successful: Child process forked and program started.
+ * @post If successful: Subsequent calls to Pid() return child process pid.
+ */
+ void Start(const std::string& program, const std::vector<std::string> &args);
+
+ /**
+ * Starts a program as a child process.
+ *
* See 'man execvp' for further information on the variadic argument list.
*
* @param program The program to start.
diff --git a/src/process.cpp b/src/process.cpp
index e79b694..7b60afc 100644
--- a/src/process.cpp
+++ b/src/process.cpp
@@ -48,7 +48,7 @@ xorg::testing::Process::Process() : d_(new Private) {
d_->pid = -1;
}
-void xorg::testing::Process::Start(const std::string& program, va_list args) {
+void xorg::testing::Process::Start(const std::string &program, const std::vector<std::string> &argv) {
if (d_->pid != -1)
throw std::runtime_error("Attempting to start an already started process");
@@ -61,18 +61,31 @@ void xorg::testing::Process::Start(const std::string& program, va_list args) {
close(1);
close(2);
- std::vector<char*> argv;
+ std::vector<char*> args;
+ std::vector<std::string>::const_iterator it;
- do
- argv.push_back(va_arg(args, char*));
- while (argv.back());
+ for (it = argv.begin(); it != argv.end(); it++)
+ if (!it->empty())
+ args.push_back(strdup(it->c_str()));
+ args.push_back(NULL);
- execvp(program.c_str(), &argv[0]);
+ execvp(program.c_str(), &args[0]);
throw std::runtime_error("Failed to start process");
}
}
+void xorg::testing::Process::Start(const std::string& program, va_list args) {
+ std::vector<std::string> argv;
+
+ do {
+ std::string arg(va_arg(args, char*));
+ argv.push_back(arg);
+ } while (!argv.back().empty());
+
+ Start(program, argv);
+}
+
void xorg::testing::Process::Start(const std::string& program, ...) {
va_list list;
va_start(list, program);
--
1.7.10.4
More information about the xorg-devel
mailing list