M
Matt Garman
I've got some code that generates a report for the user. The report is
shown with explanatory verbage. The text is relatively long, and also
has some simple formatting (paragraphs, bulleted lists).
Right now the code is trivial, similar to the following:
if (5 == result) {
cout << "A value of five was returned because ...\n"
<< "\t- reason 1\n"
<< "\t- reason 2\n ...";
// etc
}
else {
cout << "The result was not five because ...";
<< "\t- reason 1\n"
<< "\t- reason 2\n ...";
// etc
}
Although simple, this makes for long, sloppy-looking functions.
Furthermore, the above example writes text to stdout, but I also have
code that generates an HTML version of the report. So, right now, I
effectively have the exact same code twice, once with text formatting
and again with HTML formatting. (And my long-term vision is to be able
to generate the report in *any* format, e.g. HTML, text, PDF, LaTeX,
etc.)
It seems like there should be a simpler, cleaner, more elegant means to
maintain this report verbage. The first thing that comes to mind is
creating a class of string constants. But, in order to use plain text
and HTML formatting, I'd basically have to devise some kind of grammar
or translation scheme. This seems like a lot of work for a fairly
trivial problem.
Another thought is to maintain the verbage in an XML file, create a
schema and use something like xerces-c to manipulate the XML. Again,
though, this seems like a complex solution for a simple problem.
I'm sure this is a fairly typical/common problem; anyone care to share
their favorite solution(s)?
Thanks!
Matt
shown with explanatory verbage. The text is relatively long, and also
has some simple formatting (paragraphs, bulleted lists).
Right now the code is trivial, similar to the following:
if (5 == result) {
cout << "A value of five was returned because ...\n"
<< "\t- reason 1\n"
<< "\t- reason 2\n ...";
// etc
}
else {
cout << "The result was not five because ...";
<< "\t- reason 1\n"
<< "\t- reason 2\n ...";
// etc
}
Although simple, this makes for long, sloppy-looking functions.
Furthermore, the above example writes text to stdout, but I also have
code that generates an HTML version of the report. So, right now, I
effectively have the exact same code twice, once with text formatting
and again with HTML formatting. (And my long-term vision is to be able
to generate the report in *any* format, e.g. HTML, text, PDF, LaTeX,
etc.)
It seems like there should be a simpler, cleaner, more elegant means to
maintain this report verbage. The first thing that comes to mind is
creating a class of string constants. But, in order to use plain text
and HTML formatting, I'd basically have to devise some kind of grammar
or translation scheme. This seems like a lot of work for a fairly
trivial problem.
Another thought is to maintain the verbage in an XML file, create a
schema and use something like xerces-c to manipulate the XML. Again,
though, this seems like a complex solution for a simple problem.
I'm sure this is a fairly typical/common problem; anyone care to share
their favorite solution(s)?
Thanks!
Matt