Vcsn  2.2a
Be Rational
file-library.cc
Go to the documentation of this file.
1 
7 
8 #include <cassert>
9 #include <cstdlib>
10 #include <iostream>
11 #include <stdexcept>
12 #include <unistd.h>
13 
14 #include <boost/tokenizer.hpp>
15 
16 #include <vcsn/misc/escape.hh>
17 
18 namespace vcsn
19 {
20 
22  : std::runtime_error("file not found: " + p.string())
23  {}
24 
25  void
27  {
29  }
30 
31 
33  {
34  push_cwd();
35  }
36 
38  {
39  push_cwd();
40  // Then only process given path.
41  push_back(p);
42  }
43 
44 
45  file_library::file_library(const std::string& lib, const char* sep)
46  {
47  push_cwd();
48  push_back(lib, sep);
49  }
50 
51  auto
52  file_library::split(const std::string& lib, const char* sep)
53  -> strings_type
54  {
55  using tokenizer = boost::tokenizer<boost::char_separator<char>>;
56  boost::char_separator<char> seps(sep, "", boost::keep_empty_tokens);
57  tokenizer dirs(lib, seps);
58  strings_type res;
59  for (const std::string& s: dirs)
60  res.emplace_back(s);
61  return res;
62  }
63 
66  {
67  search_path_.emplace_back(absolute(p));
68  return *this;
69  }
70 
72  file_library::push_back(const std::string& lib, const char* sep)
73  {
74  for (const std::string& s: split(lib, sep))
75  if (!s.empty())
76  push_back(s);
77  return *this;
78  }
79 
82  {
83  search_path_.emplace_front(absolute(p));
84  return *this;
85  }
86 
88  file_library::push_front(const std::string& lib, const char* sep)
89  {
90  for (const std::string& s: split(lib, sep))
91  if (!s.empty())
92  push_front(s);
93  return *this;
94  }
95 
96  void
98  {
99  current_directory_.push_front(absolute(p));
100  }
101 
102  void
104  {
105  assert(!current_directory_.empty());
106  current_directory_.pop_front();
107  }
108 
109  auto
111  -> path
112  {
113  assert(!current_directory_.empty());
114  return *current_directory_.begin();
115  }
116 
117  auto
118  file_library::find_file(const path& file) const
119  -> path
120  {
121  if (file.is_absolute())
122  {
123  // If file is absolute, just check that it exists.
124  if (!exists(file))
125  {
126  errno = ENOENT;
127  throw not_found(file);
128  }
129  else
130  return file;
131  }
132  // In the current directory?
133  else if (exists(current_directory_get() / file))
134  return current_directory_get() / file;
135  else
136  return find_in_search_path(file);
137  }
138 
139  auto
141  -> path
142  {
143  // Otherwise start scanning the search path.
144  for (path dir: search_path_)
145  {
146  path p = dir / filename;
147  if (exists(p))
148  return p;
149  }
150 
151  // File not found in search path.
152  errno = ENOENT;
153  throw not_found(filename);
154  }
155 
156  std::ostream&
157  file_library::dump(std::ostream& ostr) const
158  {
159  ostr << ".";
160  for (const path& p: search_path_)
161  ostr << ":" << p;
162  return ostr;
163  }
164 }
path_list_type current_directory_
Current directory stack.
path find_in_search_path(const path &filename) const
Find file filename using include path.
void push_current_directory(const path &p)
Definition: file-library.cc:97
path current_directory_get() const
static path cwd()
Return the current working directory.
Definition: path.cc:30
path_list_type search_path_
Inclusion path list.
Manage search paths.
Definition: file-library.hh:22
void pop_current_directory()
void push_cwd()
Push the working directory on the stack.
Definition: file-library.cc:26
self_t & push_front(const path &p)
Definition: file-library.cc:81
std::ostream & dump(std::ostream &ostr) const
Paths in filesystems, i.e., file names.
Definition: path.hh:21
static strings_type split(const std::string &lib, const char *sep)
Split lib at each occurrence of sep, return the list of components.
Definition: file-library.cc:52
bool exists(const path &p)
Definition: path.cc:98
path absolute(const path &p)
Definition: path.cc:104
path find_file(const path &file) const
Search a file.
STL namespace.
Exception thrown when a file cannot be located.
Definition: file-library.hh:32
self_t & push_back(const path &p)
Definition: file-library.cc:65
std::list< std::string > strings_type
Definition: file-library.hh:96
not_found(const path &file)
Definition: file-library.cc:21
Manage sets of inclusion paths.
Definition: a-star.hh:8