aboutsummaryrefslogtreecommitdiff
path: root/src/report/report-maker.cpp
diff options
context:
space:
mode:
authorIgor Zhbanov <i.zhbanov@samsung.com>2012-10-09 20:43:20 +0400
committerChris E Ferron <chris.e.ferron@linux.intel.com>2012-10-10 08:39:14 -0700
commit03948718c1ffc35860b6663f0d04a15b6c87aaba (patch)
treee5c4e2b55092a7e7c5fba198423601ad61cf9562 /src/report/report-maker.cpp
parent72ff7dfe7222fddf784bc866bfe7f1ec226e407d (diff)
Adding report generator facility (v2)
This report generator adds the facility to generate correctly parsable reports in HTML and CSV format. It separates document structure from text formatting, enhances code readability and simplifies report generating. Also it makes possible adding other formats such as TXT or XML. This report generator implements the following document structure: body \---> section |---> header |---> paragraph \---> table \---> table row \---> table cell The report body consists of a number of sections (a.k.a. <div>s, a.k.a. tabs). Each section can contain headers (<h1>, <h2>, <h3>), paragraphs (<p>) and tables (<table>). A header is a single line of text. Paragraphs can contain only text. A table consists of table rows. A table row consists of table cells. A table cell can contain only text. Each section, table, row or cell could have its own formatting. To distinguish elements from others of its type, each element could have an unique identifier (see enums section_type, table_type, row_type and cell_type below). These identifiers are used in formatter implementations to produce special formatting. Example of usage: report_maker report(REPORT_OFF); report.set_type(REPORT_HTML); report.begin_section(); report.add_header("Big report"); report.begin_paragraph("Some text"); report.begin_table(); report.begin_row(); report.begin_cell(); report.add("Something"); report.begin_cell(CELL_SPECIAL); report.add("Foo bar"); report.finish_report(); const char *result = report.get_result();
Diffstat (limited to 'src/report/report-maker.cpp')
-rw-r--r--src/report/report-maker.cpp331
1 files changed, 331 insertions, 0 deletions
diff --git a/src/report/report-maker.cpp b/src/report/report-maker.cpp
new file mode 100644
index 0000000..4a68a8c
--- /dev/null
+++ b/src/report/report-maker.cpp
@@ -0,0 +1,331 @@
+/* Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ * http://www.samsung.com/
+ *
+ * This file is part of PowerTOP
+ *
+ * This program file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program in a file named COPYING; if not, write to the
+ * Free Software Foundation, Inc,
+ * 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ * or just google for it.
+ *
+ * Generic report generator.
+ * Written by Igor Zhbanov <i.zhbanov@samsung.com>
+ * 2012.10 */
+
+#define NDEBUG /* Enable asserts */
+
+#include <assert.h>
+#include <stdarg.h>
+
+#include "report-maker.h"
+#include "report-formatter-csv.h"
+#include "report-formatter-html.h"
+#include "report-formatter-null.h"
+
+/* ************************************************************************ */
+
+report_maker::report_maker(report_type t)
+{
+ type = t;
+ formatter = NULL;
+ setup_report_formatter();
+ clear_result(); /* To reset state and add document header */
+}
+
+/* ************************************************************************ */
+
+report_maker::~report_maker()
+{
+ if (formatter)
+ delete formatter;
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::finish_report()
+{
+ if (section_opened)
+ end_section();
+
+ formatter->finish_report();
+}
+
+/* ************************************************************************ */
+
+const char *
+report_maker::get_result()
+{
+ return formatter->get_result();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::clear_result()
+{
+ formatter->clear_result();
+ section_opened = false;
+ table_opened = false;
+ cell_opened = false;
+ row_opened = false;
+ paragraph_opened = false;
+}
+
+/* ************************************************************************ */
+
+report_type
+report_maker::get_type()
+{
+ return type;
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::set_type(report_type t)
+{
+ clear_result();
+ type = t;
+ setup_report_formatter();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::setup_report_formatter()
+{
+ if (formatter)
+ delete formatter;
+
+ if (type == REPORT_HTML)
+ formatter = new report_formatter_html();
+ else if (type == REPORT_CSV)
+ formatter = new report_formatter_csv();
+ else if (type == REPORT_OFF)
+ formatter = new report_formatter_null();
+ else
+ assert(false);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::add(const char *str)
+{
+ assert(str);
+ assert(section_opened);
+
+ formatter->add(str);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::addf(const char *fmt, ...)
+{
+ va_list ap;
+
+ assert(fmt);
+ assert(section_opened);
+
+ va_start(ap, fmt);
+ formatter->addv(fmt, ap);
+ va_end(ap);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::add_header(const char *header, int level)
+{
+ assert(header);
+ assert(section_opened);
+ assert(level > 0 && level < 4);
+
+ if (table_opened)
+ end_table();
+ else if (paragraph_opened)
+ end_paragraph();
+
+ formatter->add_header(header, level);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::begin_section(section_type stype)
+{
+ assert(stype >= 0 && stype < SECTION_MAX);
+
+ if (section_opened)
+ end_section(); /* Close previous */
+
+ section_opened = true;
+ formatter->begin_section(stype);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::end_section()
+{
+ assert(section_opened);
+
+ if (table_opened)
+ end_table();
+ else if (paragraph_opened)
+ end_paragraph();
+
+ section_opened = false;
+ formatter->end_section();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::begin_table(table_type ttype)
+{
+ assert(ttype >= 0 && ttype < TABLE_MAX);
+ assert(section_opened);
+
+ if (table_opened)
+ end_table(); /* Close previous */
+ else if (paragraph_opened)
+ end_paragraph();
+
+ table_opened = true;
+ formatter->begin_table(ttype);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::end_table()
+{
+ assert(section_opened);
+ assert(table_opened);
+
+ if (row_opened)
+ end_row();
+
+ table_opened = false;
+ formatter->end_table();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::begin_row(row_type rtype)
+{
+ assert(section_opened);
+ assert(table_opened);
+ assert(rtype >= 0 && rtype < ROW_MAX);
+
+ if (row_opened)
+ end_row(); /* Close previous */
+
+ row_opened = true;
+ formatter->begin_row(rtype);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::end_row()
+{
+ assert(section_opened);
+ assert(table_opened);
+ assert(row_opened);
+
+ if (cell_opened)
+ end_cell();
+
+ row_opened = false;
+ formatter->end_row();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::begin_cell(cell_type ctype)
+{
+ assert(section_opened);
+ assert(table_opened);
+ assert(row_opened);
+ assert(ctype >= 0 && ctype < CELL_MAX);
+
+ if (cell_opened)
+ end_cell(); /* Close previous */
+
+ cell_opened = true;
+ formatter->begin_cell(ctype);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::end_cell()
+{
+ assert(section_opened);
+ assert(table_opened);
+ assert(row_opened);
+ assert(cell_opened);
+
+ cell_opened = false;
+ formatter->end_cell();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::add_empty_cell()
+{
+ formatter->add_empty_cell();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::set_cpu_number(int nr)
+{
+ formatter->set_cpu_number(nr);
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::begin_paragraph()
+{
+ assert(section_opened);
+
+ if (table_opened)
+ end_table();
+ else if (paragraph_opened)
+ end_paragraph(); /* Close previous */
+
+ paragraph_opened = true;
+ formatter->begin_paragraph();
+}
+
+/* ************************************************************************ */
+
+void
+report_maker::end_paragraph()
+{
+ assert(paragraph_opened);
+
+ paragraph_opened = false;
+ formatter->end_paragraph();
+}