PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/OGDF/src/basic/basic.cpp

https://gitlab.com/life436/tortoisegit
C++ | 357 lines | 235 code | 77 blank | 45 comment | 49 complexity | 907ba0e78888956fa50085880e02c0c5 MD5 | raw file
  1. /*
  2. * $Revision: 2626 $
  3. *
  4. * last checkin:
  5. * $Author: gutwenger $
  6. * $Date: 2012-07-17 12:10:52 +0200 (Di, 17. Jul 2012) $
  7. ***************************************************************/
  8. /** \file
  9. * \brief Implementation of basic functionality (incl. file and
  10. * directory handling)
  11. *
  12. * \author Carsten Gutwenger
  13. *
  14. * \par License:
  15. * This file is part of the Open Graph Drawing Framework (OGDF).
  16. *
  17. * \par
  18. * Copyright (C)<br>
  19. * See README.txt in the root directory of the OGDF installation for details.
  20. *
  21. * \par
  22. * This program is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU General Public License
  24. * Version 2 or 3 as published by the Free Software Foundation;
  25. * see the file LICENSE.txt included in the packaging of this file
  26. * for details.
  27. *
  28. * \par
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * \par
  35. * You should have received a copy of the GNU General Public
  36. * License along with this program; if not, write to the Free
  37. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  38. * Boston, MA 02110-1301, USA.
  39. *
  40. * \see http://www.gnu.org/copyleft/gpl.html
  41. ***************************************************************/
  42. #include <ogdf/basic/basic.h>
  43. #include <ogdf/basic/List.h>
  44. #include <ogdf/basic/String.h>
  45. #include <time.h>
  46. // Windows includes
  47. #ifdef OGDF_SYSTEM_WINDOWS
  48. #include <direct.h>
  49. #if defined(_MSC_VER) && defined(UNICODE)
  50. #undef GetFileAttributes
  51. #undef FindFirstFile
  52. #undef FindNextFile
  53. #define GetFileAttributes GetFileAttributesA
  54. #define FindFirstFile FindFirstFileA
  55. #define WIN32_FIND_DATA WIN32_FIND_DATAA
  56. #define FindNextFile FindNextFileA
  57. #endif
  58. #endif
  59. #ifdef __BORLANDC__
  60. #define _chdir chdir
  61. #endif
  62. // Unix includes
  63. #ifdef OGDF_SYSTEM_UNIX
  64. #include <unistd.h>
  65. #include <dirent.h>
  66. #include <sys/types.h>
  67. #include <sys/times.h>
  68. #include <sys/stat.h>
  69. #include <fnmatch.h>
  70. double OGDF_clk_tck = sysconf(_SC_CLK_TCK); //is long. but definig it here avoids casts...
  71. #endif
  72. #ifdef OGDF_DLL
  73. #ifdef OGDF_SYSTEM_WINDOWS
  74. #ifdef __MINGW32__
  75. extern "C"
  76. #endif
  77. BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  78. {
  79. switch (ul_reason_for_call)
  80. {
  81. case DLL_PROCESS_ATTACH:
  82. ogdf::PoolMemoryAllocator::init();
  83. ogdf::System::init();
  84. break;
  85. case DLL_THREAD_ATTACH:
  86. case DLL_THREAD_DETACH:
  87. break;
  88. case DLL_PROCESS_DETACH:
  89. ogdf::PoolMemoryAllocator::cleanup();
  90. break;
  91. }
  92. return TRUE;
  93. }
  94. #else
  95. void __attribute__ ((constructor)) my_load(void)
  96. {
  97. ogdf::PoolMemoryAllocator::init();
  98. ogdf::System::init();
  99. }
  100. void __attribute__ ((destructor)) my_unload(void)
  101. {
  102. ogdf::PoolMemoryAllocator::cleanup();
  103. }
  104. #endif
  105. #else
  106. namespace ogdf {
  107. //static int variables are automatically initialized with 0
  108. int Initialization::s_count;
  109. Initialization::Initialization()
  110. {
  111. if (s_count++ == 0) {
  112. ogdf::PoolMemoryAllocator::init();
  113. ogdf::System::init();
  114. }
  115. }
  116. Initialization::~Initialization()
  117. {
  118. if (--s_count == 0) {
  119. ogdf::PoolMemoryAllocator::cleanup();
  120. }
  121. }
  122. } // namespace ogdf
  123. #endif
  124. namespace ogdf {
  125. // debug level (in debug build only)
  126. #ifdef OGDF_DEBUG
  127. DebugLevel debugLevel;
  128. #endif
  129. double usedTime(double& T)
  130. {
  131. double t = T;
  132. #ifdef OGDF_SYSTEM_WINDOWS
  133. T = double(clock())/CLOCKS_PER_SEC;
  134. #endif
  135. #ifdef OGDF_SYSTEM_UNIX
  136. struct tms now;
  137. times (&now);
  138. T = now.tms_utime/OGDF_clk_tck;
  139. #endif
  140. return T-t;
  141. }
  142. #ifdef OGDF_SYSTEM_WINDOWS
  143. bool isFile(const char *fileName)
  144. {
  145. DWORD att = GetFileAttributes(fileName);
  146. if (att == 0xffffffff) return false;
  147. return (att & FILE_ATTRIBUTE_DIRECTORY) == 0;
  148. }
  149. bool isDirectory(const char *fileName)
  150. {
  151. DWORD att = GetFileAttributes(fileName);
  152. if (att == 0xffffffff) return false;
  153. return (att & FILE_ATTRIBUTE_DIRECTORY) != 0;
  154. }
  155. bool changeDir(const char *dirName)
  156. {
  157. return (_chdir(dirName) == 0);
  158. }
  159. void getEntriesAppend(const char *dirName,
  160. FileType t,
  161. List<String> &entries,
  162. const char *pattern)
  163. {
  164. OGDF_ASSERT(isDirectory(dirName));
  165. String filePattern;
  166. filePattern.sprintf("%s\\%s", dirName, pattern);
  167. WIN32_FIND_DATA findData;
  168. HANDLE handle = FindFirstFile(filePattern.cstr(), &findData);
  169. if (handle != INVALID_HANDLE_VALUE)
  170. {
  171. do {
  172. DWORD isDir = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  173. if(isDir && (
  174. strcmp(findData.cFileName,".") == 0 ||
  175. strcmp(findData.cFileName,"..") == 0)
  176. )
  177. continue;
  178. if (t == ftEntry || (t == ftFile && !isDir) ||
  179. (t == ftDirectory && isDir))
  180. {
  181. entries.pushBack(findData.cFileName);
  182. }
  183. } while(FindNextFile(handle, &findData));
  184. FindClose(handle);
  185. }
  186. }
  187. #endif
  188. #ifdef OGDF_SYSTEM_UNIX
  189. bool isDirectory(const char *fname)
  190. {
  191. struct stat stat_buf;
  192. if (stat(fname,&stat_buf) != 0)
  193. return false;
  194. return (stat_buf.st_mode & S_IFMT) == S_IFDIR;
  195. }
  196. bool isFile(const char *fname)
  197. {
  198. struct stat stat_buf;
  199. if (stat(fname,&stat_buf) != 0)
  200. return false;
  201. return (stat_buf.st_mode & S_IFMT) == S_IFREG;
  202. }
  203. bool changeDir(const char *dirName)
  204. {
  205. return (chdir(dirName) == 0);
  206. }
  207. void getEntriesAppend(const char *dirName,
  208. FileType t,
  209. List<String> &entries,
  210. const char *pattern)
  211. {
  212. OGDF_ASSERT(isDirectory(dirName));
  213. DIR* dir_p = opendir(dirName);
  214. dirent* dir_e;
  215. while ( (dir_e = readdir(dir_p)) != NULL )
  216. {
  217. const char *fname = dir_e->d_name;
  218. if (pattern != 0 && fnmatch(pattern,fname,0)) continue;
  219. String fullName;
  220. fullName.sprintf("%s/%s", dirName, fname);
  221. bool isDir = isDirectory(fullName.cstr());
  222. if(isDir && (
  223. strcmp(fname,".") == 0 ||
  224. strcmp(fname,"..") == 0)
  225. )
  226. continue;
  227. if (t == ftEntry || (t == ftFile && !isDir) ||
  228. (t == ftDirectory && isDir))
  229. {
  230. entries.pushBack(fname);
  231. }
  232. }
  233. closedir(dir_p);
  234. }
  235. #endif
  236. void getEntries(const char *dirName,
  237. FileType t,
  238. List<String> &entries,
  239. const char *pattern)
  240. {
  241. entries.clear();
  242. getEntriesAppend(dirName, t, entries, pattern);
  243. }
  244. void getFiles(const char *dirName,
  245. List<String> &files,
  246. const char *pattern)
  247. {
  248. getEntries(dirName, ftFile, files, pattern);
  249. }
  250. void getSubdirs(const char *dirName,
  251. List<String> &subdirs,
  252. const char *pattern)
  253. {
  254. getEntries(dirName, ftDirectory, subdirs, pattern);
  255. }
  256. void getEntries(const char *dirName,
  257. List<String> &entries,
  258. const char *pattern)
  259. {
  260. getEntries(dirName, ftEntry, entries, pattern);
  261. }
  262. void getFilesAppend(const char *dirName,
  263. List<String> &files,
  264. const char *pattern)
  265. {
  266. getEntriesAppend(dirName, ftFile, files, pattern);
  267. }
  268. void getSubdirsAppend(const char *dirName,
  269. List<String> &subdirs,
  270. const char *pattern)
  271. {
  272. getEntriesAppend(dirName, ftDirectory, subdirs, pattern);
  273. }
  274. void getEntriesAppend(const char *dirName,
  275. List<String> &entries,
  276. const char *pattern)
  277. {
  278. getEntriesAppend(dirName, ftEntry, entries, pattern);
  279. }
  280. } // end namespace ogdf