-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFileUtilities.cpp
180 lines (149 loc) · 4.19 KB
/
FileUtilities.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
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
173
174
175
176
177
178
179
180
// Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and
// other Axom Project Developers. See the top-level LICENSE file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
#include "axom/core/utilities/FileUtilities.hpp"
#include <string>
#include <fstream>
#include <sstream>
#include <cerrno>
#include <cstdio> // defines FILENAME_MAX
#ifdef WIN32
#include <direct.h>
#include <sys/stat.h>
#define GetCurrentDir _getcwd
#define ChangeCurrentDir _chdir
#define Stat _stat
#define Unlink _unlink
#else
#include <unistd.h> // for getcwd
#include <sys/stat.h> // for stat
#define GetCurrentDir getcwd
#define ChangeCurrentDir chdir
#define Stat stat
#define Unlink unlink
#endif
// Note: The hard-wired path separator character in this file
// should be set to the backslash when on Windows.
namespace axom
{
namespace utilities
{
namespace filesystem
{
std::string getCWD()
{
char cCurrentPath[FILENAME_MAX];
if(!GetCurrentDir(cCurrentPath, FILENAME_MAX))
{
//Note: Cannot use logging in COMMON component -- topic of JIRA issue
// ATK-463
//SLIC_WARNING("Common::Could not find cwd");
return std::string("./");
}
return std::string(cCurrentPath);
}
int changeCWD(const std::string& dirName)
{
return ChangeCurrentDir(dirName.c_str());
}
//-----------------------------------------------------------------------------
bool pathExists(const std::string& fileName)
{
// Uses system's stat() function.
// Return code 0 indicates file exists
struct Stat buffer;
return (Stat(fileName.c_str(), &buffer) == 0);
}
//-----------------------------------------------------------------------------
std::string joinPath(const std::string& fileDir,
const std::string& fileName,
const std::string& separator)
{
// Check if we need to add a separator
bool pathNeedsSep =
!fileDir.empty() && (fileDir[fileDir.size() - 1] != separator[0]);
// Concatenate the path with the fileName to create the full path
std::stringstream fullFileNameStream;
fullFileNameStream << fileDir << (pathNeedsSep ? separator : "") << fileName;
return fullFileNameStream.str();
}
//-----------------------------------------------------------------------------
int makeDirsForPath(const std::string& path)
{
char separator = '/';
std::string::size_type pos = 0;
int err = 0;
do
{
pos = path.find(separator, pos + 1);
std::string dir_name = path.substr(0, pos);
#ifdef WIN32
err = _mkdir(dir_name.c_str());
#else
mode_t mode = 0777; // rwx permissions for everyone
err = mkdir(dir_name.c_str(), mode);
#endif
err = (err && (errno != EEXIST)) ? 1 : 0;
} while(pos != std::string::npos);
return err;
}
//-----------------------------------------------------------------------------
std::string prefixRelativePath(const std::string& path, const std::string& prefix)
{
if(path.empty())
{
throw std::invalid_argument("path must not be empty");
};
if(path[0] == '/' || prefix.empty())
{
return path;
}
return utilities::filesystem::joinPath(prefix, path);
}
//-----------------------------------------------------------------------------
std::string getParentPath(const std::string& path)
{
if(path.empty())
{
throw std::invalid_argument("path must not be empty");
};
char separator = '/';
std::string parent;
if(path.size() == 1 && path[0] == separator)
{
// path is root, so parent is blank.
}
else
{
std::size_t found = path.rfind(separator);
if(found != std::string::npos)
{
if(found == 0)
{
++found;
}
parent = path.substr(0, found);
}
}
return parent;
}
//-----------------------------------------------------------------------------
void getDirName(std::string& dir, const std::string& path)
{
char separator = '/';
std::size_t found = path.rfind(separator);
if(found != std::string::npos)
{
dir = path.substr(0, found);
}
else
{
dir = "";
}
}
//-----------------------------------------------------------------------------
int removeFile(const std::string& filename) { return Unlink(filename.c_str()); }
} // end namespace filesystem
} // end namespace utilities
} // end namespace axom