|
| 1 | +/* |
| 2 | + * This program is free software: you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU Lesser General Public License as published by |
| 4 | + * the Free Software Foundation, either version 3 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU Lesser General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU Lesser General Public License |
| 13 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + * |
| 15 | + * URL: https://github.com/martinruenz/gnuplot-cpp |
| 16 | + * AUTHOR: Martin Rünz, 2015 |
| 17 | + */ |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | +#include <iostream> |
| 25 | +#include <fstream> |
| 26 | + |
| 27 | +class GnuplotPipe { |
| 28 | +public: |
| 29 | + inline GnuplotPipe(bool persist = true) { |
| 30 | + std::cout << "Opening gnuplot... "; |
| 31 | + pipe = popen(persist ? "gnuplot -persist" : "gnuplot", "w"); |
| 32 | + if (!pipe) |
| 33 | + std::cout << "failed!" << std::endl; |
| 34 | + else |
| 35 | + std::cout << "succeded." << std::endl; |
| 36 | + } |
| 37 | + inline virtual ~GnuplotPipe(){ |
| 38 | + if (pipe) pclose(pipe); |
| 39 | + } |
| 40 | + |
| 41 | + void sendLine(const std::string& text, bool useBuffer = false){ |
| 42 | + if (!pipe) return; |
| 43 | + if (useBuffer) |
| 44 | + buffer.push_back(text + "\n"); |
| 45 | + else |
| 46 | + fputs((text + "\n").c_str(), pipe); |
| 47 | + } |
| 48 | + void sendEndOfData(unsigned repeatBuffer = 1){ |
| 49 | + if (!pipe) return; |
| 50 | + for (unsigned i = 0; i < repeatBuffer; i++) { |
| 51 | + for (auto& line : buffer) fputs(line.c_str(), pipe); |
| 52 | + fputs("e\n", pipe); |
| 53 | + } |
| 54 | + fflush(pipe); |
| 55 | + buffer.clear(); |
| 56 | + } |
| 57 | + void sendNewDataBlock(){ |
| 58 | + sendLine("\n", !buffer.empty()); |
| 59 | + } |
| 60 | + |
| 61 | + void writeBufferToFile(const std::string& fileName){ |
| 62 | + std::ofstream fileOut(fileName); |
| 63 | + for (auto& line : buffer) fileOut << line; |
| 64 | + fileOut.close(); |
| 65 | + } |
| 66 | + |
| 67 | +private: |
| 68 | + GnuplotPipe(GnuplotPipe const&) = delete; |
| 69 | + void operator=(GnuplotPipe const&) = delete; |
| 70 | + |
| 71 | + FILE* pipe; |
| 72 | + std::vector<std::string> buffer; |
| 73 | +}; |
0 commit comments