24 #include "ns3/core-config.h"
32 #if defined (HAVE_DIRENT_H) && defined (HAVE_SYS_TYPES_H)
35 #include <sys/types.h>
38 #if defined (HAVE_SYS_STAT_H) && defined (HAVE_SYS_TYPES_H)
41 #include <sys/types.h>
46 #include <mach-o/dyld.h>
50 #include <sys/types.h>
51 #include <sys/sysctl.h>
62 #if defined (__win32__)
63 #define SYSTEM_PATH_SEP "\\"
65 #define SYSTEM_PATH_SEP "/"
90 std::list<std::string> files;
92 #if defined HAVE_OPENDIR
93 DIR *dp = opendir (path.c_str ());
96 return std::make_tuple (files,
true);
98 struct dirent *de = readdir (dp);
101 files.push_back (de->d_name);
105 #elif defined (HAVE_FIND_FIRST_FILE)
108 WIN32_FIND_DATA fileData;
110 hFind = FindFirstFile (path.c_str (), &FindFileData);
111 if (hFind == INVALID_HANDLE_VALUE)
113 return std::make_tuple (files,
true);
117 files.push_back (fileData.cFileName);
119 while (FindNextFile (hFind, &fileData));
122 #error "No support for reading a directory on this platform"
124 return std::make_tuple (files,
false);
129 namespace SystemPath {
144 std::list<std::string> elements =
Split (path);
145 std::list<std::string>::const_iterator last = elements.end ();
147 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__)
194 LPTSTR lpFilename = (LPTSTR) malloc (
sizeof(TCHAR) * size);
195 DWORD status = GetModuleFilename (0, lpFilename, size);
196 while (status == size)
200 lpFilename = (LPTSTR) malloc (
sizeof(TCHAR) * size);
201 status = GetModuleFilename (0, lpFilename, size);
204 filename = lpFilename;
207 #elif defined (__APPLE__)
209 uint32_t bufsize = 1024;
210 char *buffer = (
char *) malloc (bufsize);
212 int status = _NSGetExecutablePath (buffer, &bufsize);
216 buffer = (
char *) malloc (bufsize);
217 status = _NSGetExecutablePath (buffer, &bufsize);
223 #elif defined (__FreeBSD__)
226 std::size_t bufSize = 1024;
227 char *buf = (
char *) malloc (bufSize);
231 mib[2] = KERN_PROC_PATHNAME;
234 sysctl (mib, 4, buf, &bufSize, NULL, 0);
241 std::string
Append (std::string left, std::string right)
248 if (lastSep != left.size () - 1)
252 left = left.substr (0, left.size () - 1);
258 std::list<std::string>
Split (std::string path)
261 std::list<std::string> retval;
262 std::string::size_type current = 0, 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);
276 std::string
Join (std::list<std::string>::const_iterator begin,
277 std::list<std::string>::const_iterator end)
280 std::string retval =
"";
281 for (std::list<std::string>::const_iterator i = begin; i != end; i++)
304 std::list<std::string> files;
319 path = std::getenv (
"TMP");
320 if (path == NULL || std::strlen (path) == 0)
322 path = std::getenv (
"TEMP");
323 if (path == NULL || std::strlen (path) == 0)
325 path =
const_cast<char *
> (
"/tmp");
333 time_t now = time (NULL);
334 struct tm *tm_now = localtime (&now);
340 long int n = rand ();
353 std::ostringstream oss;
355 << tm_now->tm_min <<
"." << tm_now->tm_sec <<
"." << n;
366 std::list<std::string> elements =
Split (path);
367 auto i = elements.begin ();
368 while (i != elements.end ())
378 std::string tmp =
Join (elements.begin (), i);
379 bool makeDirErr =
false;
381 #if defined(HAVE_MKDIR_H)
382 makeDirErr = mkdir (tmp.c_str (), S_IRWXU);
399 std::list<std::string> files;
410 auto tokens =
Split (path);
411 std::string
file = tokens.back ();
424 auto it = std::find (files.begin (), files.end (),
file);
425 if (it == files.end ())
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_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#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.
std::string MakeTemporaryDirectoryName(void)
Get the name of a temporary directory.
bool Exists(const std::string path)
Check if a path exists.
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::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 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 FindSelfDirectory(void)
Get the file system path to the current executable.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define SYSTEM_PATH_SEP
System-specific path separator used between directory names.
ns3::SystemPath declarations.