summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/file-library.hh
diff options
context:
space:
mode:
Diffstat (limited to 'tiger-compiler/lib/misc/file-library.hh')
-rw-r--r--tiger-compiler/lib/misc/file-library.hh95
1 files changed, 95 insertions, 0 deletions
diff --git a/tiger-compiler/lib/misc/file-library.hh b/tiger-compiler/lib/misc/file-library.hh
new file mode 100644
index 0000000..f59757f
--- /dev/null
+++ b/tiger-compiler/lib/misc/file-library.hh
@@ -0,0 +1,95 @@
+/**
+ ** \file misc/file-library.hh
+ ** \brief Manage sets of inclusion paths.
+ */
+
+#pragma once
+
+#include <filesystem>
+#include <string>
+#include <vector>
+
+namespace misc
+{
+ using path = std::filesystem::path;
+
+ /** \brief Manage search path.
+
+ Store search path and all informations used for handling
+ paths when processing import directives. */
+
+ class file_library
+ {
+ public:
+ /// \name Constructor.
+ /// \{
+ /// An empty library.
+ file_library();
+ /// Init the library with one path.
+ file_library(path p);
+ /// \}
+
+ /// \name Managing inclusion paths.
+ /// \{
+ void append_dir(path p);
+ void prepend_dir(path p);
+ /// \}
+
+ /// \name current directory.
+ /// \{
+ void push_current_directory(path p);
+ void pop_current_directory();
+ path current_directory_get() const;
+ /// \}
+
+ /// \name Findind file
+ /// \{
+ /** \brief Search a file.
+
+ Determine real path of \a file, by looking in search path if
+ necessary.
+
+ \return Directory containing \a file, or "" if not found. */
+ path find_file(const std::string& file);
+
+ /// \brief Check if \a file exists in directory \a dir.
+ bool find_in_directory(const path& dir, const std::string& file) const;
+ /// \}
+
+ /// \name Printing.
+ /// \{
+ std::ostream& dump(std::ostream& ostr) const;
+ /// \}
+
+ private:
+ /// Push the working directory on the stack.
+ void push_cwd();
+
+ /** \brief Find file "\a relative_path / \a filename" using include path.
+ \return Absolute path where the file lies or empty path
+ if the file does not exist. */
+ path find_in_search_path(const path& relative_path,
+ const std::string& filename) const;
+
+ /** \brief Split the string with character ':', and insert each
+ resultant string as a new search path. */
+ void append_dir_list(std::string path_list);
+
+ /** \brief Ensure that path is absolute by prepending current
+ directory if necessary */
+ path ensure_absolute_path(path p) const;
+
+ using path_list_type = std::vector<path>;
+
+ /// Inclusion path list.
+ path_list_type search_path_;
+
+ /// Current directory stack.
+ path_list_type current_directory_;
+ };
+
+ std::ostream& operator<<(std::ostream& ostr, const file_library& l);
+
+} // namespace misc
+
+#include <misc/file-library.hxx>