From 1e3c198b001ee0548b7680339e097967eee92c3e Mon Sep 17 00:00:00 2001 From: Ryan Gang Date: Tue, 28 May 2024 14:58:22 +0530 Subject: [PATCH] CC-1237: Use autoflush using std::unitbuf for c++ repos --- compiled_starters/cpp/src/Server.cpp | 73 +++++++------ solutions/cpp/01-cq2/code/src/Server.cpp | 73 +++++++------ solutions/cpp/01-cq2/diff/src/Server.cpp.diff | 101 +++++++++--------- solutions/cpp/01-cq2/explanation.md | 1 - starter_templates/cpp/src/Server.cpp | 73 +++++++------ 5 files changed, 167 insertions(+), 154 deletions(-) diff --git a/compiled_starters/cpp/src/Server.cpp b/compiled_starters/cpp/src/Server.cpp index 625f12a..e5b1c75 100644 --- a/compiled_starters/cpp/src/Server.cpp +++ b/compiled_starters/cpp/src/Server.cpp @@ -1,45 +1,48 @@ #include #include -bool match_pattern(const std::string& input_line, const std::string& pattern) { - if (pattern.length() == 1) { - return input_line.find(pattern) != std::string::npos; - } - else { - throw std::runtime_error("Unhandled pattern " + pattern); - } +bool match_pattern(const std::string &input_line, const std::string &pattern) { + if (pattern.length() == 1) { + return input_line.find(pattern) != std::string::npos; + } else { + throw std::runtime_error("Unhandled pattern " + pattern); + } } -int main(int argc, char* argv[]) { - // You can use print statements as follows for debugging, they'll be visible when running tests. - std::cout << "Logs from your program will appear here" << std::endl; +int main(int argc, char *argv[]) { + // Flush after every std::cout / std::cerr + std::cout << std::unitbuf; + std::cerr << std::unitbuf; - if (argc != 3) { - std::cerr << "Expected two arguments" << std::endl; - return 1; - } + // You can use print statements as follows for debugging, they'll be visible + // when running tests. + std::cout << "Logs from your program will appear here" << std::endl; - std::string flag = argv[1]; - std::string pattern = argv[2]; + if (argc != 3) { + std::cerr << "Expected two arguments" << std::endl; + return 1; + } - if (flag != "-E") { - std::cerr << "Expected first argument to be '-E'" << std::endl; - return 1; - } + std::string flag = argv[1]; + std::string pattern = argv[2]; - // Uncomment this block to pass the first stage - // - // std::string input_line; - // std::getline(std::cin, input_line); - // - // try { - // if (match_pattern(input_line, pattern)) { - // return 0; - // } else { - // return 1; - // } - // } catch (const std::runtime_error& e) { - // std::cerr << e.what() << std::endl; - // return 1; - // } + if (flag != "-E") { + std::cerr << "Expected first argument to be '-E'" << std::endl; + return 1; + } + + // Uncomment this block to pass the first stage + // std::string input_line; + // std::getline(std::cin, input_line); + // + // try { + // if (match_pattern(input_line, pattern)) { + // return 0; + // } else { + // return 1; + // } + // } catch (const std::runtime_error& e) { + // std::cerr << e.what() << std::endl; + // return 1; + // } } diff --git a/solutions/cpp/01-cq2/code/src/Server.cpp b/solutions/cpp/01-cq2/code/src/Server.cpp index 25a364b..a991314 100644 --- a/solutions/cpp/01-cq2/code/src/Server.cpp +++ b/solutions/cpp/01-cq2/code/src/Server.cpp @@ -1,40 +1,45 @@ #include #include -bool match_pattern(const std::string& input_line, const std::string& pattern) { - if (pattern.length() == 1) { - return input_line.find(pattern) != std::string::npos; - } - else { - throw std::runtime_error("Unhandled pattern " + pattern); - } +bool match_pattern(const std::string &input_line, const std::string &pattern) { + if (pattern.length() == 1) { + return input_line.find(pattern) != std::string::npos; + } else { + throw std::runtime_error("Unhandled pattern " + pattern); + } } -int main(int argc, char* argv[]) { - if (argc != 3) { - std::cerr << "Expected two arguments" << std::endl; - return 1; - } - - std::string flag = argv[1]; - std::string pattern = argv[2]; - - if (flag != "-E") { - std::cerr << "Expected first argument to be '-E'" << std::endl; - return 1; - } - - std::string input_line; - std::getline(std::cin, input_line); - - try { - if (match_pattern(input_line, pattern)) { - return 0; - } else { - return 1; - } - } catch (const std::runtime_error& e) { - std::cerr << e.what() << std::endl; - return 1; - } +int main(int argc, char *argv[]) { + // Flush after every std::cout / std::cerr + std::cout << std::unitbuf; + std::cerr << std::unitbuf; + + std::cout << "Logs from your program will appear here" << std::endl; + + if (argc != 3) { + std::cerr << "Expected two arguments" << std::endl; + return 1; + } + + std::string flag = argv[1]; + std::string pattern = argv[2]; + + if (flag != "-E") { + std::cerr << "Expected first argument to be '-E'" << std::endl; + return 1; + } + + std::string input_line; + std::getline(std::cin, input_line); + + try { + if (match_pattern(input_line, pattern)) { + return 0; + } else { + return 1; + } + } catch (const std::runtime_error& e) { + std::cerr << e.what() << std::endl; + return 1; + } } diff --git a/solutions/cpp/01-cq2/diff/src/Server.cpp.diff b/solutions/cpp/01-cq2/diff/src/Server.cpp.diff index e54accc..1fb7f4b 100644 --- a/solutions/cpp/01-cq2/diff/src/Server.cpp.diff +++ b/solutions/cpp/01-cq2/diff/src/Server.cpp.diff @@ -1,59 +1,62 @@ -@@ -1,45 +1,40 @@ +@@ -1,48 +1,45 @@ #include #include - bool match_pattern(const std::string& input_line, const std::string& pattern) { - if (pattern.length() == 1) { - return input_line.find(pattern) != std::string::npos; - } - else { - throw std::runtime_error("Unhandled pattern " + pattern); - } + bool match_pattern(const std::string &input_line, const std::string &pattern) { + if (pattern.length() == 1) { + return input_line.find(pattern) != std::string::npos; + } else { + throw std::runtime_error("Unhandled pattern " + pattern); + } } - int main(int argc, char* argv[]) { -- // You can use print statements as follows for debugging, they'll be visible when running tests. -- std::cout << "Logs from your program will appear here" << std::endl; -- - if (argc != 3) { - std::cerr << "Expected two arguments" << std::endl; - return 1; - } + int main(int argc, char *argv[]) { + // Flush after every std::cout / std::cerr + std::cout << std::unitbuf; + std::cerr << std::unitbuf; - std::string flag = argv[1]; - std::string pattern = argv[2]; +- // You can use print statements as follows for debugging, they'll be visible +- // when running tests. + std::cout << "Logs from your program will appear here" << std::endl; - if (flag != "-E") { - std::cerr << "Expected first argument to be '-E'" << std::endl; - return 1; - } + if (argc != 3) { + std::cerr << "Expected two arguments" << std::endl; + return 1; + } -- // Uncomment this block to pass the first stage -- // -- // std::string input_line; -- // std::getline(std::cin, input_line); -- // -- // try { -- // if (match_pattern(input_line, pattern)) { -- // return 0; -- // } else { -- // return 1; -- // } -- // } catch (const std::runtime_error& e) { -- // std::cerr << e.what() << std::endl; -- // return 1; -- // } -+ std::string input_line; -+ std::getline(std::cin, input_line); + std::string flag = argv[1]; + std::string pattern = argv[2]; + + if (flag != "-E") { + std::cerr << "Expected first argument to be '-E'" << std::endl; + return 1; + } + +- // Uncomment this block to pass the first stage +- // std::string input_line; +- // std::getline(std::cin, input_line); +- // +- // try { +- // if (match_pattern(input_line, pattern)) { +- // return 0; +- // } else { +- // return 1; +- // } +- // } catch (const std::runtime_error& e) { +- // std::cerr << e.what() << std::endl; +- // return 1; +- // } ++ std::string input_line; ++ std::getline(std::cin, input_line); + -+ try { -+ if (match_pattern(input_line, pattern)) { -+ return 0; -+ } else { -+ return 1; -+ } -+ } catch (const std::runtime_error& e) { -+ std::cerr << e.what() << std::endl; -+ return 1; -+ } ++ try { ++ if (match_pattern(input_line, pattern)) { ++ return 0; ++ } else { ++ return 1; ++ } ++ } catch (const std::runtime_error& e) { ++ std::cerr << e.what() << std::endl; ++ return 1; ++ } } diff --git a/solutions/cpp/01-cq2/explanation.md b/solutions/cpp/01-cq2/explanation.md index 0c1f204..904bb15 100644 --- a/solutions/cpp/01-cq2/explanation.md +++ b/solutions/cpp/01-cq2/explanation.md @@ -4,7 +4,6 @@ Study and uncomment the relevant code: ```cpp // Uncomment this block to pass the first stage - std::string input_line; std::getline(std::cin, input_line); diff --git a/starter_templates/cpp/src/Server.cpp b/starter_templates/cpp/src/Server.cpp index 625f12a..e5b1c75 100644 --- a/starter_templates/cpp/src/Server.cpp +++ b/starter_templates/cpp/src/Server.cpp @@ -1,45 +1,48 @@ #include #include -bool match_pattern(const std::string& input_line, const std::string& pattern) { - if (pattern.length() == 1) { - return input_line.find(pattern) != std::string::npos; - } - else { - throw std::runtime_error("Unhandled pattern " + pattern); - } +bool match_pattern(const std::string &input_line, const std::string &pattern) { + if (pattern.length() == 1) { + return input_line.find(pattern) != std::string::npos; + } else { + throw std::runtime_error("Unhandled pattern " + pattern); + } } -int main(int argc, char* argv[]) { - // You can use print statements as follows for debugging, they'll be visible when running tests. - std::cout << "Logs from your program will appear here" << std::endl; +int main(int argc, char *argv[]) { + // Flush after every std::cout / std::cerr + std::cout << std::unitbuf; + std::cerr << std::unitbuf; - if (argc != 3) { - std::cerr << "Expected two arguments" << std::endl; - return 1; - } + // You can use print statements as follows for debugging, they'll be visible + // when running tests. + std::cout << "Logs from your program will appear here" << std::endl; - std::string flag = argv[1]; - std::string pattern = argv[2]; + if (argc != 3) { + std::cerr << "Expected two arguments" << std::endl; + return 1; + } - if (flag != "-E") { - std::cerr << "Expected first argument to be '-E'" << std::endl; - return 1; - } + std::string flag = argv[1]; + std::string pattern = argv[2]; - // Uncomment this block to pass the first stage - // - // std::string input_line; - // std::getline(std::cin, input_line); - // - // try { - // if (match_pattern(input_line, pattern)) { - // return 0; - // } else { - // return 1; - // } - // } catch (const std::runtime_error& e) { - // std::cerr << e.what() << std::endl; - // return 1; - // } + if (flag != "-E") { + std::cerr << "Expected first argument to be '-E'" << std::endl; + return 1; + } + + // Uncomment this block to pass the first stage + // std::string input_line; + // std::getline(std::cin, input_line); + // + // try { + // if (match_pattern(input_line, pattern)) { + // return 0; + // } else { + // return 1; + // } + // } catch (const std::runtime_error& e) { + // std::cerr << e.what() << std::endl; + // return 1; + // } }