ProcessUtils.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "StringUtils.h"
  27. #include <cstdio>
  28. #include <list>
  29. #include <io.h>
  30. #include <fcntl.h>
  31. #include <time.h>
  32. #include <Windows.h>
  33. // Enable SHGetSpecialFolderPath on MinGW
  34. #ifndef _MSC_VER
  35. #define _WIN32_IE 0x0400
  36. #endif
  37. #include <ShlObj.h>
  38. #include "DebugNew.h"
  39. static bool consoleOpened = false;
  40. static String currentLine;
  41. static Vector<String> arguments;
  42. static Mutex staticMutex;
  43. void ErrorDialog(const char* title, const char* message)
  44. {
  45. MessageBox(0, message, title, 0);
  46. }
  47. void ErrorExit(const String& message, int exitCode)
  48. {
  49. printf("%s", message.CString());
  50. exit(exitCode);
  51. }
  52. void OpenConsoleWindow()
  53. {
  54. if (consoleOpened)
  55. return;
  56. AllocConsole();
  57. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  58. int hCrt = _open_osfhandle((long)hOut, _O_TEXT);
  59. FILE* outFile = _fdopen(hCrt, "w");
  60. setvbuf(outFile, NULL, _IONBF, 1);
  61. *stdout = *outFile;
  62. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  63. hCrt = _open_osfhandle((long)hIn, _O_TEXT);
  64. FILE* inFile = _fdopen(hCrt, "r");
  65. setvbuf(inFile, NULL, _IONBF, 128);
  66. *stdin = *inFile;
  67. consoleOpened = true;
  68. }
  69. void PrintLine(const String& str)
  70. {
  71. printf("%s\n", str.CString());
  72. }
  73. void PrintLine(const char* str)
  74. {
  75. printf("%s\n", str);
  76. }
  77. Mutex& GetStaticMutex()
  78. {
  79. return staticMutex;
  80. }
  81. const Vector<String>& ParseArguments(const char* cmdLine)
  82. {
  83. arguments.Clear();
  84. if (!cmdLine)
  85. return arguments;
  86. String cmdStr(cmdLine);
  87. unsigned cmdStart, cmdEnd;
  88. bool inCmd = false;
  89. bool inQuote = false;
  90. for (unsigned i = 0; i < cmdStr.Length(); ++i)
  91. {
  92. if (cmdStr[i] == '\"')
  93. inQuote = !inQuote;
  94. if ((cmdStr[i] == ' ') && (!inQuote))
  95. {
  96. if (inCmd)
  97. {
  98. inCmd = false;
  99. cmdEnd = i;
  100. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  101. }
  102. }
  103. else
  104. {
  105. if (!inCmd)
  106. {
  107. inCmd = true;
  108. cmdStart = i;
  109. }
  110. }
  111. }
  112. if (inCmd)
  113. {
  114. cmdEnd = cmdStr.Length();
  115. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  116. }
  117. return arguments;
  118. }
  119. const Vector<String>& GetArguments()
  120. {
  121. return arguments;
  122. }
  123. String GetConsoleInput()
  124. {
  125. String ret;
  126. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  127. if (input == INVALID_HANDLE_VALUE)
  128. return ret;
  129. // Use char-based input
  130. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  131. INPUT_RECORD record;
  132. DWORD events = 0;
  133. DWORD readEvents = 0;
  134. if (!GetNumberOfConsoleInputEvents(input, &events))
  135. return ret;
  136. while (events--)
  137. {
  138. ReadConsoleInput(input, &record, 1, &readEvents);
  139. if ((record.EventType == KEY_EVENT) && (record.Event.KeyEvent.bKeyDown))
  140. {
  141. char c = record.Event.KeyEvent.uChar.AsciiChar;
  142. if (c)
  143. {
  144. if (c == '\b')
  145. {
  146. printf("\b \b");
  147. int length = currentLine.Length();
  148. if (length)
  149. currentLine.Resize(length - 1);
  150. }
  151. else if (c == '\r')
  152. {
  153. printf("\n");
  154. ret = currentLine;
  155. currentLine.Clear();
  156. return ret;
  157. }
  158. else
  159. {
  160. // We have disabled echo, so echo manually
  161. printf("%c", c);
  162. currentLine += c;
  163. }
  164. }
  165. }
  166. }
  167. return ret;
  168. }
  169. unsigned GetNumCPUCores()
  170. {
  171. SYSTEM_INFO info;
  172. GetSystemInfo(&info);
  173. return info.dwNumberOfProcessors;
  174. }