clean way to organize string constants (for user feedback)?

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
 
?

=?ISO-8859-2?Q?Milan_=C8erm=E1k?=

I think the way is to write set of formaters (all with common abstract
parent). The formater will take the text (containing your own formating
marks) and answer text with formating marks for specific output.

The only thing the client is supposed to do is to construct new formater
(for wanted output) and let it format the text.

As the advantage new format can be add by creating new formater.

Milan Cermak
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top