WinOS.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <windows.h>
  24. #include <cstdio>
  25. #include <cstdarg>
  26. #include "StringUtils.h"
  27. #include "OS.h"
  28. namespace crown
  29. {
  30. namespace os
  31. {
  32. LARGE_INTEGER frequency;
  33. LARGE_INTEGER base_time;
  34. //-----------------------------------------------------------------------------
  35. void printf(const char* string, ...)
  36. {
  37. va_list args;
  38. va_start(args, string);
  39. ::vprintf(string, args);
  40. va_end(args);
  41. }
  42. //-----------------------------------------------------------------------------
  43. void vprintf(const char* string, va_list arg)
  44. {
  45. ::vprintf(string, arg);
  46. }
  47. //-----------------------------------------------------------------------------
  48. void log_debug(const char* string, va_list arg)
  49. {
  50. printf("D: ");
  51. vprintf(string, arg);
  52. printf("\n");
  53. }
  54. //-----------------------------------------------------------------------------
  55. void log_error(const char* string, va_list arg)
  56. {
  57. printf("E: ");
  58. vprintf(string, arg);
  59. printf("\n");
  60. }
  61. //-----------------------------------------------------------------------------
  62. void log_warning(const char* string, va_list arg)
  63. {
  64. printf("W: ");
  65. vprintf(string, arg);
  66. printf("\n");
  67. }
  68. //-----------------------------------------------------------------------------
  69. void log_info(const char* string, va_list arg)
  70. {
  71. printf("I: ");
  72. vprintf(string, arg);
  73. printf("\n");
  74. }
  75. //-----------------------------------------------------------------------------
  76. bool is_root_path(const char* path)
  77. {
  78. CE_ASSERT(path != NULL, "Path must be != NULL");
  79. if (string::strlen(path) == 1)
  80. {
  81. if ((path[0] >= 65 && path[0] <= 90) || (path[0] >= 97 && path[0] <= 122))
  82. {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. //-----------------------------------------------------------------------------
  89. bool is_absolute_path(const char* path)
  90. {
  91. CE_ASSERT(path != NULL, "Path must be != NULL");
  92. if (string::strlen(path) > 0)
  93. {
  94. if ((path[0] >= 'c' && path[0] <= 'z') || (path[0] >= 'C' && path[0] <= 'Z'))
  95. {
  96. if (path[1] == ':')
  97. {
  98. return true;
  99. }
  100. }
  101. }
  102. return false;
  103. }
  104. //-----------------------------------------------------------------------------
  105. bool exists(const char* path)
  106. {
  107. DWORD fileAttr;
  108. fileAttr = GetFileAttributes(path);
  109. return (fileAttr != INVALID_FILE_ATTRIBUTES);
  110. }
  111. //-----------------------------------------------------------------------------
  112. bool is_directory(const char* path)
  113. {
  114. DWORD fileAttr;
  115. fileAttr = GetFileAttributes(path);
  116. return (fileAttr != INVALID_FILE_ATTRIBUTES && (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != 0);
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool is_file(const char* path)
  120. {
  121. DWORD fileAttr;
  122. fileAttr = GetFileAttributes(path);
  123. return (fileAttr != INVALID_FILE_ATTRIBUTES && (fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0);
  124. }
  125. //-----------------------------------------------------------------------------
  126. bool create_file(const char* path)
  127. {
  128. HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  129. if (hFile == INVALID_HANDLE_VALUE)
  130. return false;
  131. CloseHandle(hFile);
  132. return true;
  133. }
  134. //-----------------------------------------------------------------------------
  135. bool delete_file(const char* path)
  136. {
  137. return DeleteFile(path) == TRUE;
  138. }
  139. //-----------------------------------------------------------------------------
  140. bool create_directory(const char* path)
  141. {
  142. return CreateDirectory(path, NULL) == TRUE;
  143. }
  144. //-----------------------------------------------------------------------------
  145. bool delete_directory(const char* path)
  146. {
  147. return RemoveDirectory(path) == TRUE;
  148. }
  149. //-----------------------------------------------------------------------------
  150. const char* get_cwd()
  151. {
  152. static char cwdBuf[1024];
  153. int32_t len = GetCurrentDirectory(1024, cwdBuf);
  154. if (len == 0)
  155. {
  156. return string::EMPTY;
  157. }
  158. return cwdBuf;
  159. }
  160. //-----------------------------------------------------------------------------
  161. const char* get_home()
  162. {
  163. // TODO
  164. return string::EMPTY;
  165. }
  166. //-----------------------------------------------------------------------------
  167. const char* get_env(const char* env)
  168. {
  169. static char envBuf[1024];
  170. int32_t len = GetEnvironmentVariable(env, envBuf, 1024);
  171. if (len == 0)
  172. {
  173. return string::EMPTY;
  174. }
  175. return envBuf;
  176. }
  177. //-----------------------------------------------------------------------------
  178. void list_files(const char* path, Vector<DynamicString>& files)
  179. {
  180. HANDLE file = INVALID_HANDLE_VALUE;
  181. WIN32_FIND_DATA ffd;
  182. char cur_path[MAX_PATH_LENGTH];
  183. string::strncpy(cur_path, path, string::strlen(path) + 1);
  184. string::strncat(cur_path, "\\*", 2);
  185. file = FindFirstFile(cur_path, &ffd);
  186. do
  187. {
  188. CE_ASSERT(file != INVALID_HANDLE_VALUE, "Unable to list files. errono %d", GetLastError());
  189. if ((string::strcmp(ffd.cFileName, ".") == 0) || (string::strcmp(ffd.cFileName, "..") == 0))
  190. {
  191. continue;
  192. }
  193. DynamicString filename(default_allocator());
  194. filename = ffd.cFileName;
  195. files.push_back(filename);
  196. }
  197. while (FindNextFile(file, &ffd) != 0);
  198. FindClose(file);
  199. }
  200. //-----------------------------------------------------------------------------
  201. const char* normalize_path(const char* path)
  202. {
  203. static char norm[MAX_PATH_LENGTH];
  204. for (uint32_t i = 0; i < string::strlen(path)+1; i++)
  205. {
  206. if (path[i] == '/')
  207. {
  208. norm[i] = '\\';
  209. }
  210. else
  211. {
  212. norm[i] = path[i];
  213. }
  214. }
  215. return norm;
  216. }
  217. //-----------------------------------------------------------------------------
  218. void init_os()
  219. {
  220. QueryPerformanceFrequency(&frequency);
  221. CE_ASSERT(frequency.QuadPart > 0, "Hardware does not support high resolution performance counter.\n");
  222. QueryPerformanceCounter(&base_time);
  223. }
  224. //-----------------------------------------------------------------------------
  225. uint64_t milliseconds()
  226. {
  227. LARGE_INTEGER current_time;
  228. QueryPerformanceCounter(&current_time);
  229. return (uint64_t) (current_time.QuadPart - base_time.QuadPart) / (frequency.QuadPart / 1000);
  230. }
  231. //-----------------------------------------------------------------------------
  232. uint64_t microseconds()
  233. {
  234. LARGE_INTEGER current_time;
  235. QueryPerformanceCounter(&current_time);
  236. return (uint64_t) (current_time.QuadPart - base_time.QuadPart) / (frequency.QuadPart / 1000000);
  237. }
  238. //-----------------------------------------------------------------------------
  239. void* open_library(const char* path)
  240. {
  241. HMODULE library = LoadLibrary(path);
  242. CE_ASSERT(library != NULL, "Unable to load library '%s' with error: %d\n", path, GetLastError());
  243. return library;
  244. }
  245. //-----------------------------------------------------------------------------
  246. void close_library(void* library)
  247. {
  248. BOOL freed = FreeLibrary((HMODULE)library);
  249. CE_ASSERT(freed, "Failed to close library\n with error: %d\n", GetLastError());
  250. }
  251. //-----------------------------------------------------------------------------
  252. void* lookup_symbol(void* library, const char* name)
  253. {
  254. FARPROC symbol = GetProcAddress((HMODULE)library, name);
  255. CE_ASSERT(symbol != NULL, "Unable to export symbol '%s' with error: %d\n", name, GetLastError());
  256. return symbol;
  257. }
  258. //-----------------------------------------------------------------------------
  259. void execute_process(const char* args[])
  260. {
  261. STARTUPINFO info;
  262. memset(&info, 0, sizeof(info));
  263. info.cb = sizeof(info);
  264. PROCESS_INFORMATION process;
  265. memset(&process, 0, sizeof(process));
  266. DynamicString cmds(default_allocator());
  267. for (uint32_t i = 0; args[i] != NULL; i++)
  268. {
  269. cmds += args[i];
  270. cmds += ' ';
  271. }
  272. // Necessary because CreateProcess second argument must be non-const
  273. char tmp[1024];
  274. string::strncpy(tmp, normalize_path(cmds.c_str()), string::strlen(cmds.c_str()));
  275. int32_t res;
  276. if (res = CreateProcess(args[0], tmp, NULL, NULL, TRUE, 0, NULL, NULL, &info, &process))
  277. {
  278. ::WaitForSingleObject(process.hProcess, INFINITE);
  279. CloseHandle(process.hProcess);
  280. CloseHandle(process.hThread);
  281. }
  282. CE_ASSERT(res != 0, "Unable to create process for %s, errno: %d\n", args[0], GetLastError());
  283. }
  284. } // namespace os
  285. } // namespace crown