summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/file-library.hh
blob: f59757f46905aef9a4425f0af678363fb5637d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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>