2
0

WinOS.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "OS.h"
  24. #include <windows.h>
  25. #include <cstdio>
  26. #include <cstdarg>
  27. #include "StringUtils.h"
  28. namespace crown
  29. {
  30. namespace os
  31. {
  32. // FIXME: timespec does not exists in win
  33. // static timespec base_time;
  34. LARGE_INTEGER frequency;
  35. LARGE_INTEGER base_time;
  36. //-----------------------------------------------------------------------------
  37. void printf(const char* string, ...)
  38. {
  39. va_list args;
  40. va_start(args, string);
  41. ::vprintf(string, args);
  42. va_end(args);
  43. }
  44. //-----------------------------------------------------------------------------
  45. void vprintf(const char* string, va_list arg)
  46. {
  47. ::vprintf(string, arg);
  48. }
  49. //-----------------------------------------------------------------------------
  50. void log_debug(const char* string, va_list arg)
  51. {
  52. printf("D: ");
  53. vprintf(string, arg);
  54. printf("\n");
  55. }
  56. //-----------------------------------------------------------------------------
  57. void log_error(const char* string, va_list arg)
  58. {
  59. printf("E: ");
  60. vprintf(string, arg);
  61. printf("\n");
  62. }
  63. //-----------------------------------------------------------------------------
  64. void log_warning(const char* string, va_list arg)
  65. {
  66. printf("W: ");
  67. vprintf(string, arg);
  68. printf("\n");
  69. }
  70. //-----------------------------------------------------------------------------
  71. void log_info(const char* string, va_list arg)
  72. {
  73. printf("I: ");
  74. vprintf(string, arg);
  75. printf("\n");
  76. }
  77. //-----------------------------------------------------------------------------
  78. bool is_root_path(const char* path)
  79. {
  80. CE_ASSERT(path != NULL, "Path must be != NULL");
  81. if (string::strlen(path) == 1)
  82. {
  83. if ((path[0] >= 65 && path[0] <= 90) || (path[0] >= 97 && path[0] <= 122))
  84. {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. //-----------------------------------------------------------------------------
  91. bool is_absolute_path(const char* path)
  92. {
  93. CE_ASSERT(path != NULL, "Path must be != NULL");
  94. if (string::strlen(path) > 0)
  95. {
  96. if ((path[0] >= 65 && path[0] <= 90) || (path[0] >= 97 && path[0] <= 122))
  97. {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. //-----------------------------------------------------------------------------
  104. bool exists(const char* path)
  105. {
  106. DWORD fileAttr;
  107. fileAttr = GetFileAttributes(path);
  108. return (fileAttr != INVALID_FILE_ATTRIBUTES);
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool is_dir(const char* path)
  112. {
  113. DWORD fileAttr;
  114. fileAttr = GetFileAttributes(path);
  115. return (fileAttr != INVALID_FILE_ATTRIBUTES && (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != 0);
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool is_reg(const char* path)
  119. {
  120. return !is_dir(path);
  121. }
  122. //-----------------------------------------------------------------------------
  123. bool mknod(const char* path)
  124. {
  125. HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  126. if (hFile == INVALID_HANDLE_VALUE)
  127. return false;
  128. CloseHandle(hFile);
  129. return true;
  130. }
  131. //-----------------------------------------------------------------------------
  132. bool unlink(const char* path)
  133. {
  134. return DeleteFile(path) == TRUE;
  135. }
  136. //-----------------------------------------------------------------------------
  137. bool mkdir(const char* path)
  138. {
  139. return CreateDirectory(path, NULL) == TRUE;
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool rmdir(const char* path)
  143. {
  144. return RemoveDirectory(path) == TRUE;
  145. }
  146. //-----------------------------------------------------------------------------
  147. const char* get_cwd()
  148. {
  149. static char cwdBuf[1024];
  150. int32_t len = GetCurrentDirectory(1024, cwdBuf);
  151. if (len == 0)
  152. {
  153. return string::EMPTY;
  154. }
  155. return cwdBuf;
  156. }
  157. //-----------------------------------------------------------------------------
  158. const char* get_home()
  159. {
  160. // TODO
  161. return string::EMPTY;
  162. }
  163. //-----------------------------------------------------------------------------
  164. const char* get_env(const char* env)
  165. {
  166. static char envBuf[1024];
  167. int32_t len = GetEnvironmentVariable(env, envBuf, 1024);
  168. if (len == 0)
  169. {
  170. return string::EMPTY;
  171. }
  172. return envBuf;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // bool ls(const char* path, List<Str>& fileList)
  176. // {
  177. // // TODO
  178. // return false;
  179. //}
  180. //-----------------------------------------------------------------------------
  181. void init_os()
  182. {
  183. QueryPerformanceFrequency(&frequency);
  184. CE_ASSERT(frequency.QuadPart > 0, "Hardware does not support high resolution performance counter.\n");
  185. QueryPerformanceCounter(&base_time);
  186. }
  187. //-----------------------------------------------------------------------------
  188. uint64_t milliseconds()
  189. {
  190. LARGE_INTEGER current_time;
  191. QueryPerformanceCounter(&current_time);
  192. return (uint64_t) (current_time.QuadPart - base_time.QuadPart) / (frequency.QuadPart / 1000);
  193. }
  194. //-----------------------------------------------------------------------------
  195. uint64_t microseconds()
  196. {
  197. LARGE_INTEGER current_time;
  198. QueryPerformanceCounter(&current_time);
  199. return (uint64_t) (current_time.QuadPart - base_time.QuadPart) / (frequency.QuadPart / 1000000);
  200. }
  201. //-----------------------------------------------------------------------------
  202. void* open_library(const char* path)
  203. {
  204. HMODULE library = LoadLibrary(path);
  205. CE_ASSERT(library != NULL, "Unable to load library '%s' with error: %d\n", path, GetLastError());
  206. return library;
  207. }
  208. //-----------------------------------------------------------------------------
  209. void close_library(void* library)
  210. {
  211. BOOL freed = FreeLibrary((HMODULE)library);
  212. CE_ASSERT(freed, "Failed to close library\n with error: %d\n", GetLastError());
  213. }
  214. //-----------------------------------------------------------------------------
  215. void* lookup_symbol(void* library, const char* name)
  216. {
  217. FARPROC symbol = GetProcAddress((HMODULE)library, name);
  218. CE_ASSERT(symbol != NULL, "Unable to export symbol '%s' with error: %d\n", name, GetLastError());
  219. return symbol;
  220. }
  221. } // namespace os
  222. } // namespace crown