ProcessUtils.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Mutex.h"
  25. #include "ProcessUtils.h"
  26. #include <cstdio>
  27. #include <list>
  28. #include <io.h>
  29. #include <fcntl.h>
  30. #include <time.h>
  31. #include <Windows.h>
  32. // Enable SHGetSpecialFolderPath on MinGW
  33. #ifndef _MSC_VER
  34. #define _WIN32_IE 0x0400
  35. #endif
  36. #include <ShlObj.h>
  37. #include "DebugNew.h"
  38. static bool consoleOpened = false;
  39. static String currentLine;
  40. static Vector<String> arguments;
  41. static Mutex staticMutex;
  42. void ErrorDialog(const char* title, const char* message)
  43. {
  44. MessageBox(0, message, title, 0);
  45. }
  46. void ErrorExit(const String& message, int exitCode)
  47. {
  48. printf("%s", message.CString());
  49. exit(exitCode);
  50. }
  51. void OpenConsoleWindow()
  52. {
  53. if (consoleOpened)
  54. return;
  55. AllocConsole();
  56. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  57. int hCrt = _open_osfhandle((long)hOut, _O_TEXT);
  58. FILE* outFile = _fdopen(hCrt, "w");
  59. setvbuf(outFile, NULL, _IONBF, 1);
  60. *stdout = *outFile;
  61. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  62. hCrt = _open_osfhandle((long)hIn, _O_TEXT);
  63. FILE* inFile = _fdopen(hCrt, "r");
  64. setvbuf(inFile, NULL, _IONBF, 128);
  65. *stdin = *inFile;
  66. consoleOpened = true;
  67. }
  68. void PrintLine(const String& str)
  69. {
  70. printf("%s\n", str.CString());
  71. }
  72. void PrintLine(const char* str)
  73. {
  74. printf("%s\n", str);
  75. }
  76. Mutex& GetStaticMutex()
  77. {
  78. return staticMutex;
  79. }
  80. const Vector<String>& ParseArguments(const char* cmdLine)
  81. {
  82. arguments.Clear();
  83. if (!cmdLine)
  84. return arguments;
  85. String cmdStr(cmdLine);
  86. unsigned cmdStart, cmdEnd;
  87. bool inCmd = false;
  88. bool inQuote = false;
  89. for (unsigned i = 0; i < cmdStr.Length(); ++i)
  90. {
  91. if (cmdStr[i] == '\"')
  92. inQuote = !inQuote;
  93. if ((cmdStr[i] == ' ') && (!inQuote))
  94. {
  95. if (inCmd)
  96. {
  97. inCmd = false;
  98. cmdEnd = i;
  99. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  100. }
  101. }
  102. else
  103. {
  104. if (!inCmd)
  105. {
  106. inCmd = true;
  107. cmdStart = i;
  108. }
  109. }
  110. }
  111. if (inCmd)
  112. {
  113. cmdEnd = cmdStr.Length();
  114. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  115. }
  116. return arguments;
  117. }
  118. const Vector<String>& GetArguments()
  119. {
  120. return arguments;
  121. }
  122. String GetConsoleInput()
  123. {
  124. String ret;
  125. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  126. if (input == INVALID_HANDLE_VALUE)
  127. return ret;
  128. // Use char-based input
  129. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  130. INPUT_RECORD record;
  131. DWORD events = 0;
  132. DWORD readEvents = 0;
  133. if (!GetNumberOfConsoleInputEvents(input, &events))
  134. return ret;
  135. while (events--)
  136. {
  137. ReadConsoleInput(input, &record, 1, &readEvents);
  138. if ((record.EventType == KEY_EVENT) && (record.Event.KeyEvent.bKeyDown))
  139. {
  140. char c = record.Event.KeyEvent.uChar.AsciiChar;
  141. if (c)
  142. {
  143. if (c == '\b')
  144. {
  145. printf("\b \b");
  146. int length = currentLine.Length();
  147. if (length)
  148. currentLine.Resize(length - 1);
  149. }
  150. else if (c == '\r')
  151. {
  152. printf("\n");
  153. ret = currentLine;
  154. currentLine.Clear();
  155. return ret;
  156. }
  157. else
  158. {
  159. // We have disabled echo, so echo manually
  160. printf("%c", c);
  161. currentLine += c;
  162. }
  163. }
  164. }
  165. }
  166. return ret;
  167. }
  168. unsigned GetNumCPUCores()
  169. {
  170. SYSTEM_INFO info;
  171. GetSystemInfo(&info);
  172. return info.dwNumberOfProcessors;
  173. }