libfoedus-core
FOEDUS Core Library
path.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #include <dirent.h>
19 
20 #include <ostream>
21 #include <string>
22 #include <vector>
23 
24 #include "foedus/fs/filesystem.hpp"
25 #include "foedus/fs/path.hpp"
26 
27 namespace foedus {
28 namespace fs {
29 Path::Path(const std::string& s) {
30  if (s.size() > 0 && s.at(0) == '~'
31  && (s.size() == 1 || s.at(1) == '/')) {
32  // starts with ~/ or ~ alone: resolve it as home folder
33  pathname_ = home_path().pathname_;
34  pathname_.append(s.substr(1));
35  } else {
36  if (s.empty() || s.at(0) == kPreferredSeparator) {
37  pathname_ = s;
38  } else {
39  Path tmp = current_path();
40  tmp /= s;
41  pathname_ = tmp.string();
42  }
43  }
44 }
45 
47  size_t pos = pathname_.find_last_of(kPreferredSeparator);
48  if (pos == pathname_.size()) {
49  return Path(pathname_);
50  } else {
51  return Path(pathname_.c_str() + pos);
52  }
53 }
54 
56  size_t pos = pathname_.find_last_of(kPreferredSeparator);
57  if (pos == pathname_.size()) {
58  return Path();
59  } else {
60  return Path(pathname_.substr(0, pos));
61  }
62 }
63 
64 std::vector< Path > Path::child_paths() const {
65  std::vector< Path > children;
66  if (is_directory(*this)) {
67  DIR *d = ::opendir(c_str());
68  if (d) {
69  for (dirent *e = ::readdir(d); e != nullptr; e = ::readdir(d)) {
70  if (e->d_name == std::string(".") || e->d_name == std::string("..")) {
71  continue;
72  }
73  Path child(*this);
74  child /= std::string(e->d_name);
75  children.emplace_back(child);
76  }
77  ::closedir(d);
78  }
79  }
80  return children;
81 }
82 
83 
84 std::ostream& operator<<(std::ostream& o, const Path& v) {
85  o << v.string();
86  return o;
87 }
88 
89 } // namespace fs
90 } // namespace foedus
91 
std::ostream & operator<<(std::ostream &o, const DirectIoFile &v)
bool is_directory(const Path &p)
Returns if the file is a directory.
Definition: filesystem.hpp:133
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
Path home_path()
Returns the absolute path of the home directory of the user running this process. ...
Definition: filesystem.cpp:80
Path current_path()
Returns the current working directory.
Definition: filesystem.cpp:66
std::vector< Path > child_paths() const
Definition: path.cpp:64
Analogue of boost::filesystem::path.
Definition: path.hpp:37
const std::string & string() const
Definition: path.hpp:65
Path parent_path() const
Definition: path.cpp:55
static const char kPreferredSeparator
Definition: path.hpp:39
const char * c_str() const
Definition: path.hpp:64
Path filename() const
Definition: path.cpp:46