WinOS.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "OS.h"
  23. #include <windows.h>
  24. #include <cstdio>
  25. #include <cstdarg>
  26. namespace Crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. void OS::Printf(const char* string, ...)
  30. {
  31. va_list args;
  32. va_start(args, string);
  33. vprintf(string, args);
  34. va_end(args);
  35. }
  36. //-----------------------------------------------------------------------------
  37. void OS::Vprintf(const char* string, va_list arg)
  38. {
  39. vprintf(string, arg);
  40. }
  41. //-----------------------------------------------------------------------------
  42. void OS::LogDebug(const char* string, va_list arg)
  43. {
  44. Printf("D: ");
  45. Vprintf(string, arg);
  46. Printf("\n");
  47. }
  48. //-----------------------------------------------------------------------------
  49. void OS::LogError(const char* string, va_list arg)
  50. {
  51. Printf("E: ");
  52. Vprintf(string, arg);
  53. Printf("\n");
  54. }
  55. //-----------------------------------------------------------------------------
  56. void OS::LogWarning(const char* string, va_list arg)
  57. {
  58. Printf("W: ");
  59. Vprintf(string, arg);
  60. Printf("\n");
  61. }
  62. //-----------------------------------------------------------------------------
  63. void OS::LogInfo(const char* string, va_list arg)
  64. {
  65. Printf("I: ");
  66. Vprintf(string, arg);
  67. Printf("\n");
  68. }
  69. //-----------------------------------------------------------------------------
  70. bool OS::Exists(const char* path)
  71. {
  72. DWORD fileAttr;
  73. fileAttr = GetFileAttributes(path);
  74. return (fileAttr != INVALID_FILE_ATTRIBUTES);
  75. }
  76. //-----------------------------------------------------------------------------
  77. bool OS::IsDir(const char* path)
  78. {
  79. DWORD fileAttr;
  80. fileAttr = GetFileAttributes(path);
  81. return (fileAttr != INVALID_FILE_ATTRIBUTES && (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != 0);
  82. }
  83. //-----------------------------------------------------------------------------
  84. bool OS::IsReg(const char* path)
  85. {
  86. return !IsDir(path);
  87. }
  88. //-----------------------------------------------------------------------------
  89. bool OS::Mknod(const char* path)
  90. {
  91. HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  92. if (hFile == INVALID_HANDLE_VALUE)
  93. return false;
  94. CloseHandle(hFile);
  95. return true;
  96. }
  97. //-----------------------------------------------------------------------------
  98. bool OS::Unlink(const char* path)
  99. {
  100. return DeleteFile(path) == TRUE;
  101. }
  102. //-----------------------------------------------------------------------------
  103. bool OS::Mkdir(const char* path)
  104. {
  105. return CreateDirectory(path, NULL) == TRUE;
  106. }
  107. //-----------------------------------------------------------------------------
  108. bool OS::Rmdir(const char* path)
  109. {
  110. return RemoveDirectory(path) == TRUE;
  111. }
  112. //-----------------------------------------------------------------------------
  113. const char* OS::GetCWD()
  114. {
  115. static char cwdBuf[1024];
  116. int len = GetCurrentDirectory(1024, cwdBuf);
  117. if (len == 0)
  118. {
  119. return Str::EMPTY;
  120. }
  121. return cwdBuf;
  122. }
  123. //-----------------------------------------------------------------------------
  124. const char* OS::GetHome()
  125. {
  126. // TODO
  127. return Str::EMPTY;
  128. }
  129. //-----------------------------------------------------------------------------
  130. const char* OS::GetEnv(const char* env)
  131. {
  132. static char evnBuf[1024];
  133. int len = GetEnvironmentVariable(env, envBuf, 1024);
  134. if (len == 0)
  135. {
  136. return Str::EMPTY;
  137. }
  138. return envBuf;
  139. }
  140. //-----------------------------------------------------------------------------
  141. bool OS::Ls(const char* path, List<Str>& fileList)
  142. {
  143. return false; // TODO
  144. }
  145. } // namespace Crown