aboutsummaryrefslogtreecommitdiff
path: root/libcutl/fs
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-12-16 20:29:05 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-02-24 16:40:04 +0300
commit8e761289a2446367267c6c0d9a26e734f0f78306 (patch)
treefb495d8c18801f271d124ee48731f10df396ca89 /libcutl/fs
parent4c8104756b92b9fa16b3a725e8a6aa620dfd606e (diff)
Get rid of legacy build systems and rename cutl/ to libcutl/
Diffstat (limited to 'libcutl/fs')
-rw-r--r--libcutl/fs/auto-remove.cxx30
-rw-r--r--libcutl/fs/auto-remove.hxx82
-rw-r--r--libcutl/fs/exception.cxx16
-rw-r--r--libcutl/fs/exception.hxx36
-rw-r--r--libcutl/fs/path.cxx120
-rw-r--r--libcutl/fs/path.hxx332
-rw-r--r--libcutl/fs/path.ixx70
-rw-r--r--libcutl/fs/path.txx209
8 files changed, 895 insertions, 0 deletions
diff --git a/libcutl/fs/auto-remove.cxx b/libcutl/fs/auto-remove.cxx
new file mode 100644
index 0000000..9aad242
--- /dev/null
+++ b/libcutl/fs/auto-remove.cxx
@@ -0,0 +1,30 @@
+// file : libcutl/fs/auto-remove.cxx
+// license : MIT; see accompanying LICENSE file
+
+#include <cstdio> // std::remove
+#include <cerrno>
+
+#include <libcutl/fs/auto-remove.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ auto_remove::
+ ~auto_remove ()
+ {
+ if (!canceled_)
+ std::remove (path_.string ().c_str ()); // Ignore error.
+ }
+
+ auto_removes::
+ ~auto_removes ()
+ {
+ if (!canceled_)
+ {
+ for (paths::iterator i (paths_.begin ()); i != paths_.end (); ++i)
+ std::remove (i->string ().c_str ()); // Ignore error.
+ }
+ }
+ }
+}
diff --git a/libcutl/fs/auto-remove.hxx b/libcutl/fs/auto-remove.hxx
new file mode 100644
index 0000000..c55f6f9
--- /dev/null
+++ b/libcutl/fs/auto-remove.hxx
@@ -0,0 +1,82 @@
+// file : libcutl/fs/auto-remove.hxx
+// license : MIT; see accompanying LICENSE file
+
+#ifndef LIBCUTL_FS_AUTO_REMOVE_HXX
+#define LIBCUTL_FS_AUTO_REMOVE_HXX
+
+#include <vector>
+
+#include <libcutl/fs/path.hxx>
+#include <libcutl/fs/exception.hxx>
+
+#include <libcutl/export.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ // Remove a file or an empty directory on destruction unless canceled.
+ //
+ struct LIBCUTL_EXPORT auto_remove
+ {
+ explicit
+ auto_remove (path const& p)
+ : path_ (p), canceled_ (false)
+ {
+ }
+
+ ~auto_remove ();
+
+ void
+ cancel ()
+ {
+ canceled_ = true;
+ }
+
+ private:
+ auto_remove (auto_remove const&);
+
+ auto_remove&
+ operator= (auto_remove const&);
+
+ private:
+ path path_;
+ bool canceled_;
+ };
+
+ // Remove a list of file or aempty directories on destruction unless
+ // canceled.
+ //
+ struct LIBCUTL_EXPORT auto_removes
+ {
+ auto_removes (): canceled_ (false) {}
+ ~auto_removes ();
+
+ void
+ add (path const& p)
+ {
+ paths_.push_back (p);
+ }
+
+ void
+ cancel ()
+ {
+ canceled_ = true;
+ }
+
+ private:
+ auto_removes (auto_removes const&);
+
+ auto_removes&
+ operator= (auto_removes const&);
+
+ private:
+ typedef std::vector<path> paths;
+
+ paths paths_;
+ bool canceled_;
+ };
+ }
+}
+
+#endif // LIBCUTL_FS_AUTO_REMOVE_HXX
diff --git a/libcutl/fs/exception.cxx b/libcutl/fs/exception.cxx
new file mode 100644
index 0000000..49fb9fe
--- /dev/null
+++ b/libcutl/fs/exception.cxx
@@ -0,0 +1,16 @@
+// file : libcutl/fs/exception.cxx
+// license : MIT; see accompanying LICENSE file
+
+#include <libcutl/fs/exception.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ char const* error::
+ what () const noexcept
+ {
+ return "filesystem error";
+ }
+ }
+}
diff --git a/libcutl/fs/exception.hxx b/libcutl/fs/exception.hxx
new file mode 100644
index 0000000..511d86f
--- /dev/null
+++ b/libcutl/fs/exception.hxx
@@ -0,0 +1,36 @@
+// file : libcutl/fs/exception.hxx
+// license : MIT; see accompanying LICENSE file
+
+#ifndef LIBCUTL_FS_EXCEPTION_HXX
+#define LIBCUTL_FS_EXCEPTION_HXX
+
+#include <libcutl/exception.hxx>
+
+#include <libcutl/export.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ struct LIBCUTL_EXPORT error: exception
+ {
+ error (int code): code_ (code) {}
+
+ // Error code (errno).
+ //
+ int
+ code () const
+ {
+ return code_;
+ }
+
+ virtual char const*
+ what () const noexcept;
+
+ private:
+ int code_;
+ };
+ }
+}
+
+#endif // LIBCUTL_FS_EXCEPTION_HXX
diff --git a/libcutl/fs/path.cxx b/libcutl/fs/path.cxx
new file mode 100644
index 0000000..c163d8b
--- /dev/null
+++ b/libcutl/fs/path.cxx
@@ -0,0 +1,120 @@
+// file : libcutl/fs/path.cxx
+// license : MIT; see accompanying LICENSE file
+
+#ifdef _WIN32
+# include <stdlib.h> // _MAX_PATH
+# include <direct.h> // _[w]getcwd, _[w]chdir
+#else
+# include <stdlib.h> // mbstowcs, wcstombs
+# include <limits.h> // PATH_MAX
+# include <unistd.h> // getcwd, chdir
+#endif
+
+#ifndef _WIN32
+# ifndef PATH_MAX
+# define PATH_MAX 4096
+# endif
+#endif
+
+#include <libcutl/fs/path.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ char const* invalid_path_base::
+ what () const noexcept
+ {
+ return "invalid filesystem path";
+ }
+
+ //
+ // char
+ //
+
+ template <>
+ LIBCUTL_EXPORT basic_path<char> basic_path<char>::
+ current ()
+ {
+#ifdef _WIN32
+ char cwd[_MAX_PATH];
+ if(_getcwd(cwd, _MAX_PATH) == 0)
+ throw invalid_basic_path<char> (".");
+#else
+ char cwd[PATH_MAX];
+ if (getcwd (cwd, PATH_MAX) == 0)
+ throw invalid_basic_path<char> (".");
+#endif
+
+ return basic_path<char> (cwd);
+ }
+
+ template <>
+ LIBCUTL_EXPORT void basic_path<char>::
+ current (basic_path const& p)
+ {
+ string_type const& s (p.string ());
+
+ if (p.empty ())
+ throw invalid_basic_path<char> (s);
+
+#ifdef _WIN32
+ if(_chdir(s.c_str ()) != 0)
+ throw invalid_basic_path<char> (s);
+#else
+ if (chdir (s.c_str ()) != 0)
+ throw invalid_basic_path<char> (s);
+#endif
+ }
+
+ //
+ // wchar_t
+ //
+
+ template <>
+ LIBCUTL_EXPORT basic_path<wchar_t> basic_path<wchar_t>::
+ current ()
+ {
+#ifdef _WIN32
+ wchar_t wcwd[_MAX_PATH];
+ if(_wgetcwd(wcwd, _MAX_PATH) == 0)
+ throw invalid_basic_path<wchar_t> (L".");
+#else
+ char cwd[PATH_MAX];
+ if (getcwd (cwd, PATH_MAX) == 0)
+ throw invalid_basic_path<wchar_t> (L".");
+
+ wchar_t wcwd[PATH_MAX];
+ if (mbstowcs (wcwd, cwd, PATH_MAX) == size_type (-1))
+ throw invalid_basic_path<wchar_t> (L".");
+#endif
+
+ return basic_path<wchar_t> (wcwd);
+ }
+
+ template <>
+ LIBCUTL_EXPORT void basic_path<wchar_t>::
+ current (basic_path const& p)
+ {
+ string_type const& s (p.string ());
+
+ if (p.empty ())
+ throw invalid_basic_path<wchar_t> (s);
+
+#ifdef _WIN32
+ if(_wchdir(s.c_str ()) != 0)
+ throw invalid_basic_path<wchar_t> (s);
+#else
+ char ns[PATH_MAX + 1];
+
+ if (wcstombs (ns, s.c_str (), PATH_MAX) == size_type (-1))
+ throw invalid_basic_path<wchar_t> (s);
+
+ ns[PATH_MAX] = '\0';
+
+ if (chdir (ns) != 0)
+ throw invalid_basic_path<wchar_t> (s);
+#endif
+ }
+ }
+}
diff --git a/libcutl/fs/path.hxx b/libcutl/fs/path.hxx
new file mode 100644
index 0000000..1d2119c
--- /dev/null
+++ b/libcutl/fs/path.hxx
@@ -0,0 +1,332 @@
+// file : libcutl/fs/path.hxx
+// license : MIT; see accompanying LICENSE file
+
+#ifndef LIBCUTL_FS_PATH_HXX
+#define LIBCUTL_FS_PATH_HXX
+
+#include <string>
+#include <ostream>
+
+#include <libcutl/exception.hxx>
+
+#include <libcutl/export.hxx>
+
+namespace cutl
+{
+ namespace fs
+ {
+ template <typename C>
+ class basic_path;
+
+ template <typename C>
+ struct path_traits
+ {
+ typedef std::basic_string<C> string_type;
+ typedef typename string_type::size_type size_type;
+
+ // Canonical directory and path seperators.
+ //
+#ifdef _WIN32
+ static C const directory_separator = '\\';
+ static C const path_separator = ';';
+#else
+ static C const directory_separator = '/';
+ static C const path_separator = ':';
+#endif
+
+ // Directory separator tests. On some platforms there
+ // could be multiple seperators. For example, on Windows
+ // we check for both '/' and '\'.
+ //
+ static bool
+ is_separator (C c)
+ {
+#ifdef _WIN32
+ return c == '\\' || c == '/';
+#else
+ return c == '/';
+#endif
+ }
+
+ static size_type
+ find_separator (string_type const& s, size_type pos = 0)
+ {
+ for (size_type n (s.size ()); pos < n; ++pos)
+ {
+ if (is_separator (s[pos]))
+ return pos;
+ }
+
+ return string_type::npos;
+ }
+
+ static size_type
+ rfind_separator (string_type const& s, size_type pos = string_type::npos)
+ {
+ if (pos == string_type::npos)
+ pos = s.size ();
+ else
+ pos++;
+
+ for (; pos > 0; --pos)
+ {
+ if (is_separator (s[pos - 1]))
+ return pos - 1;
+ }
+
+ return string_type::npos;
+ }
+
+ static int
+ compare (string_type const& l, string_type const& r)
+ {
+ size_type ln (l.size ()), rn (r.size ()), n (ln < rn ? ln : rn);
+ for (size_type i (0); i != n; ++i)
+ {
+#ifdef _WIN32
+ C lc (tolower (l[i])), rc (tolower (r[i]));
+#else
+ C lc (l[i]), rc (r[i]);
+#endif
+ if (is_separator (lc) && is_separator (rc))
+ continue;
+
+ if (lc < rc) return -1;
+ if (lc > rc) return 1;
+ }
+
+ return ln < rn ? -1 : (ln > rn ? 1 : 0);
+ }
+
+ private:
+#ifdef _WIN32
+ static C
+ tolower (C);
+#endif
+ };
+
+ template <typename C>
+ class invalid_basic_path;
+
+ typedef basic_path<char> path;
+ typedef invalid_basic_path<char> invalid_path;
+
+ typedef basic_path<wchar_t> wpath;
+ typedef invalid_basic_path<wchar_t> invalid_wpath;
+
+ //
+ //
+ class LIBCUTL_EXPORT invalid_path_base: exception
+ {
+ public:
+ virtual char const*
+ what () const noexcept;
+ };
+
+ template <typename C>
+ class invalid_basic_path: public invalid_path_base
+ {
+ public:
+ typedef std::basic_string<C> string_type;
+
+ invalid_basic_path (C const* p): path_ (p) {}
+ invalid_basic_path (string_type const& p): path_ (p) {}
+ ~invalid_basic_path () noexcept {}
+
+ string_type const&
+ path () const
+ {
+ return path_;
+ }
+
+ private:
+ string_type path_;
+ };
+
+ template <typename C>
+ class basic_path
+ {
+ public:
+ typedef std::basic_string<C> string_type;
+ typedef typename string_type::size_type size_type;
+
+ typedef path_traits<C> traits;
+
+ // Construct special empty path.
+ //
+ basic_path ()
+ {
+ }
+
+ explicit
+ basic_path (C const* s)
+ : path_ (s)
+ {
+ init ();
+ }
+
+ basic_path (C const* s, size_type n)
+ : path_ (s, n)
+ {
+ init ();
+ }
+
+ explicit
+ basic_path (string_type const& s)
+ : path_ (s)
+ {
+ init ();
+ }
+
+ void
+ swap (basic_path& p)
+ {
+ path_.swap (p.path_);
+ }
+
+ void
+ clear ()
+ {
+ path_.clear ();
+ }
+
+ static basic_path
+ current ();
+
+ static void
+ current (basic_path const&);
+
+ public:
+ bool
+ empty () const
+ {
+ return path_.empty ();
+ }
+
+ bool
+ absolute () const;
+
+ bool
+ relative () const
+ {
+ return !absolute ();
+ }
+
+ bool
+ root () const;
+
+ public:
+ // Return the path without the directory part.
+ //
+ basic_path
+ leaf () const;
+
+ // Return the directory part of the path or empty path if
+ // there is no directory.
+ //
+ basic_path
+ directory () const;
+
+ // Return the path without the extension, if any.
+ //
+ basic_path
+ base () const;
+
+ public:
+ // Normalize the path. This includes collapsing the '.' and '..'
+ // directories if possible, collapsing multiple directory
+ // separators, and converting all directory separators to the
+ // canonical form. Returns *this.
+ //
+ basic_path&
+ normalize ();
+
+ // Make the path absolute using the current directory unless
+ // it is already absolute.
+ //
+ basic_path&
+ complete ();
+
+ public:
+ basic_path
+ operator/ (basic_path const& x) const
+ {
+ basic_path r (*this);
+ r /= x;
+ return r;
+ }
+
+ basic_path&
+ operator/= (basic_path const&);
+
+ basic_path
+ operator+ (string_type const& s) const
+ {
+ return basic_path (path_ + s);
+ }
+
+ basic_path&
+ operator+= (string_type const& s)
+ {
+ path_ += s;
+ return *this;
+ }
+
+ // Note that comparison is case-insensitive if the filesystem is
+ // not case-sensitive (e.g., Windows).
+ //
+ bool
+ operator== (basic_path const& x) const
+ {
+ return traits::compare (path_, x.path_) == 0;
+ }
+
+ bool
+ operator!= (basic_path const& x) const
+ {
+ return !(*this == x);
+ }
+
+ bool
+ operator< (basic_path const& x) const
+ {
+ return traits::compare (path_, x.path_) < 0;
+ }
+
+ public:
+ const string_type&
+ string () const
+ {
+ return path_;
+ }
+
+ // If possible, return a POSIX representation of the path. For example,
+ // for a Windows path in the form foo\bar this function will return
+ // foo/bar. If it is not possible to create a POSIX representation for
+ // this path (e.g., c:\foo), this function will throw the invalid_path
+ // exception.
+ //
+ string_type
+ posix_string () const;
+
+ private:
+ void
+ init ();
+
+ private:
+ string_type path_;
+ };
+
+ template <typename C>
+ inline std::basic_ostream<C>&
+ operator<< (std::basic_ostream<C>& os, basic_path<C> const& p)
+ {
+ return os << p.string ();
+ }
+ }
+}
+
+#include <libcutl/fs/path.ixx>
+#include <libcutl/fs/path.txx>
+
+#endif // LIBCUTL_FS_PATH_HXX
diff --git a/libcutl/fs/path.ixx b/libcutl/fs/path.ixx
new file mode 100644
index 0000000..461a871
--- /dev/null
+++ b/libcutl/fs/path.ixx
@@ -0,0 +1,70 @@
+// file : libcutl/fs/path.ixx
+// license : MIT; see accompanying LICENSE file
+
+#ifdef _WIN32
+# include <cctype> // std::tolower
+# include <cwctype> // std::towlower
+#endif
+
+namespace cutl
+{
+ namespace fs
+ {
+#ifdef _WIN32
+ template <>
+ inline char path_traits<char>::
+ tolower (char c)
+ {
+ return std::tolower (c);
+ }
+
+ template <>
+ inline wchar_t path_traits<wchar_t>::
+ tolower (wchar_t c)
+ {
+ return std::towlower (c);
+ }
+#endif
+
+ template <typename C>
+ inline bool basic_path<C>::
+ absolute () const
+ {
+#ifdef _WIN32
+ return path_.size () > 1 && path_[1] == ':';
+#else
+ return !path_.empty () && traits::is_separator (path_[0]);
+#endif
+ }
+
+ template <typename C>
+ inline bool basic_path<C>::
+ root () const
+ {
+#ifdef _WIN32
+ return path_.size () == 2 && path_[1] == ':';
+#else
+ return path_.size () == 1 && traits::is_separator (path_[0]);
+#endif
+ }
+
+ template <typename C>
+ inline basic_path<C>& basic_path<C>::
+ complete ()
+ {
+ if (relative ())
+ *this = current () / *this;
+
+ return *this;
+ }
+
+#ifndef _WIN32
+ template <typename C>
+ inline typename basic_path<C>::string_type basic_path<C>::
+ posix_string () const
+ {
+ return string ();
+ }
+#endif
+ }
+}
diff --git a/libcutl/fs/path.txx b/libcutl/fs/path.txx
new file mode 100644
index 0000000..9993c99
--- /dev/null
+++ b/libcutl/fs/path.txx
@@ -0,0 +1,209 @@
+// file : libcutl/fs/path.txx
+// license : MIT; see accompanying LICENSE file
+
+#include <vector>
+
+namespace cutl
+{
+ namespace fs
+ {
+ template <typename C>
+ basic_path<C> basic_path<C>::
+ leaf () const
+ {
+ size_type p (traits::rfind_separator (path_));
+
+ return p != string_type::npos
+ ? basic_path (path_.c_str () + p + 1, path_.size () - p - 1)
+ : *this;
+ }
+
+ template <typename C>
+ basic_path<C> basic_path<C>::
+ directory () const
+ {
+ if (root ())
+ return basic_path ();
+
+ size_type p (traits::rfind_separator (path_));
+
+ // Include the trailing slash so that we get correct behavior
+ // if directory is root.
+ //
+ return p != string_type::npos
+ ? basic_path (path_.c_str (), p + 1)
+ : basic_path ();
+ }
+
+ template <typename C>
+ basic_path<C> basic_path<C>::
+ base () const
+ {
+ size_type i (path_.size ());
+
+ for (; i > 0; --i)
+ {
+ if (path_[i - 1] == '.')
+ break;
+
+ if (traits::is_separator (path_[i - 1]))
+ {
+ i = 0;
+ break;
+ }
+ }
+
+ // Weed out paths like ".txt" and "/.txt"
+ //
+ if (i > 1 && !traits::is_separator (path_[i - 2]))
+ {
+ return basic_path (path_.c_str (), i - 1);
+ }
+ else
+ return *this;
+ }
+
+#ifdef _WIN32
+ template <typename C>
+ typename basic_path<C>::string_type basic_path<C>::
+ posix_string () const
+ {
+ if (absolute ())
+ throw invalid_basic_path<C> (path_);
+
+ string_type r (path_);
+
+ // Translate Windows-style separators to the POSIX ones.
+ //
+ for (size_type i (0), n (r.size ()); i != n; ++i)
+ if (r[i] == '\\')
+ r[i] = '/';
+
+ return r;
+ }
+#endif
+
+ template <typename C>
+ basic_path<C>& basic_path<C>::
+ operator/= (basic_path<C> const& r)
+ {
+ if (r.absolute ())
+ throw invalid_basic_path<C> (r.path_);
+
+ if (path_.empty () || r.path_.empty ())
+ {
+ path_ += r.path_;
+ return *this;
+ }
+
+ if (!traits::is_separator (path_[path_.size () - 1]))
+ path_ += traits::directory_separator;
+
+ path_ += r.path_;
+
+ return *this;
+ }
+
+ template <typename C>
+ basic_path<C>& basic_path<C>::
+ normalize ()
+ {
+ if (empty ())
+ return *this;
+
+ bool abs (absolute ());
+
+ typedef std::vector<string_type> paths;
+ paths ps;
+
+ for (size_type b (0), e (traits::find_separator (path_)),
+ n (path_.size ());;
+ e = traits::find_separator (path_, b))
+ {
+ string_type s (path_, b, e == string_type::npos ? e : e - b);
+ ps.push_back (s);
+
+ if (e == string_type::npos)
+ break;
+
+ ++e;
+
+ while (e < n && traits::is_separator (path_[e]))
+ ++e;
+
+ if (e == n)
+ break;
+
+ b = e;
+ }
+
+ // First collapse '.' and '..'.
+ //
+ paths r;
+
+ for (typename paths::const_iterator i (ps.begin ()), e (ps.end ());
+ i != e; ++i)
+ {
+ string_type const& s (*i);
+ size_type n (s.size ());
+
+ if (n == 1 && s[0] == '.')
+ continue;
+
+ if (n == 2 && s[0] == '.' && s[1] == '.')
+ {
+ // Pop the last directory from r unless it is '..'.
+ //
+ if (!r.empty ())
+ {
+ string_type const& s1 (r.back ());
+
+ if (!(s1.size () == 2 && s1[0] == '.' && s1[1] == '.'))
+ {
+ // Cannot go past the root directory.
+ //
+ if (abs && r.size () == 1)
+ throw invalid_basic_path<C> (path_);
+
+ r.pop_back ();
+ continue;
+ }
+ }
+ }
+
+ r.push_back (s);
+ }
+
+ // Reassemble the path.
+ //
+ string_type p;
+
+ for (typename paths::const_iterator i (r.begin ()), e (r.end ());
+ i != e;)
+ {
+ p += *i;
+
+ if (++i != e)
+ p += traits::directory_separator;
+ }
+
+ if (p.empty () && !r.empty ())
+ p += traits::directory_separator; // Root directory.
+
+ path_.swap (p);
+ return *this;
+ }
+
+ template <typename C>
+ void basic_path<C>::
+ init ()
+ {
+ // Strip trailing slashes except for the case where the single
+ // slash represents the root directory.
+ //
+ size_type n (path_.size ());
+ for (; n > 1 && traits::is_separator (path_[n - 1]); --n) ;
+ path_.resize (n);
+ }
+ }
+}