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 pathLength = readlink(
"/proc/self/exe", buffer, size);
141 if ((pathLength > 0 && pathLength < size) ||
142 (pathLength == -1 && errno != ENAMETOOLONG))
148 buffer = (
char*)malloc(size);
149 memset(buffer, 0, size);
151 if (pathLength == -1)
158#elif defined(__WIN32__)
162 auto lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
163 DWORD pathLength = GetModuleFileName(
nullptr, lpFilename, size);
164 while (pathLength == size)
168 lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
169 pathLength = GetModuleFileName(
nullptr, lpFilename, size);
172 filename = lpFilename;
175#elif defined(__APPLE__)
178 char* buffer = (
char*)malloc(bufsize);
180 int status = _NSGetExecutablePath(buffer, &bufsize);
184 buffer = (
char*)malloc(bufsize);
185 status = _NSGetExecutablePath(buffer, &bufsize);
191#elif defined(__FreeBSD__)
194 std::size_t bufSize = 1024;
195 char* buf = (
char*)malloc(bufSize);
199 mib[2] = KERN_PROC_PATHNAME;
202 sysctl(mib, 4, buf, &bufSize,
nullptr, 0);
216Append(std::string left, std::string right)
223 if (lastSep != left.size() - 1)
227 left = left.substr(0, left.size() - 1);
233std::list<std::string>
238 std::list<std::string> retval(items.begin(), items.end());
243Join(std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
246 std::string retval =
"";
247 for (
auto i = begin; i != end; i++)
266std::list<std::string>
271 std::list<std::string> files;
272 std::tie(files, err) = ReadFilesNoThrow(path);
298 time_t now = time(
nullptr);
299 struct tm* tm_now = localtime(&now);
304 srand(time(
nullptr));
318 std::ostringstream oss;
319 oss << path <<
SYSTEM_PATH_SEP <<
"ns-3." << tm_now->tm_hour <<
"." << tm_now->tm_min <<
"."
320 << tm_now->tm_sec <<
"." << n;
331 if (!std::filesystem::exists(path))
333 std::filesystem::create_directories(path, ec);
349 std::list<std::string> files;
350 tie(files, err) = ReadFilesNoThrow(dirpath);
360 auto tokens =
Split(path);
361 const std::string& file = tokens.back();
374 auto it = std::find(files.begin(), files.end(), file);
375 if (it == files.end())
393 std::regex incompatible_characters(
" |:[^\\\\]|<|>|\\*");
394 std::string valid_path;
395 std::regex_replace(std::back_inserter(valid_path),
398 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.
std::string FindSelf()
Get the file system path to the current executable.
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.