testUVProcessChainHelper.cxx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <cctype>
  2. #include <chrono>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <set>
  6. #include <sstream>
  7. #include <string>
  8. #include <thread>
  9. #include "cmsys/SystemTools.hxx"
  10. #ifdef _WIN32
  11. # include <windows.h>
  12. #endif
  13. static std::string getStdin()
  14. {
  15. char buffer[1024];
  16. std::ostringstream str;
  17. do {
  18. std::cin.read(buffer, 1024);
  19. str.write(buffer, std::cin.gcount());
  20. } while (std::cin.gcount() > 0);
  21. return str.str();
  22. }
  23. int main(int argc, char** argv)
  24. {
  25. if (argc < 2) {
  26. return -1;
  27. }
  28. std::string command = argv[1];
  29. if (command == "echo") {
  30. std::this_thread::sleep_for(std::chrono::milliseconds(6000));
  31. std::cout << "HELLO world!" << std::flush;
  32. std::cerr << "1" << std::flush;
  33. return 0;
  34. }
  35. if (command == "capitalize") {
  36. std::this_thread::sleep_for(std::chrono::milliseconds(12000));
  37. std::string input = getStdin();
  38. for (auto& c : input) {
  39. c = static_cast<char>(std::toupper(c));
  40. }
  41. std::cout << input << std::flush;
  42. std::cerr << "2" << std::flush;
  43. return 1;
  44. }
  45. if (command == "dedup") {
  46. // Use a nested scope to free all resources before aborting below.
  47. try {
  48. std::string input = getStdin();
  49. std::set<char> seen;
  50. std::string output;
  51. for (auto c : input) {
  52. if (!seen.count(c)) {
  53. seen.insert(c);
  54. output += c;
  55. }
  56. }
  57. std::cout << output << std::flush;
  58. std::cerr << "3" << std::flush;
  59. } catch (...) {
  60. }
  61. // On Windows, the exit code of abort() is different between debug and
  62. // release builds. Instead, simulate an access violation.
  63. #ifdef _WIN32
  64. return STATUS_ACCESS_VIOLATION;
  65. #else
  66. std::abort();
  67. #endif
  68. }
  69. if (command == "pwd") {
  70. std::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
  71. std::cout << cwd << std::flush;
  72. return 0;
  73. }
  74. return -1;
  75. }