24 #include "ns3/core-config.h"
28 #if defined (HAVE_DIRENT_H) and defined (HAVE_SYS_TYPES_H)
30 #include <sys/types.h>
33 #if defined (HAVE_SYS_STAT_H) and defined (HAVE_SYS_TYPES_H)
35 #include <sys/types.h>
40 #include <mach-o/dyld.h>
44 #include <sys/types.h>
45 #include <sys/sysctl.h>
52 #if defined (__win32__)
53 #define SYSTEM_PATH_SEP "\\"
55 #define SYSTEM_PATH_SEP "/"
62 namespace SystemPath {
67 std::list<std::string> elements =
Split (path);
68 std::list<std::string>::const_iterator last = elements.end();
70 return Join (elements.begin (), last);
86 #if defined(__linux__)
89 char *buffer = (
char*)malloc (size);
90 memset (buffer, 0, size);
94 status = readlink(
"/proc/self/exe", buffer, size);
95 if (status != 1 || (status == -1 && errno != ENAMETOOLONG))
101 buffer = (
char*)malloc (size);
102 memset (buffer, 0, size);
111 #elif defined (__win32__)
116 LPTSTR lpFilename = (LPTSTR) malloc (
sizeof(TCHAR) * size);
117 DWORD status = GetModuleFilename (0, lpFilename, size);
118 while (status == size)
122 lpFilename = (LPTSTR) malloc (
sizeof(TCHAR) * size);
123 status = GetModuleFilename (0, lpFilename, size);
126 filename = lpFilename;
129 #elif defined (__APPLE__)
131 uint32_t bufsize = 1024;
132 char *buffer = (
char *) malloc (bufsize);
134 int status = _NSGetExecutablePath (buffer, &bufsize);
138 buffer = (
char *) malloc (bufsize);
139 status = _NSGetExecutablePath (buffer, &bufsize);
145 #elif defined (__FreeBSD__)
148 size_t bufSize = 1024;
149 char *buf = (
char *) malloc(bufSize);
153 mib[2] = KERN_PROC_PATHNAME;
156 sysctl(mib, 4, buf, &bufSize, NULL, 0);
163 std::string
Append (std::string left, std::string right)
170 if (lastSep != left.size () - 1)
174 left = left.substr (0, left.size () - 1);
180 std::list<std::string>
Split (std::string path)
183 std::list<std::string> retval;
184 std::string::size_type
current = 0, next = 0;
186 while (next != std::string::npos)
188 std::string item = path.substr (current, next - current);
189 retval.push_back (item);
193 std::string item = path.substr (current, next - current);
194 retval.push_back (item);
198 std::string
Join (std::list<std::string>::const_iterator begin,
199 std::list<std::string>::const_iterator end)
202 std::string retval =
"";
203 for (std::list<std::string>::const_iterator i = begin; i != end; i++)
220 std::list<std::string> files;
221 #if defined HAVE_OPENDIR
222 DIR *dp = opendir (path.c_str ());
227 struct dirent *de = readdir (dp);
230 files.push_back (de->d_name);
234 #elif defined (HAVE_FIND_FIRST_FILE)
237 WIN32_FIND_DATA fileData;
239 hFind = FindFirstFile (path.c_str (), &FindFileData);
240 if (hFind == INVALID_HANDLE_VALUE)
246 files.push_back (fileData.cFileName);
247 }
while (FindNextFile (hFind, &fileData));
250 #error "No support for reading a directory on this platform"
261 path = getenv (
"TMP");
264 path = getenv (
"TEMP");
267 path =
const_cast<char *
> (
"/tmp");
275 time_t now = time (NULL);
276 struct tm *tm_now = localtime (&now);
282 long int n = rand ();
295 std::ostringstream oss;
297 << tm_now->tm_min <<
"." << tm_now->tm_sec <<
"." << n;
306 std::list<std::string> elements =
Split (path);
307 for (std::list<std::string>::const_iterator i = elements.begin (); i != elements.end (); ++i)
309 std::string tmp =
Join (elements.begin (), i);
310 #if defined(HAVE_MKDIR_H)
311 if (mkdir (tmp.c_str (), S_IRWXU))
317 #if defined(HAVE_MKDIR_H)
318 if (mkdir (path.c_str (), S_IRWXU))
#define NS_LOG_FUNCTION(parameters)
#define NS_ASSERT(condition)
void MakeDirectories(std::string path)
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
NS_LOG_COMPONENT_DEFINE("SystemPath")
#define NS_FATAL_ERROR(msg)
fatal error handling
std::string Join(std::list< std::string >::const_iterator begin, std::list< std::string >::const_iterator end)
std::list< std::string > ReadFiles(std::string path)
std::string FindSelfDirectory(void)
std::string Append(std::string left, std::string right)
std::string MakeTemporaryDirectoryName(void)
std::string Dirname(std::string path)
#define NS_LOG_ERROR(msg)
std::list< std::string > Split(std::string path)