35#include <mach-o/dyld.h>
39#include <sys/sysctl.h>
49#define WIN32_LEAN_AND_MEAN
85std::tuple<std::list<std::string>,
bool>
89 std::list<std::string> files;
90 if (!std::filesystem::exists(path))
92 return std::make_tuple(files,
true);
94 for (
auto& it : std::filesystem::directory_iterator(path))
96 if (!std::filesystem::is_directory(it.path()))
98 files.push_back(it.path().filename().string());
101 return std::make_tuple(files,
false);
123 std::list<std::string> elements =
Split(path);
124 auto last = elements.end();
126 return Join(elements.begin(), last);
142 std::string filename;
143#if defined(__linux__)
146 char* buffer = (
char*)malloc(size);
147 memset(buffer, 0, size);
151 status = readlink(
"/proc/self/exe", buffer, size);
152 if (status != 1 || (status == -1 && errno != ENAMETOOLONG))
158 buffer = (
char*)malloc(size);
159 memset(buffer, 0, size);
168#elif defined(__WIN32__)
172 auto lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
173 DWORD status = GetModuleFileName(
nullptr, lpFilename, size);
174 while (status == size)
178 lpFilename = (LPTSTR)malloc(
sizeof(TCHAR) * size);
179 status = GetModuleFileName(
nullptr, lpFilename, size);
182 filename = lpFilename;
185#elif defined(__APPLE__)
188 char* buffer = (
char*)malloc(bufsize);
190 int status = _NSGetExecutablePath(buffer, &bufsize);
194 buffer = (
char*)malloc(bufsize);
195 status = _NSGetExecutablePath(buffer, &bufsize);
201#elif defined(__FreeBSD__)
204 std::size_t bufSize = 1024;
205 char* buf = (
char*)malloc(bufSize);
209 mib[2] = KERN_PROC_PATHNAME;
212 sysctl(mib, 4, buf, &bufSize,
nullptr, 0);
220Append(std::string left, std::string right)
227 if (lastSep != left.size() - 1)
231 left = left.substr(0, left.size() - 1);
237std::list<std::string>
242 std::list<std::string> retval(items.begin(), items.end());
247Join(std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
250 std::string retval =
"";
251 for (
auto i = begin; i != end; i++)
270std::list<std::string>
275 std::list<std::string> files;
276 std::tie(files, err) = ReadFilesNoThrow(path);
302 time_t now = time(
nullptr);
303 struct tm* tm_now = localtime(&now);
308 srand(time(
nullptr));
322 std::ostringstream oss;
323 oss << path <<
SYSTEM_PATH_SEP <<
"ns-3." << tm_now->tm_hour <<
"." << tm_now->tm_min <<
"."
324 << tm_now->tm_sec <<
"." << n;
335 if (!std::filesystem::exists(path))
337 std::filesystem::create_directories(path, ec);
353 std::list<std::string> files;
354 tie(files, err) = ReadFilesNoThrow(dirpath);
364 auto tokens =
Split(path);
365 std::string file = tokens.back();
378 auto it = std::find(files.begin(), files.end(), file);
379 if (it == files.end())
398 std::regex incompatible_characters(
" |:[^\\\\]|<|>|\\*");
399 std::string valid_path;
400 std::regex_replace(std::back_inserter(valid_path),
403 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.