aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2016-08-25 09:36:18 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2016-08-25 09:36:18 +0200
commit9635692214cc6c3d19578bcadac4da68e0742740 (patch)
tree3d1b865355b78faaf8748dfe920cd449d1bbae41
parent67aabb1dca0ce7a4d8c62c07f0784132410fd63a (diff)
Add process::try_path_search()
-rw-r--r--butl/process26
-rw-r--r--butl/process.cxx30
-rw-r--r--butl/process.ixx24
3 files changed, 62 insertions, 18 deletions
diff --git a/butl/process b/butl/process
index 7966309..96b161f 100644
--- a/butl/process
+++ b/butl/process
@@ -29,8 +29,8 @@ namespace butl
process_error (int e, bool child)
: system_error (e, std::system_category ()), child_ (child) {}
#else
- process_error (int e)
- : system_error (e, std::system_category ()), child_ (false) {}
+ process_error (int e, bool child = false)
+ : system_error (e, std::system_category ()), child_ (child) {}
process_error (const std::string& d, int e = ECHILD)
: system_error (e, std::system_category (), d), child_ (false) {}
@@ -243,16 +243,22 @@ namespace butl
path_search (const char* file, bool init, const dir_path& = dir_path ());
static process_path
- path_search (const std::string& f, bool i, const dir_path& fb = dir_path ())
- {
- return path_search (f.c_str (), i, fb);
- }
+ path_search (const std::string&, bool, const dir_path& = dir_path ());
static process_path
- path_search (const path& f, bool i, const dir_path& fb = dir_path ())
- {
- return path_search (f.string ().c_str (), i, fb);
- }
+ path_search (const path&, bool, const dir_path& = dir_path ());
+
+ // As above but if not found return empty process_path instead of
+ // throwing.
+ //
+ static process_path
+ try_path_search (const char*, bool, const dir_path& = dir_path ());
+
+ static process_path
+ try_path_search (const std::string&, bool, const dir_path& = dir_path ());
+
+ static process_path
+ try_path_search (const path&, bool, const dir_path& = dir_path ());
public:
#ifndef _WIN32
diff --git a/butl/process.cxx b/butl/process.cxx
index 37e9f72..ed26eb9 100644
--- a/butl/process.cxx
+++ b/butl/process.cxx
@@ -32,6 +32,8 @@
# include <butl/win32-utility>
#endif
+#include <errno.h>
+
#include <cassert>
#include <cstddef> // size_t
#include <cstring> // strlen(), strchr()
@@ -96,12 +98,24 @@ namespace butl
process_path process::
path_search (const char* f, bool init, const dir_path& fb)
{
+ process_path r (try_path_search (f, init, fb));
+
+ if (r.empty ())
+ throw process_error (ENOENT, false);
+
+ return r;
+ }
+
+ process_path process::
+ try_path_search (const char* f, bool init, const dir_path& fb)
+ {
process_path r (butl::path_search (f, fb));
- path& rp (r.recall);
- r.initial = init
- ? f
- : (rp.empty () ? (rp = path (f)) : rp).string ().c_str ();
+ if (!init && !r.empty ())
+ {
+ path& rp (r.recall);
+ r.initial = (rp.empty () ? (rp = path (f)) : rp).string ().c_str ();
+ }
return r;
}
@@ -117,7 +131,7 @@ namespace butl
size_t fn (strlen (f));
- process_path r (nullptr, path (), path ());
+ process_path r (f, path (), path ()); // Make sure it is not empty.
path& rp (r.recall);
path& ep (r.effect);
@@ -190,7 +204,7 @@ namespace butl
// Did not find anything.
//
- throw process_error (ENOENT, false);
+ return process_path ();
}
process::
@@ -378,7 +392,7 @@ namespace butl
ext = (e == nullptr || casecmp (e, ".exe") != 0);
}
- process_path r (nullptr, path (), path ());
+ process_path r (f, path (), path ()); // Make sure it is not empty.
path& rp (r.recall);
path& ep (r.effect);
@@ -519,7 +533,7 @@ namespace butl
// Did not find anything.
//
- throw process_error (ENOENT);
+ return process_path ();
}
class auto_handle
diff --git a/butl/process.ixx b/butl/process.ixx
index 264f77c..83918bf 100644
--- a/butl/process.ixx
+++ b/butl/process.ixx
@@ -75,6 +75,30 @@ namespace butl
return r;
}
+ inline process_path process::
+ path_search (const std::string& f, bool i, const dir_path& fb)
+ {
+ return path_search (f.c_str (), i, fb);
+ }
+
+ inline process_path process::
+ path_search (const path& f, bool i, const dir_path& fb)
+ {
+ return path_search (f.string ().c_str (), i, fb);
+ }
+
+ inline process_path process::
+ try_path_search (const std::string& f, bool i, const dir_path& fb)
+ {
+ return try_path_search (f.c_str (), i, fb);
+ }
+
+ inline process_path process::
+ try_path_search (const path& f, bool i, const dir_path& fb)
+ {
+ return try_path_search (f.string ().c_str (), i, fb);
+ }
+
inline process::
process ()
: handle (0),