Filesystem.cpp 3.31 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
#include <config.h>
2

3
4
5
6
7
8
9
10
11
12
#include "Filesystem.hpp"

#ifdef _WIN32
  #include <direct.h>   // _mkdir
  #define GET_CURRENT_DIR _getcwd
#else
  #include <unistd.h>
  #define GET_CURRENT_DIR getcwd
#endif

Praetorius, Simon's avatar
Praetorius, Simon committed
13
14
15
16
17
18
19
20
21
#include <errno.h>    // errno, ENOENT, EEXIST
#include <stdio.h>  // defines FILENAME_MAX
#include <sys/stat.h>
#include <sys/types.h>

#include <cctype>
#include <fstream>
#include <string>

22
23
#include <amdis/common/Algorithm.hpp>
#include <amdis/common/String.hpp>
24

25
26
27
28
29

namespace {
  template <class... Args>
  void inline _ignore_(Args&&...) {}
}
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

namespace AMDiS { namespace filesystem {

std::string path::string() const
{
  if (empty())
    return "";

  auto it = begin();
  auto result = *it;
  for (++it; it != end(); ++it)
    result += preferred_separator + *it;
  return result;
}


void path::split(std::string p)
{
  std::string separators = "/\\";
  bool relative = true;

  trim(p);
  AMDiS::split(p.begin(), p.end(), separators.begin(), separators.end(),
53
54
55
56
57
    [this,&relative](auto first, auto end)
    {
      auto token = std::string(first, end);

      if ((!token.empty() && token != "." && token != "..") || (token.empty() && this->empty())) {
58
        this->push_back(token);
59
60
61
62
63
64
65
66
        relative = false;
      } else if (token == "..") {
        if (relative || this->empty()) {
          this->push_back(token);
        }
        else {
          this->pop_back();
        }
67
      }
68
    });
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
}


path path::stem() const
{
  auto f = filename().string();
  auto pos = f.find_last_of('.');
  if (f == "." || f == ".." || pos == std::string::npos)
    return {f};
  else
    return {f.substr(0,pos)};
}


path path::extension() const
{
  auto f = filename().string();
  auto pos = f.find_last_of('.');
  if (f == "." || f == ".." || pos == std::string::npos)
    return {};
  else
    return {f.substr(pos)};
}


bool path::is_absolute(std::string p)
{
  if (p[0] == '/')
    return true;

  // c:\ or z:/
  if (std::isalpha(p[0]) && p[1] == ':' && (p[2] == '/' || p[2] == '\\'))
    return true;

  return false;
}


path& path::operator/=(path const& p)
{
  insert(end(), p.begin(), p.end());
  original += preferred_separator + p.original;
  return *this;
}


bool path::is_file() const
{
  std::string p = this->string();
  struct stat info;
  return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFREG) != 0;
}

122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
bool path::is_directory() const
{
  std::string p = this->string();
  struct stat info;
  return stat(p.c_str(), &info) == 0 && (info.st_mode & S_IFDIR) != 0;
}


path current_path()
{
  char cwd_[FILENAME_MAX];
  _ignore_(GET_CURRENT_DIR(cwd_, sizeof(cwd_)));
  std::string cwd(cwd_);
  return { trim(cwd) };
}


bool exists(path const& p)
{
  return p.is_file() || p.is_directory();
}


bool create_directories(path const& p)
{
  if (p.is_directory())
    return true;

  auto parent = p.parent_path();
  if (!parent.empty() && !parent.is_directory())
    create_directories(parent);

#ifdef _WIN32
  int ret = _mkdir(p.string().c_str());
#else
  mode_t mode = 0755;
  int ret = mkdir(p.string().c_str(), mode);
#endif
  if (ret == 0)
    return true;

  switch (errno)
  {
    case ENOENT:
167
      throw std::runtime_error("parent didn't exist. Should not happen, since parent directory created before!");
168
169
170
171
172
173
174
175
176
177
178
      return false;
      break;
    case EEXIST:
      return true;
      break;
    default:
      return false;
  }
}

} } // end namespace Dec