Template of sending text

Hi,

I am writing some code to send text to a third part software. Basically each sending looks like this:
1
2
3
4
5
6
7
8
9
p << "set terminal eps\n";
p << "set output '07.eps'\n";
p << "plot '-' using ($1 == 0 ? NaN : $1) with lines linecolor 2 title 'comparison ratio', \
	'-' using ($1 == 0 ? NaN : $1) with lines title 'comparison ratio'\n";
p.send(TOex_ar).send(TOnew_ar);
p << "set terminal wxt 7\n";
p << "plot '-' using ($1 == 0 ? NaN : $1) with lines linecolor 2 title 'comparison ratio', \
	'-' using ($1 == 0 ? NaN : $1) with lines title 'comparison ratio'\n";
p.send(TOex_ar).send(TOnew_ar);

Each time I have to change the number "07" and "7";
decide to add or not the following parts: "using ($1 == 0 ? NaN : $1)", "with lines", "linecolor 2";
and write the title 'comparison ratio' and the name of data to send "TOex_ar" and "TOnew_ar".

Since I have to do this kind of thing 50 times in my code, and it is in the form of text, I am wondering whether in C++ we can write a template or function to simplify the program, just to input the changing parts.

Could someone please help me? Thanks!
You don't need to template it. Just send the variable data as parameters and construct the string dynamically.

IE... something like....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void output(std::ostream& p,
    int outputNum,  // the '0' / '07'
    bool showUsing, // whether to show 'using ($1 ...'
    bool showLines, // whether to show 'with lines'
    ... etc   )
{
  p << "set terminal eps\n";
  p << "set output '" << std::setfill('0') << std::setw(2) << outputNum << ".eps'\n";
  p << "plot '-'";
  if( showUsing )  p << " using ($1 == 0 ? NaN : $1)";
  if( showLines )  p << " with lines";

  //... etc
}
Last edited on
Thanks Disch! Ouah that really simplifies the coding... :)
Oh, I have some problem about how to make the last line:
 
p.send(TOex_ar).send(TOnew_ar);

It is actually a command with the function "send" defined by an external header. It did not work if I put it inside the text sending function.

Could someone please help me? Thanks !
Topic archived. No new replies allowed.