ProcessUtils.cpp 5.5 KB

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