Vcsn  2.8
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/filesystem.hpp>
15 #include <boost/tokenizer.hpp>
16 
17 #include <vcsn/misc/escape.hh>
18 
19 namespace vcsn
20 {
21 
23  : std::runtime_error("file not found: " + p.string())
24  {}
25 
26  void
28  {
29  push_current_directory(boost::filesystem::current_path());
30  }
31 
32 
34  {
35  push_cwd();
36  }
37 
39  {
40  push_cwd();
41  // Then only process given path.
42  push_back(p);
43  }
44 
45 
46  file_library::file_library(const std::string& lib, const char* sep)
47  {
48  push_cwd();
49  push_back(lib, sep);
50  }
51 
52  auto
53  file_library::split(const std::string& lib, const char* sep)
54  -> strings_type
55  {
56  using tokenizer = boost::tokenizer<boost::char_separator<char>>;
57  boost::char_separator<char> seps(sep, "", boost::keep_empty_tokens);
58  tokenizer dirs(lib, seps);
60  for (const std::string& s: dirs)
61  res.emplace_back(s);
62  return res;
63  }
64 
67  {
68  search_path_.emplace_back(absolute(p));
69  return *this;
70  }
71 
73  file_library::push_back(const std::string& lib, const char* sep)
74  {
75  for (const std::string& s: split(lib, sep))
76  if (!s.empty())
77  push_back(s);
78  return *this;
79  }
80 
83  {
84  search_path_.emplace_front(absolute(p));
85  return *this;
86  }
87 
89  file_library::push_front(const std::string& lib, const char* sep)
90  {
91  for (const std::string& s: split(lib, sep))
92  if (!s.empty())
93  push_front(s);
94  return *this;
95  }
96 
97  void
99  {
100  current_directory_.push_front(absolute(p));
101  }
102 
103  void
105  {
106  assert(!current_directory_.empty());
107  current_directory_.pop_front();
108  }
109 
110  auto
112  -> path
113  {
114  assert(!current_directory_.empty());
115  return *current_directory_.begin();
116  }
117 
118  auto
119  file_library::find_file(const path& file) const
120  -> path
121  {
122  if (file.is_absolute())
123  {
124  // If file is absolute, just check that it exists.
125  if (!exists(file))
126  {
127  errno = ENOENT;
128  throw not_found(file);
129  }
130  else
131  return file;
132  }
133  // In the current directory?
134  else if (exists(current_directory_get() / file))
135  return current_directory_get() / file;
136  else
137  return find_in_search_path(file);
138  }
139 
140  auto
142  -> path
143  {
144  // Otherwise start scanning the search path.
145  for (path dir: search_path_)
146  {
147  path p = dir / filename;
148  if (exists(p))
149  return p;
150  }
151 
152  // File not found in search path.
153  errno = ENOENT;
154  throw not_found(filename);
155  }
156 
157  std::ostream&
158  file_library::dump(std::ostream& ostr) const
159  {
160  ostr << ".";
161  for (const path& p: search_path_)
162  ostr << ":" << p;
163  return ostr;
164  }
165 }
std::ostream & dump(std::ostream &ostr) const
path find_file(const path &file) const
Search a file.
std::vector< std::string > strings_type
Definition: file-library.hh:96
self_t & push_front(const path &p)
Definition: file-library.cc:82
void pop_current_directory()
path_list_type current_directory_
Current directory stack.
path find_in_search_path(const path &filename) const
Find file filename using include path.
path current_directory_get() const
not_found(const path &file)
Definition: file-library.cc:22
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:53
Definition: a-star.hh:8
void push_cwd()
Push the working directory on the stack.
Definition: file-library.cc:27
void push_current_directory(const path &p)
Definition: file-library.cc:98
path_list_type search_path_
Inclusion path list.
Manage search paths.
Definition: file-library.hh:22
boost::filesystem::path path
Definition: file-library.hh:27
self_t & push_back(const path &p)
Definition: file-library.cc:66
Manage sets of inclusion paths.
STL namespace.
return res
Definition: multiply.hh:399