templates & reporting

M

Matt Garman

Is there a generally accepted means of generating reports in C
and/or C++?

In particular, I have an application with many parameterized text
strings that get displayed to a user. These could be as simple as
"The value of x is 19.2134", to a lengthy paragraph describing
something in detail.

The strings may appear in plain text form, or need to be put into a
structured document for nicer formatting (e.g. HTML).

The quick and dirty way to accomplish what I want is to just have
inline code, ala

fprintf(report_file, "The value of x is %lf\n", x);

Obviously, if there are any number of these types of reports, it
becomes a maintenance nightmare.

I'd like to keep my raw verbage (e.g. "The value of x is ") in some
type of separate data source (i.e. not part of the source code). Be
it individual plain text files, XML data or even a database, is
fine---it's the mechanism that cleanly (and flexibly) ties the
verbage to the parameters generated by the program.

Scripting languages tend to support this type of thing very
well---particularly PHP, where I can imbed variables in HTML
templates pretty easily.

I'd like this to be relatively simple as well. Some ideas I've had
are along the lines of having a "store" (database, flat files, XML,
etc) of strings, and the strings have special codes to represent
variable data.

For example, I might have a file that contains this:

The value of x is ::VARIABLE_X::

And write some function or class that would know to replace
"::VARIABLE_X::" with something appropriate.

But even with this "solution", the text store has to know something
about the code and/or the code has to know something about the info
contained in the text store.

I'd like as clean a separation between the text store and the code
as possible.

Anyone have any suggestions on ways to achieve something like this?

Thanks!
Matt
 
J

Jianli Shen

not sure if you can make use of following code,

if possible give some feedback/
Thanks


#ifndef REPORTGEN_H
#define REPORTGEN_H

#include <stdio.h>
#include <stdlib.h>

class Report {
private:
static const int MAXREPORTSTACK = 32;

static FILE *rfd[MAXREPORTSTACK];
static int tos;
static FILE *createTmp(const char *name);
Report();
public:
// Creates a new report file. Notice that if the name has the syntax
// .XXXXXX it would modify the XXXXXX for the final file name.
static void openFile(char *name);
static void field(int fn, const char *format,...);
static void field(const char *format,...);
static void close();
static void flush();
};

// Report::field("bla bla bla:",a);

#endif // REPORTGEN_H


////////================= report.cpp ================

#include <alloca.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>

#include "nanassert.h"
#include "ReportGen.h"

FILE *Report::rfd[MAXREPORTSTACK];
int Report::tos=0;

Report::Report()
{
rfd[0]=stdout;
tos=1;
}

void Report::eek:penFile(char *name)
{
I(tos<MAXREPORTSTACK);

FILE *ffd;

if(strstr(name, "XXXXXX")) {
int fd;

fd = mkstemp(name);

ffd = fdopen(fd, "a");
}else{
ffd = fopen(name, "a");
}

if(ffd == 0) {
fprintf(stderr, "NANASSERT::REPORT could not open temporal file [%s]\n",
name);
exit(-3);
}

rfd[tos++]=ffd;
}

void Report::close()
{
if( tos ) {
tos--;
fclose(rfd[tos]);
}
}

void Report::field(int fn, const char *format,...)
{
va_list ap;

I( fn < tos );
FILE *ffd = rfd[fn];

va_start(ap, format);

vfprintf(ffd, format, ap);

va_end(ap);

fprintf(ffd, "\n");
}

void Report::field(const char *format, ...)
{
va_list ap;

I( tos );
FILE *ffd = rfd[tos-1];

va_start(ap, format);

vfprintf(ffd, format, ap);

va_end(ap);

fprintf(ffd, "\n");
}

void Report::flush()
{
if( tos == 0 )
return;

fflush(rfd[tos-1]);
}
 

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

Similar Threads


Members online

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top