您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

childProgram.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <iostream>
  2. #ifdef DEBUG_MESSAGES
  3. #include <fstream> // debug
  4. #endif
  5. #include <string>
  6. #include <vector>
  7. #include <thread>
  8. #include <chrono>
  9. int
  10. main(int argc, char *argv[]) {
  11. #ifdef DEBUG_MESSAGES
  12. std::ofstream log("childProgram.log");
  13. #endif
  14. int returnStatus = 25; // Successful return.
  15. // Output for read test.
  16. if (argc == 2) {
  17. // Write a single line and exit.
  18. if (std::string(argv[1]) == "write") {
  19. #ifdef DEBUG_MESSAGES
  20. log << "Command is \"write\". Returning \"This is a test\""
  21. << " to stdout and stderr." << std::endl;
  22. #endif
  23. std::cout << "This ";
  24. std::cout.flush();
  25. std::cerr << "is a";
  26. std::cout << " test";
  27. std::cout.flush();
  28. if (!std::cout) {
  29. returnStatus = 10; // Unsuccessful return.
  30. }
  31. goto exit;
  32. }
  33. // Exit without writing anything.
  34. if (std::string(argv[1]) == "quit") {
  35. #ifdef DEBUG_MESSAGES
  36. log << "Command is \"quit\". Exiting" << std::endl;
  37. #endif
  38. goto exit;
  39. }
  40. // Wait for standard input to close, and exit.
  41. if (std::string(argv[1]) == "checkStdinEOF") {
  42. #ifdef DEBUG_MESSAGES
  43. log << "Command is \"checkStdinEOF\". Checking that stdin is closed."
  44. << std::endl;
  45. #endif
  46. int const BufferSize = 512;
  47. std::vector<char> readBuffer;
  48. readBuffer.resize(BufferSize, 0);
  49. std::cin.read(&(readBuffer[0]), BufferSize);
  50. if (!std::cin.eof()) {
  51. #ifdef DEBUG_MESSAGES
  52. log << "stdin was not closed." << std::endl;
  53. #endif
  54. returnStatus = 10; // Unsuccessful return.
  55. } else {
  56. #ifdef DEBUG_MESSAGES
  57. log << "stdin was closed." << std::endl;
  58. #endif
  59. returnStatus = 15; // Successful return.
  60. }
  61. goto exit;
  62. }
  63. }
  64. char ch;
  65. #ifdef DEBUG_MESSAGES
  66. log << "Received \"";
  67. #endif
  68. while (std::cin >> ch) {
  69. // Exit?
  70. if ('q' == ch) {
  71. #ifdef DEBUG_MESSAGES
  72. log << (char) ch << "\"" << std::endl;
  73. #endif
  74. break;
  75. }
  76. std::cout << (char) std::toupper(ch);
  77. std::cout.flush();
  78. #ifdef DEBUG_MESSAGES
  79. log << (char) ch;
  80. log.flush();
  81. #endif
  82. }
  83. exit:
  84. #ifdef DEBUG_MESSAGES
  85. log.close();
  86. #endif
  87. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  88. return returnStatus;
  89. }