based on 3rd party lib (http://pstreams.sourceforge.net/)
| 17 | |
| 18 | // based on 3rd party lib (http://pstreams.sourceforge.net/) |
| 19 | int executeCommand(const std::string &cmd, const std::vector<std::string> &args, |
| 20 | std::string *pOutputStr, bool appendFromStdErr) |
| 21 | { |
| 22 | std::string cmdLine = cmd; |
| 23 | |
| 24 | for (auto it = args.begin(); it != args.end(); ++it) { |
| 25 | cmdLine += " '"; |
| 26 | for (char c : *it) { |
| 27 | if (c == '\'') { |
| 28 | cmdLine += "'\\''"; |
| 29 | } else { |
| 30 | cmdLine += c; |
| 31 | } |
| 32 | } |
| 33 | cmdLine += "'"; |
| 34 | } |
| 35 | |
| 36 | if (pOutputStr) { |
| 37 | pOutputStr->clear(); |
| 38 | } |
| 39 | |
| 40 | redi::ipstream proc(cmdLine, redi::pstreams::pstdout | redi::pstreams::pstderr); |
| 41 | std::string line; |
| 42 | // read child's stdout |
| 43 | while (std::getline(proc.out(), line)) { |
| 44 | if (pOutputStr) { |
| 45 | *pOutputStr += line + "\n"; |
| 46 | } |
| 47 | } |
| 48 | // if reading stdout stopped at EOF then reset the state: |
| 49 | if (proc.eof() && proc.fail()) { |
| 50 | proc.clear(); |
| 51 | } |
| 52 | |
| 53 | if (appendFromStdErr) { |
| 54 | // read child's stderr |
| 55 | while (std::getline(proc.err(), line)) { |
| 56 | if (pOutputStr) { |
| 57 | *pOutputStr += line + "\n"; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | proc.close(); |
| 63 | if (proc.rdbuf()->exited()) { |
| 64 | // status() is the raw wait-status from waitpid (e.g. exit code 2 -> 512), which |
| 65 | // is misleading when logged. Normalize to the real exit code so callers and logs |
| 66 | // see what the command actually returned. exited() == WIFEXITED, so WEXITSTATUS |
| 67 | // is well-defined here. |
| 68 | return WEXITSTATUS(proc.rdbuf()->status()); |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | size_t findCaseInsensitive(std::string data, std::string toSearch, size_t pos) |
no test coverage detected