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>
69#define WIN32_LEAN_AND_MEAN
105std::tuple<std::list<std::string>,
bool>
109 std::list<std::string> files;
110 if (!fs::exists(path))
112 return std::make_tuple(files,
true);
114 for (
auto& it : fs::directory_iterator(path))
116 if (!fs::is_directory(it.path()))
118 files.push_back(it.path().filename().string());
121 return std::make_tuple(files,
false);
143 std::list<std::string> elements =
Split(path);
144 std::list<std::string>::const_iterator last = elements.end();
146 return Join(elements.begin(), last);
162 std::string filename;
163#if defined(__linux__)
166 char* buffer = (
char*)malloc(size);
167 memset(buffer, 0, size);
171 status = readlink(
"/proc/self/exe", buffer, size);
172 if (status != 1 || (status == -1 && errno != ENAMETOOLONG))
178 buffer = (
char*)malloc(size);
179 memset(buffer, 0, size);
188#elif defined(__WIN32__)
192 LPTSTR lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
193 DWORD status = GetModuleFileName(
nullptr, lpFilename, size);
194 while (status == size)
198 lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
199 status = GetModuleFileName(
nullptr, lpFilename, size);
202 filename = lpFilename;
205#elif defined(__APPLE__)
208 char* buffer = (
char*)malloc(bufsize);
210 int status = _NSGetExecutablePath(buffer, &bufsize);
214 buffer = (
char*)malloc(bufsize);
215 status = _NSGetExecutablePath(buffer, &bufsize);
221#elif defined(__FreeBSD__)
224 std::size_t bufSize = 1024;
225 char* buf = (
char*)malloc(bufSize);
229 mib[2] = KERN_PROC_PATHNAME;
232 sysctl(mib, 4, buf, &bufSize,
nullptr, 0);
240Append(std::string left, std::string right)
247 if (lastSep != left.size() - 1)
251 left = left.substr(0, left.size() - 1);
257std::list<std::string>
262 std::list<std::string> retval(items.begin(), items.end());
267Join(std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
270 std::string retval =
"";
271 for (std::list<std::string>::const_iterator i = begin; i != end; i++)
290std::list<std::string>
295 std::list<std::string> files;
296 std::tie(files, err) = ReadFilesNoThrow(path);
322 time_t now = time(
nullptr);
323 struct tm* tm_now = localtime(&now);
328 srand(time(
nullptr));
342 std::ostringstream oss;
343 oss << path <<
SYSTEM_PATH_SEP <<
"ns-3." << tm_now->tm_hour <<
"." << tm_now->tm_min <<
"."
344 << tm_now->tm_sec <<
"." << n;
355 if (!fs::exists(path))
357 fs::create_directories(path, ec);
373 std::list<std::string> files;
374 tie(files, err) = ReadFilesNoThrow(dirpath);
384 auto tokens =
Split(path);
385 std::string file = tokens.back();
398 auto it = std::find(files.begin(), files.end(), file);
399 if (it == files.end())
418 std::regex incompatible_characters(
" |:[^\\\\]|<|>|\\*");
419 std::string valid_path;
420 std::regex_replace(std::back_inserter(valid_path),
423 incompatible_characters,
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
Class Environment declaration.
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.
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
ns3::StringValue attribute value declarations.
constexpr auto SYSTEM_PATH_SEP
System-specific path separator used between directory names.
ns3::SystemPath declarations.