[Saga-devel] saga SVN commit 3257: /trunk/saga/saga/adaptors/utils/
amerzky at cct.lsu.edu
amerzky at cct.lsu.edu
Sat Jan 10 15:25:10 CST 2009
User: amerzky
Date: 2009/01/10 03:25 PM
Added:
/trunk/saga/saga/adaptors/utils/process/
Makefile, process.cpp, process.hpp
Modified:
/trunk/saga/saga/adaptors/utils/
Makefile, utils.hpp
/trunk/saga/saga/adaptors/utils/ini/
Makefile
Log:
moving boost::process wrapper out of the adaptors, into
adaptors::utils, to reduce code redundency.
Added some helper functions to utils, too (split, grep, tac,
rev)
A
File Changes:
Directory: /trunk/saga/saga/adaptors/utils/ini/
===============================================
File [modified]: Makefile
Delta lines: +1 -1
===================================================================
--- trunk/saga/saga/adaptors/utils/ini/Makefile 2009-01-10 21:00:07 UTC (rev 3256)
+++ trunk/saga/saga/adaptors/utils/ini/Makefile 2009-01-10 21:24:48 UTC (rev 3257)
@@ -10,5 +10,5 @@
SAGA_SRC = ini.cpp
SAGA_OBJ = ini.o
-include $(SAGA_ROOT)/make/saga.application.mk
+include $(SAGA_ROOT)/make/saga.mk
Directory: /trunk/saga/saga/adaptors/utils/process/
===================================================
File [added]: Makefile
Delta lines: +14 -0
===================================================================
--- trunk/saga/saga/adaptors/utils/process/Makefile 2009-01-10 21:00:07 UTC (rev 3256)
+++ trunk/saga/saga/adaptors/utils/process/Makefile 2009-01-10 21:24:48 UTC (rev 3257)
@@ -0,0 +1,14 @@
+# Copyright (c) 2005-2006 Andre Merzky (andre at merzky.net)
+#
+# Use, modification and distribution is subject to the Boost Software
+# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+
+SAGA_ROOT = ../../../../../
+
+SAGA_HDR = process.hpp
+SAGA_SRC = process.cpp
+SAGA_OBJ = process.o
+
+include $(SAGA_ROOT)/make/saga.mk
+
File [added]: process.cpp
Delta lines: +277 -0
===================================================================
--- trunk/saga/saga/adaptors/utils/process/process.cpp 2009-01-10 21:00:07 UTC (rev 3256)
+++ trunk/saga/saga/adaptors/utils/process/process.cpp 2009-01-10 21:24:48 UTC (rev 3257)
@@ -0,0 +1,277 @@
+// Copyright (c) 2005-2007 Andre Merzky (andre at merzky.net)
+//
+// Use, modification and distribution is subject to the Boost Software
+// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+// TODO: add stdin handling
+
+#include <saga/saga/adaptors/utils/process/process.hpp>
+
+// for the throw macro...
+#include <saga/impl/exception.hpp>
+
+namespace saga
+{
+ namespace adaptors
+ {
+ namespace utils
+ {
+ process::process (void)
+ : cmd_ (""),
+ done_ (false),
+ fail_ (false)
+ {
+ }
+
+ process::process (std::string cmd)
+ : cmd_ (cmd),
+ done_ (false),
+ fail_ (false)
+ {
+ }
+
+ process::process (std::vector <std::string> args)
+ : args_ (args),
+ done_ (false),
+ fail_ (false)
+ {
+ }
+
+ process::process (std::map <std::string, std::string> env)
+ : env_ (env),
+ done_ (false),
+ fail_ (false)
+ {
+ }
+
+ process::process (std::string cmd,
+ std::vector <std::string> args)
+ : cmd_ (cmd),
+ args_ (args),
+ done_ (false),
+ fail_ (false)
+ {
+ }
+
+ process::process (std::string cmd,
+ std::map <std::string, std::string> env)
+ : cmd_ (cmd),
+ env_ (env),
+ done_ (false),
+ fail_ (false)
+ {
+ }
+
+ process::process (std::string cmd,
+ std::vector <std::string> args,
+ std::map <std::string, std::string> env)
+ : cmd_ (cmd),
+ args_ (args),
+ env_ (env),
+ done_ (false),
+ fail_ (false)
+ {
+ }
+
+ void process::clear (void)
+ {
+ cmd_ = "";
+
+ env_.clear ();
+ args_.clear ();
+
+ msg_ = "";
+ fail_ = false;
+ done_ = false;
+
+ out_v_.clear ();
+ err_v_.clear ();
+ }
+
+ void process::set_cmd (std::string cmd)
+ {
+ cmd_= cmd;
+ }
+
+ void process::clear_args (void)
+ {
+ args_.clear ();
+ }
+
+ void process::set_args (std::vector <std::string> args)
+ {
+ args_ = args;
+ }
+
+ void process::add_args (std::string arg_1,
+ std::string arg_2)
+ {
+ args_.push_back (arg_1);
+ args_.push_back (arg_2);
+ }
+
+ void process::add_arg (std::string arg)
+ {
+ args_.push_back (arg);
+ }
+
+ void process::clear_env (void)
+ {
+ env_.clear ();
+ }
+
+ void process::set_env (std::map <std::string, std::string> env)
+ {
+ env_ = env;
+ }
+
+ void process::add_env (std::string key, std::string val)
+ {
+ env_[key] = val;
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////
+ //
+ // run a process, and return stdout. In case of errors, throw a NoSuccess with
+ // stderr as error message
+ //
+ std::vector <std::string> process::run_sync (void)
+ {
+ try
+ {
+ // reset state
+ fail_ = false;
+ done_ = false;
+
+ if ( cmd_ == "")
+ {
+ std::cout << " === no cmd\n";
+ fail_ = true;
+ msg_ = "no command to run";
+
+ std::vector <std::string> out;
+ return out;
+ }
+
+
+ boost::process::command_line cl (cmd_);
+ std::string log (" >> ");
+ log += cmd_ + " ";
+
+ for ( unsigned int i = 0; i < args_.size (); i++ )
+ {
+ cl.argument (args_[i]);
+ log += args_[i] + " ";
+ }
+
+ SAGA_LOG_ALWAYS (log.c_str ());
+
+ boost::process::launcher l;
+
+ l.set_stdin_behavior (boost::process::redirect_stream);
+ l.set_stdout_behavior (boost::process::redirect_stream);
+ l.set_stderr_behavior (boost::process::redirect_stream);
+
+ std::map <std::string, std::string> :: iterator it;
+
+ for ( it = env_.begin (); it != env_.end (); it++ )
+ {
+ l.set_environment ( (*it).first, (*it).second );
+ }
+
+ c_ = l.start (cl);
+
+ boost::process::postream & in = c_.get_stdin ();
+ boost::process::pistream & out = c_.get_stdout ();
+ boost::process::pistream & err = c_.get_stderr ();
+
+
+ for ( std::string line; getline (out, line); out_v_.push_back (line) );
+ for ( std::string line; getline (err, line); err_v_.push_back (line) );
+
+ boost::process::status status = c_.wait();
+
+ if ( ! status.exited () || status.exit_status () )
+ {
+ fail_ = true;
+ msg_ = "exit status != 0";
+
+ SAGA_LOG_ALWAYS (msg_.c_str ());
+
+ for ( int i = 0; i < err_v_.size (); i++ )
+ {
+ SAGA_LOG_ALWAYS (err_v_[i].c_str ());
+ }
+ }
+ else
+ {
+ done_ = true;
+ }
+
+ }
+ catch ( const boost::process::not_found_error <std::string> & e )
+ {
+ SAGA_THROW_NO_OBJECT ("Could not find executable in path", saga::NoSuccess);
+ }
+ catch ( const boost::process::system_error & e )
+ {
+ SAGA_THROW_NO_OBJECT ("Execution failed", saga::NoSuccess);
+ }
+
+ return out_v_;
+ }
+
+ boost::process::pistream & process::get_out (void)
+ {
+ return c_.get_stdout ();
+ }
+
+ boost::process::pistream & process::get_err (void)
+ {
+ return c_.get_stderr ();
+ }
+
+
+ std::string process::get_out_s (void)
+ {
+ std::string out;
+
+ for ( int i = 0; i < out_v_.size (); i++ )
+ {
+ out += out_v_[i] + "\n";
+ }
+
+ return out;
+ }
+
+ std::string process::get_err_s (void)
+ {
+ std::string err;
+
+ for ( int i = 0; i < err_v_.size (); i++ )
+ {
+ err += err_v_[i] + "\n";
+ }
+
+ return err;
+ }
+
+
+ std::vector <std::string> process::get_out_v (void)
+ {
+ return out_v_;
+ }
+
+ std::vector <std::string> process::get_err_v (void)
+ {
+ return err_v_;
+ }
+ ///////////////////////////////////////////////////////////////////////////////
+
+ } // namespace utils
+
+ } // namespace adaptors
+
+} // namespace saga
+
File [added]: process.hpp
Delta lines: +96 -0
===================================================================
--- trunk/saga/saga/adaptors/utils/process/process.hpp 2009-01-10 21:00:07 UTC (rev 3256)
+++ trunk/saga/saga/adaptors/utils/process/process.hpp 2009-01-10 21:24:48 UTC (rev 3257)
@@ -0,0 +1,96 @@
+// Copyright (c) 2005-2007 Andre Merzky (andre at merzky.net)
+//
+// Use, modification and distribution is subject to the Boost Software
+// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef _SAGA_PROCESS_H_
+#define _SAGA_PROCESS_H_ 1
+
+#include <map>
+#include <vector>
+#include <iostream>
+
+// other includes from SAGA (this pulls export definitions, which we actually
+// want, but can't include directly for some reason
+#include <saga/saga/util.hpp>
+
+// boost process
+#include <boost/process.hpp>
+
+namespace saga
+{
+ namespace adaptors
+ {
+ namespace utils
+ {
+ class SAGA_EXPORT process
+ {
+ private:
+ std::string cmd_;
+ std::vector <std::string> args_;
+ std::map <std::string, std::string> env_;
+
+ boost::process::child c_;
+
+ std::vector <std::string> out_v_;
+ std::vector <std::string> err_v_;
+
+ bool done_;
+ bool fail_;
+ std::string msg_;
+
+
+ public:
+ process (void);
+
+ process (std::string cmd);
+ process (std::vector <std::string> args);
+ process (std::map <std::string, std::string> env);
+
+ process (std::string cmd,
+ std::vector <std::string> args);
+ process (std::string cmd,
+ std::map <std::string, std::string> env);
+
+ process (std::string cmd,
+ std::vector <std::string> args,
+ std::map <std::string, std::string> env);
+
+ void clear (void);
+ void set_cmd (std::string cmd);
+
+ void clear_args (void);
+ void set_args (std::vector <std::string> args);
+ void add_arg (std::string arg);
+ void add_args (std::string arg_1,
+ std::string arg_2);
+
+ void clear_env (void);
+ void set_env (std::map <std::string, std::string> env);
+ void add_env (std::string key, std::string val);
+
+ std::vector <std::string> run_sync (void);
+
+ boost::process::pistream & get_out (void);
+ boost::process::pistream & get_err (void);
+
+ std::string get_out_s (void);
+ std::string get_err_s (void);
+
+ std::vector <std::string> get_out_v (void);
+ std::vector <std::string> get_err_v (void);
+
+ bool done (void) { return done_; }
+ bool fail (void) { return fail_; }
+ std::string msg (void) { return msg_; }
+ };
+
+ } // namespace utils
+
+ } // namespace adaptors
+
+} // namespace saga
+
+#endif // _SAGA_PROCESS_H_
+
Directory: /trunk/saga/saga/adaptors/utils/
===========================================
File [modified]: Makefile
Delta lines: +4 -2
===================================================================
--- trunk/saga/saga/adaptors/utils/Makefile 2009-01-10 21:00:07 UTC (rev 3256)
+++ trunk/saga/saga/adaptors/utils/Makefile 2009-01-10 21:24:48 UTC (rev 3257)
@@ -6,9 +6,11 @@
SAGA_ROOT = ../../../../
-SAGA_SUBDIRS = ini
+SAGA_SUBDIRS = ini process
-SAGA_HDR = $(wildcard *.hpp)
+SAGA_HDR = utils.hpp
+SAGA_SRC = utils.cpp
+SAGA_OBJ = utils.o
include $(SAGA_ROOT)/make/saga.mk
File [modified]: utils.hpp
Delta lines: +24 -4
===================================================================
--- trunk/saga/saga/adaptors/utils/utils.hpp 2009-01-10 21:00:07 UTC (rev 3256)
+++ trunk/saga/saga/adaptors/utils/utils.hpp 2009-01-10 21:24:48 UTC (rev 3257)
@@ -4,11 +4,31 @@
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
-#ifndef SAGA_SAGA_ADAPTORS_UTILS_HPP
-#define SAGA_ADAPTORS_UTILS_HPP
+#ifndef SAGA_UTILS_HPP
+#define SAGA_UTILS_HPP
#include <saga/saga/adaptors/utils/ini/ini.hpp>
+#include <saga/saga/adaptors/utils/process/process.hpp>
+
+namespace saga
+{
+ namespace adaptors
+ {
+ namespace utils
+ {
+ std::vector <std::string> split (std::string line,
+ char delim = '\t');
+ std::vector <std::string> head (int n,
+ std::vector <std::string> in);
+ std::vector <std::string> tail (int n,
+ std::vector <std::string> in);
+ std::vector <std::string> rev (std::vector <std::string> in);
+ std::vector <std::string> tac (std::vector <std::string> in);
+ std::vector <std::string> grep (std::string pattern,
+ std::vector <std::string> in);
+ } // namespace utils
+ } // namespace adaptors
+} // namespace saga
-#endif // SAGA_ADAPTORS_UTILS_HPP
+#endif // SAGA_UTILS_HPP
-
More information about the saga-devel
mailing list