Skip to content

Commit 28cf19a

Browse files
committed
Extending REFORMAT() to also allow row-based min/max highlighting.
1 parent 63974e7 commit 28cf19a

4 files changed

Lines changed: 100 additions & 45 deletions

File tree

src/reformat.cpp

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ void Reformat::Cell::apply(const Cell& c)
353353
m_grouping = c.m_grouping;
354354
}
355355

356-
//! Test if we need to read the column data
357-
bool Reformat::Column::readdata() const
356+
//! Test if we need to read the row/column data
357+
bool Reformat::Line::readdata() const
358358
{
359359
if (m_min_format == MF_BOLD || m_min_format == MF_EMPH)
360360
return true;
@@ -366,20 +366,20 @@ bool Reformat::Column::readdata() const
366366
}
367367

368368
//! Check for valid min/max format
369-
Reformat::Column::minmax_format_type
370-
Reformat::Column::parse_minmax(const std::string& key, const std::string& value)
369+
Reformat::Line::minmax_format_type
370+
Reformat::Line::parse_minmax(const std::string& key, const std::string& value)
371371
{
372372
if (value == "") return MF_NONE;
373373
if (value == "bold" || value == "bf") return MF_BOLD;
374374
if (value == "emph" || value == "em") return MF_EMPH;
375375

376-
OUT_THROW("Invalid formatting for column key " << key << ": " << value);
376+
OUT_THROW("Invalid formatting for row/column key " << key << ": " << value);
377377
}
378378

