24#include <mach-o/dyld.h>
28#include <sys/sysctl.h>
38#define WIN32_LEAN_AND_MEAN
74std::tuple<std::list<std::string>,
bool>
78 std::list<std::string> files;
79 if (!std::filesystem::exists(path))
81 return std::make_tuple(files,
true);
83 for (
auto& it : std::filesystem::directory_iterator(path))
85 if (!std::filesystem::is_directory(it.path()))
87 files.push_back(it.path().filename().string());
90 return std::make_tuple(files,
false);
112 std::list<std::string> elements =
Split(path);
113 auto last = elements.end();
115 return Join(elements.begin(), last);
131 std::string filename;
132#if defined(__linux__)
135 char* buffer = (
char*)malloc(size);
136 memset(buffer, 0, size);
140 status = readlink(
"/proc/self/exe", buffer, size);
141 if (status != 1 || (status == -1 && errno != ENAMETOOLONG))
147 buffer = (
char*)malloc(size);
148 memset(buffer, 0, size);
157#elif defined(__WIN32__)
161 auto lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
162 DWORD status = GetModuleFileName(
nullptr, lpFilename, size);
163 while (status == size)
167 lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
168 status = GetModuleFileName(
nullptr, lpFilename, size);
171 filename = lpFilename;
174#elif defined(__APPLE__)
177 char* buffer = (
char*)malloc(bufsize);
179 int status = _NSGetExecutablePath(buffer, &bufsize);
183 buffer = (
char*)malloc(bufsize);
184 status = _NSGetExecutablePath(buffer, &bufsize);
190#elif defined(__FreeBSD__)
193 std::size_t bufSize = 1024;
194 char* buf = (
char*)malloc(bufSize);
198 mib[2] = KERN_PROC_PATHNAME;
201 sysctl(mib, 4, buf, &bufSize,
nullptr, 0);
209Append(std::string left, std::string right)
216 if (lastSep != left.size() - 1)
220 left = left.substr(0, left.size() - 1);
226std::list<std::string>
231 std::list<std::string> retval(items.begin(), items.end());
236Join(std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
239 std::string retval =
"";
240 for (
auto i = begin; i != end; i++)
259std::list<std::string>
264 std::list<std::string> files;
265 std::tie(files, err) = ReadFilesNoThrow(path);
291 time_t now = time(
nullptr);
292 struct tm* tm_now = localtime(&now);
297 srand(time(
nullptr));
311 std::ostringstream oss;
312 oss << path <<
SYSTEM_PATH_SEP <<
"ns-3." << tm_now->tm_hour <<
"." << tm_now->tm_min <<
"."
313 << tm_now->tm_sec <<
"." << n;
324 if (!std::filesystem::exists(path))
326 std::filesystem::create_directories(path, ec);
342 std::list<std::string> files;
343 tie(files, err) = ReadFilesNoThrow(dirpath);
353 auto tokens =
Split(path);
354 const std::string& file = tokens.back();
367 auto it = std::find(files.begin(), files.end(), file);
368 if (it == files.end())
386 std::regex incompatible_characters(
" |:[^\\\\]|<|>|\\*");
387 std::string valid_path;
388 std::regex_replace(std::back_inserter(valid_path),
391 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.
Namespace for various file and directory path functions.
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.