Vcsn  2.3
Be Rational
path.cc
Go to the documentation of this file.
1 
6 #include <cctype>
7 #include <iostream>
8 #include <cstdio>
9 #include <fcntl.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <sys/param.h> // MAXPATHLEN
13 #include <unistd.h>
14 
15 #include <boost/tokenizer.hpp>
16 
17 #include <vcsn/misc/path.hh>
18 
19 namespace vcsn
20 {
21  path::path(const std::string& p)
22  : value_(p)
23  {}
24 
25  path::path(const char* p)
26  : path(std::string(p))
27  {}
28 
29  path
31  {
32  // Store the working directory
33  char res[MAXPATHLEN + 1];
34 
35  if (!getcwd(res, MAXPATHLEN + 1))
36  throw std::runtime_error("working directory name too long");
37 
38  return {res};
39  }
40 
41  path&
42  path::operator/=(const path& rhs)
43  {
44  // if (rhs.absolute_get())
45  // throw invalid_path(
46  // "Rhs of concatenation is absolute: " + rhs.string());
47  value_ += "/" + rhs.value_;
48  return *this;
49  }
50 
51  path
52  path::operator/(const path& rhs) const
53  {
54  path res = *this;
55  return res /= rhs;
56  }
57 
58  bool
59  path::operator==(const path& rhs) const
60  {
61  return value_ == rhs.value_;
62  }
63 
64  std::ostream&
65  path::dump(std::ostream& o) const
66  {
67  return o << string();
68  }
69 
72  {
73  using tokenizer = boost::tokenizer<boost::char_separator<char>>;
74  boost::char_separator<char> seps("/", "", boost::keep_empty_tokens);
75  tokenizer dirs(value_, seps);
76 
77  path_type res;
78  for (const std::string& s: dirs)
79  res.emplace_back(s);
80  return res;
81  }
82 
84  {
85  auto slash = value_.rfind('/');
86  if (slash == value_.npos || slash == 0)
87  return ".";
88  else
89  return {value_.substr(0, slash - 1)};
90  }
91 
93  {
94  auto slash = value_.rfind('/');
95  return {value_.substr(slash + 1)};
96  }
97 
98  bool exists(const path& p)
99  {
100  struct stat buf;
101  return 0 == stat (p.c_str(), &buf);
102  }
103 
104  path absolute(const path& p)
105  {
106  if (p.is_absolute())
107  return p;
108  else
109  return path::cwd() / p;
110  }
111 
112 }
const std::string & string() const
Definition: path.hxx:62
STL namespace.
path absolute(const path &p)
Definition: path.cc:104
bool operator==(const path &rhs) const ATTRIBUTE_PURE
Definition: path.cc:59
static path cwd()
Return the current working directory.
Definition: path.cc:30
const char * c_str() const
Definition: path.hxx:55
path parent_path() const
Return a path which is the parent directory.
Definition: path.cc:83
bool exists(const path &p)
Definition: path.cc:98
return res
Definition: multiply.hh:398
Declaration of vcsn::path.
path(const std::string &p)
Init object with path.
Definition: path.cc:21
std::ostream & dump(std::ostream &ostr) const
Definition: path.cc:65
Paths in filesystems, i.e., file names.
Definition: path.hh:21
path operator/(const path &rhs) const
Definition: path.cc:52
Definition: a-star.hh:8
path filename() const
Also known as basename.
Definition: path.cc:92
path_type components() const
Definition: path.cc:71
std::list< std::string > path_type
Definition: path.hh:83
bool is_absolute() const
Whether starts with /.
Definition: path.hxx:48
path & operator/=(const path &rhs)
Definition: path.cc:42
value_type value_
Wrapped value.
Definition: path.hh:95