43#if __has_include(<filesystem>)
45namespace fs = std::filesystem;
46#elif __has_include(<experimental/filesystem>)
47#include <experimental/filesystem>
48namespace fs = std::experimental::filesystem;
50#error "No support for filesystem library"
55#include <mach-o/dyld.h>
59#include <sys/sysctl.h>
68#define WIN32_LEAN_AND_MEAN
104std::tuple<std::list<std::string>,
bool>
108 std::list<std::string> files;
109 if (!fs::exists(
path))
111 return std::make_tuple(files,
true);
113 for (
auto& it : fs::directory_iterator(
path))
115 if (!fs::is_directory(it.path()))
117 files.push_back(it.path().filename().string());
120 return std::make_tuple(files,
false);
142 std::list<std::string> elements =
Split(
path);
143 std::list<std::string>::const_iterator last = elements.end();
145 return Join(elements.begin(), last);
161 std::string filename;
162#if defined(__linux__)
165 char* buffer = (
char*)malloc(size);
166 memset(buffer, 0, size);
170 status = readlink(
"/proc/self/exe", buffer, size);
171 if (status != 1 || (status == -1 && errno != ENAMETOOLONG))
177 buffer = (
char*)malloc(size);
178 memset(buffer, 0, size);
187#elif defined(__WIN32__)
191 LPTSTR lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
192 DWORD status = GetModuleFileName(
nullptr, lpFilename, size);
193 while (status == size)
197 lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
198 status = GetModuleFileName(
nullptr, lpFilename, size);
201 filename = lpFilename;
204#elif defined(__APPLE__)
207 char* buffer = (
char*)malloc(bufsize);
209 int status = _NSGetExecutablePath(buffer, &bufsize);
213 buffer = (
char*)malloc(bufsize);
214 status = _NSGetExecutablePath(buffer, &bufsize);
220#elif defined(__FreeBSD__)
223 std::size_t bufSize = 1024;
224 char* buf = (
char*)malloc(bufSize);
228 mib[2] = KERN_PROC_PATHNAME;
231 sysctl(mib, 4, buf, &bufSize,
nullptr, 0);
239Append(std::string left, std::string right)
246 if (lastSep != left.size() - 1)
250 left = left.substr(0, left.size() - 1);
256std::list<std::string>
260 std::list<std::string> retval;
261 std::string::size_type current = 0;
262 std::string::size_type next = 0;
264 while (next != std::string::npos)
266 std::string item =
path.substr(current, next - current);
267 retval.push_back(item);
271 std::string item =
path.substr(current, next - current);
272 retval.push_back(item);
277Join(std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
280 std::string retval =
"";
281 for (std::list<std::string>::const_iterator i = begin; i != end; i++)
300std::list<std::string>
305 std::list<std::string> files;
318 char*
path =
nullptr;
320 path = std::getenv(
"TMP");
321 if (!
path || std::strlen(
path) == 0)
323 path = std::getenv(
"TEMP");
324 if (!
path || std::strlen(
path) == 0)
326 path =
const_cast<char*
>(
"/tmp");
334 time_t now = time(
nullptr);
335 struct tm* tm_now = localtime(&now);
340 srand(time(
nullptr));
354 std::ostringstream oss;
355 oss <<
path <<
SYSTEM_PATH_SEP <<
"ns-3." << tm_now->tm_hour <<
"." << tm_now->tm_min <<
"."
356 << tm_now->tm_sec <<
"." << n;
367 if (!fs::exists(
path))
369 fs::create_directories(
path, ec);
385 std::list<std::string> files;
397 std::string
file = tokens.back();
410 auto it = std::find(files.begin(), files.end(),
file);
411 if (it == files.end())
430 std::regex incompatible_characters(
" |:[^\\\\]|<|>|\\*");
431 std::string valid_path;
432 std::regex_replace(std::back_inserter(valid_path),
435 incompatible_characters,
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
NS_FATAL_x macro definitions.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
std::list< std::string > ReadFiles(std::string path)
Get the list of files located in a file system directory.
bool Exists(const std::string path)
Check if a path exists.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
std::string Dirname(std::string path)
Get the directory path for a file.
void MakeDirectories(std::string path)
Create all the directories leading to path.
std::string MakeTemporaryDirectoryName()
Get the name of a temporary directory.
std::string Append(std::string left, std::string right)
Join two file system path elements.
std::string Join(std::list< std::string >::const_iterator begin, std::list< std::string >::const_iterator end)
Join a list of file system path directories into a single file system path.
std::string CreateValidSystemPath(const std::string path)
Replace incompatible characters in a path, to get a path compatible with different file systems.
std::tuple< std::list< std::string >, bool > ReadFilesNoThrow(std::string path)
Get the list of files located in a file system directory with error.
std::string FindSelfDirectory()
Get the file system path to the current executable.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
constexpr auto SYSTEM_PATH_SEP
System-specific path separator used between directory names.
ns3::SystemPath declarations.