39 native_path = windows_path
41 native_path = posix_path
45 path_impl() : m_type(native_path), m_absolute(
false) { }
47 path_impl(
const path_impl &path)
48 : m_type(path.m_type), m_path(path.m_path), m_absolute(path.m_absolute) {}
50 path_impl(path_impl &&path)
51 : m_type(path.m_type), m_path(std::move(path.m_path)),
52 m_absolute(path.m_absolute) {}
54 path_impl(
const char *
string) { set(
string); }
56 path_impl(
const std::string &
string) { set(
string); }
59 path_impl(
const std::wstring &wstring) { set(wstring); }
60 path_impl(
const wchar_t *wstring) { set(wstring); }
63 size_t length()
const {
return m_path.size(); }
65 bool empty()
const {
return m_path.empty(); }
67 bool is_absolute()
const {
return m_absolute; }
69 path_impl make_absolute()
const;
73 size_t file_size()
const;
75 bool is_directory()
const;
79 std::string extension()
const {
80 const std::string &name = filename();
81 size_t pos = name.find_last_of(
".");
82 if (pos == std::string::npos)
86 if( name ==
"." || name ==
".." )
89 return name.substr(pos);
92 std::string filename()
const {
95 const std::string &last = m_path[m_path.size()-1];
99 path_impl parent_path()
const {
101 result.m_absolute = m_absolute;
103 if (m_path.empty()) {
105 result.m_path.push_back(
"..");
107 size_t until = m_path.size() - 1;
108 for (
size_t i = 0; i < until; ++i)
109 result.m_path.push_back(m_path[i]);
114 path_impl operator/(
const path_impl &other)
const {
115 if (other.m_absolute)
116 throw std::runtime_error(
"path::operator/(): expected a relative path!");
117 if (m_type != other.m_type)
118 throw std::runtime_error(
"path::operator/(): expected a path of the same type!");
120 path_impl result(*
this);
122 for (
size_t i=0; i<other.m_path.size(); ++i)
123 result.m_path.push_back(other.m_path[i]);
128 std::string str(path_type type = native_path)
const {
129 std::ostringstream oss;
131 if (m_type == posix_path && m_absolute)
134 for (
size_t i=0; i<m_path.size(); ++i) {
136 if (i+1 < m_path.size()) {
137 if (type == posix_path)
147 void set(
const std::string &str, path_type type = native_path) {
149 if (type == windows_path) {
150 m_path = tokenize(str,
"/\\");
151 m_absolute = str.size() >= 2 && std::isalpha(str[0]) && str[1] ==
':';
153 m_path = tokenize(str,
"/");
154 m_absolute = !str.empty() && str[0] ==
'/';
158 path_impl &operator=(
const path_impl &path) {
159 m_type = path.m_type;
160 m_path = path.m_path;
161 m_absolute = path.m_absolute;
165 path_impl &operator=(path_impl &&path) {
167 m_type = path.m_type;
168 m_path = std::move(path.m_path);
169 m_absolute = path.m_absolute;
174 friend std::ostream &operator<<(std::ostream &os,
const path_impl &path) {
181 bool resize_file(
size_t target_length );
183 static path_impl getcwd();
186 std::wstring wstr( path_type type = native_path )
const;
187 void set(
const std::wstring &wstring, path_type type = native_path );
188 path_impl &operator=(
const std::wstring &str) { set(str);
return *
this; }
191 bool operator==(
const path_impl &p)
const {
return p.m_path == m_path; }
192 bool operator!=(
const path_impl &p)
const {
return p.m_path != m_path; }
195 static std::vector<std::string> tokenize(
const std::string &
string,
const std::string &delim) {
196 std::string::size_type lastPos = 0, pos =
string.find_first_of(delim, lastPos);
197 std::vector<std::string> tokens;
199 while (lastPos != std::string::npos) {
201 tokens.push_back(
string.substr(lastPos, pos - lastPos));
203 if (lastPos == std::string::npos || lastPos + 1 ==
string.length())
205 pos =
string.find_first_of(delim, ++lastPos);
213 std::vector<std::string> m_path;
217 bool create_directory(
const path_impl& p );
Simple class for manipulating paths on Linux/Windows/Mac OS.
Definition: path.h:33