From f0fb6aeab118255266370121db79ab2a2fed88ad Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 13 Sep 2009 18:41:38 +0200 Subject: Add the fs::basic_path class --- cutl/fs/path.txx | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 cutl/fs/path.txx (limited to 'cutl/fs/path.txx') diff --git a/cutl/fs/path.txx b/cutl/fs/path.txx new file mode 100644 index 0000000..caff259 --- /dev/null +++ b/cutl/fs/path.txx @@ -0,0 +1,96 @@ +// file : cutl/fs/path.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +namespace cutl +{ + namespace fs + { + template + basic_path basic_path:: + leaf () const + { + size_type n (path_.size ()), i (n); + + for (; i > 0; --i) + { + if (path_[i - 1] == '/' || path_[i - 1] == '\\') + break; + } + + return i != 0 ? basic_path (path_.c_str () + i, n - i) : *this; + } + + template + basic_path basic_path:: + directory () const + { + size_type i (path_.size ()); + + for (; i > 0; --i) + { + if (path_[i - 1] == '/' || path_[i - 1] == '\\') + break; + } + + return i != 0 ? basic_path (path_.c_str (), i - 1) : *this; + } + + template + basic_path basic_path:: + base () const + { + size_type i (path_.size ()); + + for (; i > 0; --i) + { + if (path_[i - 1] == '.') + break; + + if (path_[i - 1] == '/' || path_[i - 1] == '\\') + { + i = 0; + break; + } + } + + // Weed out paths like ".txt" and "/.txt" + // + if (i > 1 && path_[i - 2] != '/' && path_[i - 2] != '\\') + { + return basic_path (path_.c_str (), i - 1); + } + else + return *this; + } + + template + basic_path basic_path:: + operator/ (basic_path const& r) + { + if (r.path_.empty ()) + throw invalid_path (); + + basic_path x (*this); + x.path_ += '/'; + x.path_ += r.path_; + return x; + } + + template + void basic_path:: + init (bool internal) + { + if (!internal && path_.empty ()) + throw invalid_path (); + + // Strip trailing slashes. This way empty string represents + // root directory. + // + size_type n (path_.size ()); + for (; n > 0 && (path_[n - 1] == '/' || path_[n - 1] == '\\'); --n) ; + path_.resize (n); + } + } +} -- cgit v1.1