Filesystem.cpp 3.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#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
11
12
13
14
15
16
17
18
19
#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>

20
#include <amdis/Output.hpp>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

template <class... Args>
void inline _ignore_(Args&&...) {}

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(),
47
48
49
50
51
    [this,&relative](auto first, auto end)
    {
      auto token = std::string(first, end);

      if ((!token.empty() && token != "." && token != "..") || (token.empty() && this->empty())) {
52
        this->push_back(token);
53
54
55
56
57
58
59
60
        relative = false;
      } else if (token == "..") {
        if (relative || this->empty()) {
          this->push_back(token);
        }
        else {
          this->pop_back();
        }
61
      }
62
    });
63
64
65
66
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
}


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;
}

116

117
118
119
120
121
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
167
168
169
170
171
172
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:
      error_exit("parent didn't exist. Should not happen, since parent directory created before!");
      return false;
      break;
    case EEXIST:
      return true;
      break;
    default:
      return false;
  }
}

} } // end namespace Dec