379-
//! Parse column-level key=value format
380-
bool Reformat::Column::parse_keyformat(const std::string& key,
381-
std::string::const_iterator& curr,
382-
const std::string::const_iterator& end)
379+
//! Parse row/column-level key=value format
380+
bool Reformat::Line::parse_keyformat(const std::string& key,
381+
std::string::const_iterator& curr,
382+
const std::string::const_iterator& end)
383383
{
384384
if (key == "min" || key == "minimum")
385385
{
@@ -399,30 +399,30 @@ bool Reformat::Column::parse_keyformat(const std::string& key,
399399
}
400400
else
401401
{
402-
OUT_THROW("Invalid column-level format key: " << key);
402+
OUT_THROW("Invalid row/column-level format key: " << key);
403403
}
404404

405405
return true;
406406
}
407407

408-
//! Parse column-level formating arguments
409-
bool Reformat::Column::parse_format(const std::string& format)
408+
//! Parse row/column-level formating arguments
409+
bool Reformat::Line::parse_format(const std::string& format)
410410
{
411411
std::string::const_iterator curr = format.begin();
412412
std::string key;
413413

414414
while (parse_keyword(curr, format.end(), key))
415415
{
416-
DBG("column-level format key: " << key << "-");
416+
DBG("row/column-level format key: " << key << "-");
417417

418418
parse_keyformat(key, curr, format.end());
419419
}
420420

421421
return true;
422422
}
423423

424-
//! Apply formats of other column-level object
425-
void Reformat::Column::apply(const Column& c)
424+
//! Apply formats of other row/column-level object
425+
void Reformat::Line::apply(const Line& c)
426426
{
427427
if (c.m_min_format != MF_UNDEF)
428428
{
@@ -494,13 +494,30 @@ bool Reformat::parse_format(const std::string& format)
494494

495495
std::set<unsigned> cols = parse_numbers(arg);
496496

497-
Column cformat;
498-
cformat.parse_format(parse_keyvalue(curr, format.end()));
497+
Line colfmt;
498+
colfmt.parse_format(parse_keyvalue(curr, format.end()));
499499

500500
for (std::set<unsigned>::iterator c = cols.begin();
501501
c != cols.end(); ++c)
502502
{
503-
m_colfmt[*c].apply(cformat);
503+
m_colfmt[*c].apply(colfmt);
504+
}
505+
}
506+
else if (key == "row" || key == "rows")
507+
{
508+
std::string arg = parse_keyargument(curr, format.end());
509+
510+
DBG("top-level format key: " << key << " arg: " << arg << "-");
511+
512+
std::set<unsigned> rows = parse_numbers(arg);
513+
514+
Line rowfmt;
515+
rowfmt.parse_format(parse_keyvalue(curr, format.end()));
516+
517+
for (std::set<unsigned>::iterator r = rows.begin();
518+
r != rows.end(); ++r)
519+
{
520+
m_rowfmt[*r].apply(rowfmt);
504521
}
505522
}
506523
else if (m_fmt.parse_keyformat(key, curr, format.end()))
@@ -523,7 +540,8 @@ void Reformat::prepare(const SqlQuery& sql)
523540
{
524541
for (unsigned j = 0; j < sql->num_cols(); ++j)
525542
{
526-
if (!m_colfmt[j].readdata() && !m_fmt.readdata())
543+
if (!m_rowfmt[i].readdata() && !m_colfmt[j].readdata() &&
544+
!m_fmt.readdata())
527545
continue;
528546

529547
std::string text = sql->text(i,j);
@@ -532,6 +550,18 @@ void Reformat::prepare(const SqlQuery& sql)
532550
double v;
533551
if (from_str(text, v))
534552
{
553+
if (v < m_rowfmt[i].m_min_value)
554+
{
555+
m_rowfmt[i].m_min_value = v;
556+
m_rowfmt[i].m_min_text = text;
557+
}
558+
559+
if (v > m_rowfmt[i].m_max_value)
560+
{
561+
m_rowfmt[i].m_max_value = v;
562+
m_rowfmt[i].m_max_text = text;
563+
}
564+
535565
if (v < m_colfmt[j].m_min_value)
536566
{
537567
m_colfmt[j].m_min_value = v;
@@ -573,11 +603,17 @@ std::string Reformat::format(int row, int col, const std::string& in_text) const
573603
double v;
574604
if (from_str(text, v))
575605
{
576-
Column fmt = m_fmt;
606+
Line fmt = m_fmt;
577607

578-
colfmt_type::const_iterator colfmt = m_colfmt.find(col);
579-
if (colfmt != m_colfmt.end())
580-
fmt.apply(colfmt->second);
608+
{
609+
linefmt_type::const_iterator rowfmt = m_rowfmt.find(row);
610+
if (rowfmt != m_rowfmt.end())
611+
fmt.apply(rowfmt->second);
612+
613+
linefmt_type::const_iterator colfmt = m_colfmt.find(col);
614+
if (colfmt != m_colfmt.end())
615+
fmt.apply(colfmt->second);
616+
}
581617

582618
// *** Round Double Number ***
583619

@@ -655,22 +691,22 @@ std::string Reformat::format(int row, int col, const std::string& in_text) const
655691

656692
text = replace_all(text, ",", fmt.m_grouping);
657693

658-
// *** check for column minimum or maximum formatting ***
694+
// *** check for row/column minimum or maximum formatting ***
659695

660696
DBG("fmt: " << in_text << " - " << fmt.m_min_text);
661697

662698
if (in_text == fmt.m_min_text)
663699
{
664-
if (fmt.m_min_format == Column::MF_BOLD)
700+
if (fmt.m_min_format == Line::MF_BOLD)
665701
text = "\\bf " + text;
666-
else if (fmt.m_min_format == Column::MF_EMPH)
702+
else if (fmt.m_min_format == Line::MF_EMPH)
667703
text = "\\em " + text;
668704
}
669705
else if (in_text == fmt.m_max_text)
670706
{
671-
if (fmt.m_max_format == Column::MF_BOLD)
707+
if (fmt.m_max_format == Line::MF_BOLD)
672708
text = "\\bf " + text;
673-
else if (fmt.m_max_format == Column::MF_EMPH)
709+
else if (fmt.m_max_format == Line::MF_EMPH)
674710
text = "\\em " + text;
675711
}
676712
}

src/reformat.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,57 +124,60 @@ class Reformat
124124
void apply(const Cell& c);
125125
};
126126

127-
//! Specifications for column-level formats
128-
struct Column : public Cell
127+
//! Specifications for row- or column-level formats
128+
struct Line : public Cell
129129
{
130130
//! Enum to specify formatting of min/max in column
131131
enum minmax_format_type { MF_UNDEF, MF_NONE, MF_BOLD, MF_EMPH };
132132

133133
//! Formatting of minimum or maximum
134134
minmax_format_type m_min_format, m_max_format;
135135

136-
//! Minimum and maximum values in column.
136+
//! Minimum and maximum values in row/column.
137137
double m_min_value, m_max_value;
138138

139-
//! Minimum and maximum values in column, as text for matching.
139+
//! Minimum and maximum values in row/column, as text for matching.
140140
std::string m_min_text, m_max_text;
141141

142-
//! Initialize column min/max with sentinels
143-
Column()
142+
//! Initialize row/column min/max with sentinels
143+
Line()
144144
: m_min_format(MF_UNDEF),
145145
m_max_format(MF_UNDEF),
146146
m_min_value( std::numeric_limits<double>::max() ),
147147
m_max_value( std::numeric_limits<double>::min() )
148148
{
149149
}
150150

151-
//! Test if we need to read the column data
151+
//! Test if we need to read the row/column data
152152
bool readdata() const;
153153

154154
//! Check for valid min/max format
155155
static minmax_format_type parse_minmax(const std::string& key,
156156
const std::string& value);
157157

158-
//! Parse column-level key=value format
158+
//! Parse row/column-level key=value format
159159
bool parse_keyformat(const std::string& key,
160160
std::string::const_iterator& curr,
161161
const std::string::const_iterator& end);
162162

163-
//! Parse column-level formating arguments
163+
//! Parse row/column-level formating arguments
164164
bool parse_format(const std::string& format);
165165

166-
//! Apply formats of other column-level object
167-
void apply(const Column& c);
166+
//! Apply formats of other row/column-level object
167+
void apply(const Line& c);
168168
};
169169

170-
//! Typedef of column format container
171-
typedef std::map<unsigned, Column> colfmt_type;
170+
//! Typedef of row/column format container
171+
typedef std::map<unsigned, Line> linefmt_type;
172172

173-
//! Set of column-level formats
174-
colfmt_type m_colfmt;
173+
//! Set of row-specific formats
174+
linefmt_type m_rowfmt;
175175

176-
//! Default column-level and cell-level formats
177-
Column m_fmt;
176+
//! Set of column-specific formats
177+
linefmt_type m_colfmt;
178+
179+
//! Default row/column-level and cell-level formats
180+
Line m_fmt;
178181

179182
public:
180183

tests/latex/format1.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ line
5353
%% SELECT 0.001, 0.01, 0.1, 1, 10.04, 100.05, 1000.05
5454
0.001 & 0.01 & \bf 0.1 & \em 1 & \em 10.04 & 100.05 & 1000.05 \\
5555
% END TABULAR SELECT 0.001, 0.01, 0.1, 1, 10.04, 100.05, 1000.05
56+
line
57+
%% TABULAR REFORMAT(row 0=(max=bold))
58+
%% SELECT 0.001, 0.01, 0.1, 1, 1000.05, 10.04, 100.05
59+
0.001 & 0.01 & 0.1 & 1 & \bf 1000.05 & 10.04 & 100.05 \\
60+
% END TABULAR SELECT 0.001, 0.01, 0.1, 1, 1000.05, 10.04, 100.05
61+
line
62+
%% TABULAR REFORMAT(row 1=(max=bold))
63+
%% SELECT 0.001, 0.01, 0.1, 1, 1000.05, 10.04, 100.05
64+
0.001 & 0.01 & 0.1 & 1 & 1000.05 & 10.04 & 100.05 \\
65+
% END TABULAR SELECT 0.001, 0.01, 0.1, 1, 1000.05, 10.04, 100.05
5666
end

tests/latex/format1.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@
3131
line
3232
%% TABULAR REFORMAT(col 2=(max=bold) col 3-4=(min=emph))
3333
%% SELECT 0.001, 0.01, 0.1, 1, 10.04, 100.05, 1000.05
34+
line
35+
%% TABULAR REFORMAT(row 0=(max=bold))
36+
%% SELECT 0.001, 0.01, 0.1, 1, 1000.05, 10.04, 100.05
37+
line
38+
%% TABULAR REFORMAT(row 1=(max=bold))
39+
%% SELECT 0.001, 0.01, 0.1, 1, 1000.05, 10.04, 100.05
3440
end

0 commit comments

Comments
 (0)