ProcessUtils.cpp 5.5 KB

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