ProcessUtils.cpp 5.5 KB

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