Index: src/core/model/system-path.cc |
=================================================================== |
--- a/src/core/model/system-path.cc |
+++ b/src/core/model/system-path.cc |
@@ -57,8 +57,10 @@ |
* \def SYSTEM_PATH_SEP |
* System-specific path separator used between directory names. |
*/ |
-#if defined (__win32__) |
+#if defined (_WIN32) |
#define SYSTEM_PATH_SEP "\\" |
+#define WIN32_LEAN_AND_MEAN |
+#include <windows.h> |
#else |
#define SYSTEM_PATH_SEP "/" |
#endif |
@@ -73,8 +75,6 @@ |
NS_LOG_COMPONENT_DEFINE ("SystemPath"); |
-namespace SystemPath { |
- |
/** |
* \ingroup systempath |
* \brief Get the directory path for a file. |
@@ -85,7 +85,8 @@ |
* \param [in] path The full path to a file. |
* \returns The full path to the containing directory. |
*/ |
-std::string Dirname (std::string path) |
+std::string |
+SystemPath::Dirname (std::string path) |
{ |
NS_LOG_FUNCTION (path); |
std::list<std::string> elements = Split (path); |
@@ -94,7 +95,8 @@ |
return Join (elements.begin (), last); |
} |
-std::string FindSelfDirectory (void) |
+std::string |
+SystemPath::FindSelfDirectory (void) |
{ |
/** |
* This function returns the path to the running $PREFIX. |
@@ -132,20 +134,20 @@ |
filename = buffer; |
free (buffer); |
} |
-#elif defined (__win32__) |
+#elif defined (_WIN32) |
{ |
/** \todo untested. it should work if code is compiled with |
* LPTSTR = char * |
*/ |
DWORD size = 1024; |
LPTSTR lpFilename = (LPTSTR) malloc (sizeof(TCHAR) * size); |
- DWORD status = GetModuleFilename (0, lpFilename, size); |
+ DWORD status = GetModuleFileName(0, lpFilename, size); |
while (status == size) |
{ |
size = size * 2; |
free (lpFilename); |
lpFilename = (LPTSTR) malloc (sizeof(TCHAR) * size); |
- status = GetModuleFilename (0, lpFilename, size); |
+ status = GetModuleFileName(0, lpFilename, size); |
} |
NS_ASSERT (status != 0); |
filename = lpFilename; |
@@ -185,7 +187,8 @@ |
return Dirname (filename); |
} |
-std::string Append (std::string left, std::string right) |
+std::string |
+SystemPath::Append (std::string left, std::string right) |
{ |
// removing trailing separators from 'left' |
NS_LOG_FUNCTION (left << right); |
@@ -202,7 +205,8 @@ |
return retval; |
} |
-std::list<std::string> Split (std::string path) |
+std::list<std::string> |
+SystemPath::Split (std::string path) |
{ |
NS_LOG_FUNCTION (path); |
std::list<std::string> retval; |
@@ -220,7 +224,8 @@ |
return retval; |
} |
-std::string Join (std::list<std::string>::const_iterator begin, |
+std::string |
+SystemPath::Join (std::list<std::string>::const_iterator begin, |
std::list<std::string>::const_iterator end) |
{ |
NS_LOG_FUNCTION (&begin << &end); |
@@ -239,7 +244,8 @@ |
return retval; |
} |
-std::list<std::string> ReadFiles (std::string path) |
+std::list<std::string> |
+SystemPath::ReadFiles (std::string path) |
{ |
NS_LOG_FUNCTION (path); |
std::list<std::string> files; |
@@ -260,15 +266,17 @@ |
/** \todo untested */ |
HANDLE hFind; |
WIN32_FIND_DATA fileData; |
- |
- hFind = FindFirstFile (path.c_str (), &FindFileData); |
+ std::string wildcard = path + "\\*"; |
+ |
+ hFind = FindFirstFile (wildcard.c_str (), &fileData); |
if (hFind == INVALID_HANDLE_VALUE) |
{ |
NS_FATAL_ERROR ("Could not open directory=" << path); |
} |
do |
{ |
- files.push_back (fileData.cFileName); |
+ if (strcmp(fileData.cFileName, ".") != 0 && strcmp(fileData.cFileName, "..") != 0) |
+ files.push_back (fileData.cFileName); |
} while (FindNextFile (hFind, &fileData)); |
FindClose(hFind); |
#else |
@@ -278,7 +286,7 @@ |
} |
std::string |
-MakeTemporaryDirectoryName (void) |
+SystemPath::MakeTemporaryDirectoryName (void) |
{ |
NS_LOG_FUNCTION_NOARGS (); |
char *path = NULL; |
@@ -303,7 +311,7 @@ |
// But we also randomize the name in case there are multiple users doing |
// this at the same time |
// |
- srand (time (0)); |
+ srand (static_cast<unsigned int>(time (0))); |
long int n = rand (); |
// |
@@ -325,7 +333,7 @@ |
} |
void |
-MakeDirectories (std::string path) |
+SystemPath::MakeDirectories (std::string path) |
{ |
NS_LOG_FUNCTION (path); |
@@ -343,19 +351,86 @@ |
NS_LOG_LOGIC ("creating directory " << *i); |
++i; // Now points to one past the directory we want to create |
std::string tmp = Join (elements.begin (), i); |
- bool makeDirErr = false; |
#if defined(HAVE_MKDIR_H) |
+ bool makeDirErr = false; |
makeDirErr = mkdir (tmp.c_str (), S_IRWXU); |
-#endif |
if (makeDirErr) |
{ |
NS_LOG_ERROR ("failed creating directory " << tmp); |
} |
+#else |
+#ifdef _WIN32 |
+ if (CreateDirectory(tmp.c_str(), NULL)) |
+ { |
+ NS_LOG_ERROR("failed creating directory " << tmp); |
+ } |
+#endif |
+#endif |
} |
+ |
+ // Make the final directory. Is this redundant with last iteration above? |
+#if defined(HAVE_MKDIR_H) |
+ if (mkdir (path.c_str (), S_IRWXU)) |
+ { |
+ NS_LOG_ERROR ("failed creating directory " << path); |
+ } |
+#else |
+#ifdef _WIN32 |
+ if (CreateDirectory(path.c_str(), NULL)) |
+ { |
+ NS_LOG_ERROR("failed creating directory " << path); |
+ } |
+#endif |
+#endif |
+ |
} |
-} // namespace SystemPath |
+std::string |
+SystemPath::GetTopLevelSourceDir(void) |
+{ |
+ NS_LOG_FUNCTION_NOARGS(); |
+ std::string self = SystemPath::FindSelfDirectory(); |
+ std::list<std::string> elements = SystemPath::Split(self); |
+ while (!elements.empty()) |
+ { |
+ std::string path = SystemPath::Join(elements.begin(), elements.end()); |
+ if (IsTopLevelSourceDir(path)) |
+ { |
+ return path; |
+ } |
+ elements.pop_back(); |
+ } |
+ NS_FATAL_ERROR("Could not find source directory from self=" << self); |
+} |
+ |
+bool |
+SystemPath::IsTopLevelSourceDir(std::string path) |
+{ |
+ NS_LOG_FUNCTION(path); |
+ bool haveVersion = false; |
+ bool haveLicense = false; |
+ |
+ // |
+ // If there's a file named VERSION and a file named LICENSE in this |
+ // directory, we assume it's our top level source directory. |
+ // |
+ |
+ std::list<std::string> files = SystemPath::ReadFiles(path); |
+ for (std::list<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) |
+ { |
+ if (*i == "VERSION") |
+ { |
+ haveVersion = true; |
+ } |
+ else if (*i == "LICENSE") |
+ { |
+ haveLicense = true; |
+ } |
+ } |
+ |
+ return haveVersion && haveLicense; |
+} |
} // namespace ns3 |