[ create a new paste ] login | about

Link: http://codepad.org/5v7DKGaM    [ raw code | fork ]

hecomi - C++, pasted on Jul 14:
#pragma warning(disable: 4996)

#include <stdio.h>
#include <stdarg.h>
#include <iostream>
#include <sstream>
#include "gnuplot.h"

const unsigned int CGnuplot::DefaultNo = 1;
const char* CGnuplot::InitialStyleFileName = "macro/initial.txt";
const char* CGnuplot::OutputStyleFileName  = "macro/output_line.txt";

CGnuplot::CGnuplot()
: TempFileName("Temp"), No(DefaultNo), MaxNo(DefaultNo), IfMultiplot(false), PlotType(PLOT_TYPE_LINES)
{
	Ini();
}

CGnuplot::CGnuplot(const char* fileName)
: TempFileName(fileName), No(DefaultNo), MaxNo(DefaultNo), IfMultiplot(false), PlotType(PLOT_TYPE_LINES)
{
	Ini();
}

void CGnuplot::Ini()
{
	Fp = _popen("pgnuplot", "w");
	if (Fp == NULL) {
		printf("pipe error\n");
		exit(EXIT_FAILURE);
	}
	CommandFromFile(InitialStyleFileName);
}

CGnuplot::~CGnuplot()
{
	Command("exit");

	// プロットファイルの削除
	for (No=DefaultNo; No<=MaxNo; No++) {
		remove(GetPlotFileName().c_str());
	}

	_pclose(Fp);
}

bool CGnuplot::Check()
{
	if (Fp == NULL) {
		return false;
	}
	return true;
}

std::string CGnuplot::GetPlotFileName()
{
	std::stringstream ss;
	ss << TempFileName << No << ".dat";
	return ss.str();
}

void CGnuplot::Plot()
{
	Command("plot '%s' w %s ls %d notitle", GetPlotFileName().c_str(), GetPlotType().c_str(), No);

	// マルチプロットの場合は番号をセット
	if (IfMultiplot) {
		No++;
		if (No > MaxNo) {
			MaxNo = No;
		}
	}
}

std::string CGnuplot::GetPlotType()
{
	switch (PlotType) {
		case PLOT_TYPE_LINES:			return "lines";			break;
		case PLOT_TYPE_POINTS:			return "points";		break;
		case PLOT_TYPE_LINES_POINTS:	return "linespoints";	break;
		default:						return "lines";
	}
}

void CGnuplot::Command(const char* format, ...)
{
	char buf[1024];
	va_list ap;

	va_start(ap, format);
	vsprintf(buf, format, ap);
	va_end(ap);

	fprintf(Fp, "%s\n", buf);
	Flush();
}

void CGnuplot::DrawFunc(const char* func)
{
	Command("plot %s", func);
}

void CGnuplot::SetPlotType(PLOT_TYPE pt)
{
	PlotType = pt;
}

void CGnuplot::SetMultiplot(bool sw)
{
	if (sw) {
		Command("set multiplot");
		IfMultiplot = true;

	} else {
		Command("unset multiplot");
		IfMultiplot = false;
		No = DefaultNo;
	}
}

void CGnuplot::SetTitle(const char* title)
{
	Command("set title '%s'", title);
}

void CGnuplot::SetLabel(const char* labelX, const char* labelY)
{
	Command("set xlabel '%s'", labelX);
	Command("set ylabel '%s'", labelY);
}

void CGnuplot::SetXRange(const double min, const double max)
{
	Command("set xrange [%f:%f]", min, max);
}

void CGnuplot::SetYRange(const double min, const double max)
{
	Command("set yrange [%f:%f]", min, max);
}

void CGnuplot::Flush()
{
	fflush(Fp);
}

void CGnuplot::Replot()
{
	Command("replot");
}

void CGnuplot::Reset()
{
	Command("reset");
}

void CGnuplot::SetLogPlotY(bool sw)
{
	if (sw) {
		Command("set logscale y");
		Command("set format y \"10^{%%L}\"");
	} else {
		Command("set nolog y");
		Command("set format y");
	}

}


void CGnuplot::DumpToFile(const char* fileName)
{
	std::ofstream fout(fileName, std::ios_base::binary);
	std::ifstream fin(GetPlotFileName().c_str(), std::ios_base::binary);
	if (fin.fail() || fout.fail()) {
		std::cout << "Error! (@DumpToFile)" << std::endl;
		return;
	}
	while (!fin.eof()) {
		const int BUF_SIZE = 4096;
		char buf[BUF_SIZE];
		fin.read(buf, BUF_SIZE);
		fout.write(buf, BUF_SIZE);
	}
}

void CGnuplot::DumpToEps(const char* fileName)
{
	CommandFromFile(OutputStyleFileName);
	Command("set term postscript eps enhanced color \"Tahoma\" 35");
	Command("set output '%s.eps'", fileName);
	Command("set size 1.6,1.6");
	Command("set ticscale 3");
	Command("replot");
	Command("set output");
	Command("set terminal window");
	Command("set ticscale");
	Command("set size");
	CommandFromFile(InitialStyleFileName);
}

void CGnuplot::DumpToPng(const char* fileName)
{
	Command("set term png");
	Command("set output '%s.png'", fileName);
	Command("set size");
	Command("set ticscale 3");
	Command("replot");
	Command("set output");
	Command("set terminal window");
	Command("set ticscale");
	CommandFromFile(InitialStyleFileName);
}

void CGnuplot::CommandFromFile(const char* fileName)
{
	std::ifstream fin(fileName);
	if (fin.fail()) {
		std::cout << "Error! '" << fileName << "' is not found. (@CommandFromFile)" << std::endl;
		return;
	}
	std::string str;
	while (!fin.eof()) {
		std::getline(fin, str);
		Command(str.c_str());
	}
}


Create a new paste based on this one


Comments: