24 #include "ns3/core-config.h"
30 #if defined (HAVE_DIRENT_H) and defined (HAVE_SYS_TYPES_H)
33 #include <sys/types.h>
36 #if defined (HAVE_SYS_STAT_H) and defined (HAVE_SYS_TYPES_H)
39 #include <sys/types.h>
44 #include <mach-o/dyld.h>
48 #include <sys/types.h>
49 #include <sys/sysctl.h>
60 #if defined (__win32__)
61 #define SYSTEM_PATH_SEP "\\"
63 #define SYSTEM_PATH_SEP "/"
76 namespace SystemPath {
91 std::list<std::string> elements =
Split (path);
92 std::list<std::string>::const_iterator last = elements.end();
94 return Join (elements.begin (), last);
109 std::string filename;
110 #if defined(__linux__)
113 char *buffer = (
char*)malloc (size);
114 memset (buffer, 0, size);
118 status = readlink(
"/proc/self/exe", buffer, size);
119 if (status != 1 || (status == -1 && errno != ENAMETOOLONG))
125 buffer = (
char*)malloc (size);
126 memset (buffer, 0, size);
135 #elif defined (__win32__)
140 LPTSTR lpFilename = (LPTSTR) malloc (
sizeof(TCHAR) * size);
141 DWORD status = GetModuleFilename (0, lpFilename, size);
142 while (status == size)
146 lpFilename = (LPTSTR) malloc (
sizeof(TCHAR) * size);
147 status = GetModuleFilename (0, lpFilename, size);
150 filename = lpFilename;
153 #elif defined (__APPLE__)
155 uint32_t bufsize = 1024;
156 char *buffer = (
char *) malloc (bufsize);
158 int status = _NSGetExecutablePath (buffer, &bufsize);
162 buffer = (
char *) malloc (bufsize);
163 status = _NSGetExecutablePath (buffer, &bufsize);
169 #elif defined (__FreeBSD__)
172 size_t bufSize = 1024;
173 char *buf = (
char *) malloc(bufSize);
177 mib[2] = KERN_PROC_PATHNAME;
180 sysctl(mib, 4, buf, &bufSize, NULL, 0);
187 std::string
Append (std::string left, std::string right)
194 if (lastSep != left.size () - 1)
198 left = left.substr (0, left.size () - 1);
204 std::list<std::string>
Split (std::string path)
207 std::list<std::string> retval;
208 std::string::size_type current = 0, next = 0;
210 while (next != std::string::npos)
212 std::string item = path.substr (current, next - current);
213 retval.push_back (item);
217 std::string item = path.substr (current, next - current);
218 retval.push_back (item);
222 std::string
Join (std::list<std::string>::const_iterator begin,
223 std::list<std::string>::const_iterator end)
226 std::string retval =
"";
227 for (std::list<std::string>::const_iterator i = begin; i != end; i++)
244 std::list<std::string> files;
245 #if defined HAVE_OPENDIR
246 DIR *dp = opendir (path.c_str ());
251 struct dirent *de = readdir (dp);
254 files.push_back (de->d_name);
258 #elif defined (HAVE_FIND_FIRST_FILE)
261 WIN32_FIND_DATA fileData;
263 hFind = FindFirstFile (path.c_str (), &FindFileData);
264 if (hFind == INVALID_HANDLE_VALUE)
270 files.push_back (fileData.cFileName);
271 }
while (FindNextFile (hFind, &fileData));
274 #error "No support for reading a directory on this platform"
285 path = getenv (
"TMP");
288 path = getenv (
"TEMP");
291 path =
const_cast<char *
> (
"/tmp");
299 time_t now = time (NULL);
300 struct tm *tm_now = localtime (&now);
306 long int n = rand ();
319 std::ostringstream oss;
321 << tm_now->tm_min <<
"." << tm_now->tm_sec <<
"." << n;
332 std::list<std::string> elements =
Split (path);
333 for (std::list<std::string>::const_iterator i = elements.begin (); i != elements.end (); ++i)
335 std::string tmp =
Join (elements.begin (), i);
336 #if defined(HAVE_MKDIR_H)
337 if (mkdir (tmp.c_str (), S_IRWXU))
345 #if defined(HAVE_MKDIR_H)
346 if (mkdir (path.c_str (), S_IRWXU))
NS_FATAL_x macro definitions.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
System-independent file and directory function declarations.
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.
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
std::string MakeTemporaryDirectoryName(void)
Get the name of a temporary directory.
std::list< std::string > ReadFiles(std::string path)
Get the list of files located in a file system directory.
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 FindSelfDirectory(void)
Get the file system path to the current executable.
std::string Append(std::string left, std::string right)
Join two file system path elements.
#define SYSTEM_PATH_SEP
System-specific path separator used between directory names.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.