From 9a160bf853af796c4c09ec05f77ca5fe1cc293e4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 13 Sep 2010 14:26:43 +0200 Subject: Use cutl::fs::path for path handling --- odb/odb.cxx | 132 ++++++++++++++++++++++-------------------------------------- 1 file changed, 48 insertions(+), 84 deletions(-) (limited to 'odb/odb.cxx') diff --git a/odb/odb.cxx b/odb/odb.cxx index c15d587..789238f 100644 --- a/odb/odb.cxx +++ b/odb/odb.cxx @@ -19,6 +19,8 @@ #include // size_t #include +#include + #include #include @@ -27,15 +29,13 @@ #endif using namespace std; +using cutl::fs::path; -static string -driver_path (string const& driver); - -static string -plugin_path (string const& driver); +static path +driver_path (path const& driver); -inline size_t -rfind_slash (string const&); +static path +plugin_path (path const& driver); static char const* const db_macro[] = { @@ -54,11 +54,11 @@ main (int argc, char* argv[]) // driver. // #ifndef STATIC_PLUGIN - string plugin (plugin_path (argv[0])); + path plugin (plugin_path (path (argv[0]))); #else // Use a dummy name if the plugin is linked into the compiler. // - string plugin ("odb"); + path plugin ("odb"); #endif if (plugin.empty ()) @@ -76,21 +76,21 @@ main (int argc, char* argv[]) // g++ by default. // #ifdef GXX_NAME - string gxx (GXX_NAME); + path gxx (GXX_NAME); // If the g++ name is a relative path (starts with '.'), then use // our own path as base. // - if (!gxx.empty () && gxx[0] == '.') + if (!gxx.empty () && gxx.string ()[0] == '.') { - string path (driver_path (argv[0])); - size_t p (rfind_slash (path)); + path dp (driver_path (path (argv[0]))); + path d (dp.directory ()); - if (p != string::npos) - gxx = string (path, 0, p + 1) + gxx; + if (!d.empty ()) + gxx = d / gxx; } - args.push_back (gxx); + args.push_back (gxx.string ()); #else args.push_back ("g++"); #endif @@ -100,7 +100,7 @@ main (int argc, char* argv[]) args.push_back ("-x"); args.push_back ("c++"); args.push_back ("-S"); - args.push_back ("-fplugin=" + plugin); + args.push_back ("-fplugin=" + plugin.string ()); // Parse driver options. // @@ -380,31 +380,12 @@ main (int argc, char* argv[]) // Path manipulation. // -#ifdef _WIN32 -const char dir_sep = '\\'; -const char path_sep = ';'; -#else -const char dir_sep = '/'; -const char path_sep = ':'; -#endif - -inline size_t -rfind_slash (string const& s) +static path +driver_path (path const& drv) { - size_t r (s.rfind ('/')); - - if (r == string::npos) - r = s.rfind ('\\'); + typedef path::traits traits; - return r; -} - -static string -driver_path (string const& drv) -{ - size_t p (rfind_slash (drv)); - - if (p != string::npos) + if (!drv.directory ().empty ()) return drv; // Search the PATH environment variable. @@ -417,38 +398,33 @@ driver_path (string const& drv) if (char const* s = getenv ("PATH")) paths = s; else - paths = path_sep; + paths = traits::path_separator; // On Windows also check the current directory. // #ifdef _WIN32 - paths += path_sep; + paths += traits::path_separator; #endif struct stat info; - for (size_t b (0), e (paths.find (path_sep)); b != string::npos;) + for (size_t b (0), e (paths.find (traits::path_separator)); + b != string::npos;) { - string p (paths, b, e != string::npos ? e - b : e); + path p (string (paths, b, e != string::npos ? e - b : e)); // Empty path (i.e., a double colon or a colon at the beginning // or end of PATH) means search in the current dirrectory. // if (p.empty ()) - p = "."; - - string dp (p); - - char l (p[p.size () - 1]); - if (l != '/' && l != '\\') - dp += dir_sep; + p = path ("."); - dp += drv; + path dp (p / drv); // Just check that the file exist without checking for // permissions, etc. // - if (stat (dp.c_str (), &info) == 0 && S_ISREG (info.st_mode)) + if (stat (dp.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode)) return dp; // On Windows also try the path with the .exe extension. @@ -456,7 +432,7 @@ driver_path (string const& drv) #ifdef _WIN32 dp += ".exe"; - if (stat (dp.c_str (), &info) == 0 && S_ISREG (info.st_mode)) + if (stat (dp.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode)) return dp; #endif @@ -465,58 +441,46 @@ driver_path (string const& drv) else { b = e + 1; - e = paths.find (path_sep, b); + e = paths.find (traits::path_separator, b); } } - return ""; + return path (); } -static string -plugin_path (string const& drv) +static path +plugin_path (path const& drv) { - string path (driver_path (drv)); + path dp (driver_path (drv)); - if (!path.empty ()) + if (!dp.empty ()) { - // If the driver name starts with 'lt-', then we are running thought + // If the driver name starts with 'lt-', then we are running through // the libtool script. Strip this prefix -- the shared object should // be in the same directory. // { - size_t p (rfind_slash (path)); - p = p != string::npos ? p + 1 : 0; - - if (p + 2 < path.size () && - path[p] == 'l' && path[p + 1] == 't' && path[p + 2] == '-') - { - path = (p != 0 ? string (path, 0, p) : string ()) + - string (path, p + 3, string::npos); - } + string n (dp.leaf ().string ()); + if (n.size () > 3 && n[0] == 'l' && n[1] == 't' && n[2] == '-') + dp = dp.directory () / path (string (n, 3, string::npos)); } struct stat info; - string so (path + ".so"); - if (stat (so.c_str (), &info) == 0) + path so (dp + ".so"); + if (stat (so.string ().c_str (), &info) == 0) return so; - string la (path + ".la"); - if (stat (la.c_str (), &info) == 0) + path la (dp + ".la"); + if (stat (la.string ().c_str (), &info) == 0) { - size_t p (rfind_slash (path)); - - if (p != string::npos) - { - string so (path, 0, p + 1); - so += string (".libs") + dir_sep + string (path, p + 1) + ".so"; + so = la.directory () / path (".libs") / dp.leaf () + ".so"; - if (stat (so.c_str (), &info) == 0) - return so; - } + if (stat (so.string ().c_str (), &info) == 0) + return so; } } - return ""; + return path (); } -- cgit v1.1