-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgnuplot.cpp
More file actions
627 lines (512 loc) · 18.4 KB
/
gnuplot.cpp
File metadata and controls
627 lines (512 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/******************************************************************************
* src/gnuplot.cpp
*
* Process embedded SQL plot instructions in Gnuplot files.
*
******************************************************************************
* Copyright (C) 2013-2016 Timo Bingmann <[email protected]>
*
* This program 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, either version 3 of the License, or (at your option)
* any later version.
*
* 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. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <boost/regex.hpp>
#include "common.h"
#include "strtools.h"
#include "sql.h"
#include "textlines.h"
#include "importdata.h"
class SpGnuplot
{
public:
//! processed line data
TextLines& m_lines;
//! comment character
static const char comment_char = '#';
// *** current gnuplot datafile ***
std::ostream* m_datafile;
std::string m_datafilename;
unsigned int m_dataindex;
//! scan for next comment line with given prefix
inline ssize_t
scan_lines_for_comment(size_t ln, const std::string& cprefix)
{
return m_lines.scan_for_comment<comment_char>(ln, cprefix);
}
//! Process # SQL commands
void sql(size_t ln, size_t indent, const std::string& cmdline);
//! Process # IMPORT-DATA commands
int importdata(size_t ln, size_t indent, const std::string& cmdline);
//! Process # CONNECT commands
bool connect(size_t ln, size_t indent, const std::string& cmdline);
//! Struct to rewrite Gnuplot "plot" directives with new datafile/index
//! pairs
struct Dataset
{
unsigned int index;
std::string title;
std::string type;
};
//! Helper to rewrite Gnuplot "plot" directives with new datafile/index
//! pairs
void plot_rewrite(size_t ln, size_t indent,
const std::vector<Dataset>& datasets,
const char* plot_type);
//! Process # PLOT commands
void plot(size_t ln, size_t indent, const std::string& cmdline);
//! Process # MULTIPLOT commands
void multiplot(size_t ln, size_t indent, const std::string& cmdline);
//! Process # MACRO commands
void macro(size_t ln, size_t indent, const std::string& cmdline);
//! Process TextLines
int process();
//! Process Textlines
SpGnuplot(const std::string& filename, TextLines& lines);
};
//! Process # SQL commands
void SpGnuplot::sql(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
{
SqlQuery sql = g_db->query(cmdline);
OUT("SQL command successful.");
}
//! Process # IMPORT-DATA commands
int SpGnuplot::importdata(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
{
// split argument at whitespaces
std::vector<std::string> args = split_ws(cmdline);
// convert std::strings to char*
char* argv[args.size() + 1];
for (size_t i = 0; i < args.size(); ++i)
argv[i] = (char*)args[i].c_str();
argv[args.size()] = NULL;
return ImportData(true).main(args.size(), argv);
}
//! Process # CONNECT commands
bool SpGnuplot::connect(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
{
return g_db_connect(cmdline);
}
//! Helper to rewrite Gnuplot "plot" directives with new datafile/index pairs
void SpGnuplot::plot_rewrite(size_t ln, size_t indent,
const std::vector<Dataset>& datasets,
const char* plot_type)
{
std::ostringstream oss;
// check whether line contains an "plot" command
static const boost::regex re_plot("[[:blank:]]*plot.*\\\\[[:blank:]]*");
if (ln >= m_lines.size() ||
!boost::regex_match(m_lines[ln], re_plot))
{
// no "plot" command: construct default version from scratch
if (datasets.size())
oss << "plot";
for (size_t i = 0; i < datasets.size(); ++i)
{
if (i != 0) oss << ',';
oss << " \\" << std::endl
<< " '" << m_datafilename << "' index " << datasets[i].index;
if (datasets[i].title.size())
oss << " title \"" << datasets[i].title << '"';
oss << " with " << datasets[i].type;
}
if (datasets.size())
oss << std::endl;
m_lines.replace(ln, ln, indent, oss.str(), plot_type);
return;
}
// scan following lines for plot descriptions
static const boost::regex re_line("[[:blank:]]*'[^']+' index [0-9]+( title \"[^\"]*\")?( .*?)(, \\\\)?[[:blank:]]*");
boost::smatch rm;
if (datasets.size())
oss << "plot";
size_t eln = ln+1;
size_t entry = 0; // dataset entry
while (eln < m_lines.size() &&
boost::regex_match(m_lines[eln], rm, re_line))
{
++eln;
// copy properties to new plot line
if (entry < datasets.size())
{
if (entry != 0) oss << ',';
oss << " \\" << std::endl
<< " '" << m_datafilename << "' index " << datasets[entry].index;
// if dataset contains a title, add it
if (datasets[entry].title.size())
oss << " title \"" << datasets[entry].title << '"';
// output extended properties
oss << rm[2];
++entry;
// break if no \ was found at the end
if (rm[3].length() == 0) break;
}
else
{
// gobble additional plot line
}
}
// append missing plot descriptions
while (entry < datasets.size())
{
if (entry != 0) oss << ',';
oss << " \\" << std::endl
<< " '" << m_datafilename << "' index " << datasets[entry].index;
if (datasets[entry].title.size())
oss << " title \"" << datasets[entry].title << '"';
oss << " with " << datasets[entry].type;
++entry;
}
if (datasets.size())
oss << std::endl;
m_lines.replace(ln, eln, indent, oss.str(), plot_type);
}
//! Process # PLOT commands
void SpGnuplot::plot(size_t ln, size_t indent, const std::string& cmdline)
{
SqlQuery sql = g_db->query(cmdline);
// write a header to the datafile containing the query
std::ostream& df = *m_datafile;
df << std::string(80, '#') << std::endl
<< "# PLOT " << cmdline << std::endl
<< '#' << std::endl;
// write result data rows
while (sql->step())
{
for (unsigned int col = 0; col < sql->num_cols(); ++col)
{
if (col != 0) df << '\t';
df << sql->text(col);
}
df << std::endl;
}
// append plot line to gnuplot
std::vector<Dataset> datasets(1);
datasets[0].index = m_dataindex;
datasets[0].type = "linespoints";
// finish index in datafile
df << std::endl << std::endl;
++m_dataindex;
plot_rewrite(ln, indent, datasets, "PLOT");
}
//! Process # MULTIPLOT commands
void SpGnuplot::multiplot(size_t ln, size_t indent, const std::string& cmdline)
{
// extract MULTIPLOT columns
static const boost::regex
re_multiplot("MULTIPLOT\\(([^)]+)\\) (.+)");
boost::smatch rm_multiplot;
if (!boost::regex_match(cmdline, rm_multiplot, re_multiplot))
OUT_THROW("MULTIPLOT() requires group column list.");
std::string multiplot = rm_multiplot[1].str();
std::string query = rm_multiplot[2].str();
query = replace_all(query, "MULTIPLOT", multiplot);
std::vector<std::string> groupfields = split(multiplot, ',');
std::for_each(groupfields.begin(), groupfields.end(), trim_inplace_ws);
// execute query
SqlQuery sql = g_db->query(query);
std::vector<Dataset> datasets;
// read column names
sql->read_colmap();
// check for existing x and y columns.
if (!sql->exist_col("x"))
OUT_THROW("MULTIPLOT failed: result contains no 'x' column.");
if (!sql->exist_col("y"))
OUT_THROW("MULTIPLOT failed: result contains no 'y' column.");
unsigned int col_x = sql->find_col("x"), col_y = sql->find_col("y");
int col_xmin = !sql->exist_col("xmin") ? -1 : sql->find_col("xmin");
int col_xmax = !sql->exist_col("xmax") ? -1 : sql->find_col("xmax");
int col_ymin = !sql->exist_col("ymin") ? -1 : sql->find_col("ymin");
int col_ymax = !sql->exist_col("ymax") ? -1 : sql->find_col("ymax");
bool have_xerrorbars = col_xmin >= 0 && col_xmax >= 0;
bool have_yerrorbars = col_ymin >= 0 && col_ymax >= 0;
// check existance of group fields and save ids
std::vector<int> groupcols;
for (std::vector<std::string>::const_iterator gi = groupfields.begin();
gi != groupfields.end(); ++gi)
{
if (!sql->exist_col(*gi))
{
OUT_THROW("MULTIPLOT failed: result contains no '" << *gi <<
"' column, which is a MULTIPLOT group field.");
}
groupcols.push_back(sql->find_col(*gi));
}
// write a header to the datafile containing the query
std::ostream& df = *m_datafile;
df << std::string(80, '#') << std::endl
<< "# " << cmdline << std::endl
<< '#' << std::endl;
// collect coordinates groups
{
std::vector<std::string> lastgroup;
size_t rows = 0;
while (sql->step())
{
// collect groupfields for this row
std::vector<std::string> rowgroup (groupcols.size());
for (size_t i = 0; i < groupcols.size(); ++i)
rowgroup[i] = sql->text(groupcols[i]);
if (sql->current_row() == 0 || lastgroup != rowgroup)
{
// group fields mismatch (or first row) -> start new group
if (sql->current_row() != 0) {
df << std::endl << std::endl;
++m_dataindex;
}
lastgroup = rowgroup;
// store group's legend string
std::ostringstream os;
for (size_t i = 0; i < groupcols.size(); ++i) {
if (i != 0) os << ',';
os << groupfields[i] << '=' << rowgroup[i];
}
datasets.push_back(Dataset());
datasets.back().index = m_dataindex;
datasets.back().title = os.str();
datasets.back().type = "linespoints";
if (have_xerrorbars && have_yerrorbars)
datasets.back().type = "xyerrorbars";
else if (have_xerrorbars)
datasets.back().type = "xerrorbars";
else if (have_yerrorbars)
datasets.back().type = "yerrorbars";
df << "# index " << m_dataindex << ' ' << os.str() << std::endl;
}
// group fields match with last row -> append coordinates.
df << sql->text(col_x)
<< '\t' << sql->text(col_y);
if (have_xerrorbars) {
df << '\t' << sql->text(col_xmin)
<< '\t' << sql->text(col_xmax);
}
if (have_yerrorbars) {
df << '\t' << sql->text(col_ymin)
<< '\t' << sql->text(col_ymax);
}
df << std::endl;
++rows;
}
if (rows == 0)
df << "- # (no data rows)" << std::endl;
// finish last plot
df << std::endl << std::endl;
++m_dataindex;
}
plot_rewrite(ln, indent, datasets, "MULTIPLOT");
}
static inline
std::string maybe_quote(const std::string& str)
{
if (str_is_double(str)) // fully parsed
return str;
else // needs quoting
return '\'' + str + '\'';
}
//! Process # MACRO commands
void SpGnuplot::macro(size_t ln, size_t indent, const std::string& cmdline)
{
SqlQuery sql = g_db->query(cmdline);
sql->step();
// write each column as macro value
std::ostringstream oss;
for (unsigned int col = 0; col < sql->num_cols(); ++col)
{
oss << sql->col_name(col) << " = "
<< maybe_quote( sql->text(col) ) << std::endl;
}
// scan following lines for macro defintions
static const boost::regex re_macro("[^=]+ = .*");
size_t eln = ln;
while (eln < m_lines.size() &&
boost::regex_match(m_lines[eln], re_macro))
{
++eln;
}
m_lines.replace(ln, eln, indent, oss.str(), "MACRO");
}
//! process line-based file in place
int SpGnuplot::process()
{
bool active_range = gopt_ranges.size() ? false : true;
// iterate over all lines
for (size_t ln = 0; ln < m_lines.size();)
{
// collect command from comment lines
std::string cmd;
size_t indent;
if (!m_lines.collect_comment<comment_char>(ln, cmd, indent))
continue;
// extract first word
std::string::size_type space_pos =
cmd.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ-_");
std::string first_word = cmd.substr(0, space_pos);
if (first_word == "RANGE")
{
// extract second word
std::string::size_type non_space_pos =
cmd.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ-_", space_pos);
std::string::size_type space2_pos =
cmd.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ-_", non_space_pos);
std::string::size_type non_space2_pos =
cmd.find_first_not_of(" ", space2_pos);
std::string second_word = cmd.substr(
non_space_pos, space2_pos - non_space_pos);
std::string rest_word = cmd.substr(non_space2_pos);
if (second_word == "BEGIN")
{
if (std::find(gopt_ranges.begin(), gopt_ranges.end(),
rest_word) != gopt_ranges.end())
{
OUT(ln << "# " << cmd);
active_range = true;
}
}
else if (second_word == "END")
{
if (std::find(gopt_ranges.begin(), gopt_ranges.end(),
rest_word) != gopt_ranges.end())
{
OUT(ln << "# " << cmd);
active_range = false;
}
}
else
{
OUT("? maybe unknown keywords " << first_word << " " << second_word);
}
}
else if (!active_range)
{
// skip keywords in non-active ranges
}
else if (first_word == "SQL")
{
OUT(ln << "# " << cmd);
sql(ln, indent, cmd.substr(space_pos+1));
}
else if (first_word == "IMPORT-DATA")
{
OUT(ln << "# " << cmd);
if (importdata(ln, indent, cmd) != EXIT_SUCCESS)
return EXIT_FAILURE;
}
else if (first_word == "CONNECT")
{
OUT(ln << "# " << cmd);
if (!connect(ln, indent, cmd.substr(space_pos+1)))
return EXIT_FAILURE;
}
else if (first_word == "PLOT")
{
OUT(ln << "# " << cmd);
plot(ln, indent, cmd.substr(space_pos+1));
}
else if (first_word == "MULTIPLOT")
{
OUT(ln << "# " << cmd);
multiplot(ln, indent, cmd);
}
else if (first_word == "MACRO")
{
OUT(ln << "# " << cmd);
macro(ln, indent, cmd.substr(space_pos+1));
}
else
{
if (first_word.size() >= 4 && first_word[0] != '-')
OUT("? maybe unknown keyword " << first_word);
}
}
return EXIT_SUCCESS;
}
//! process a stream
SpGnuplot::SpGnuplot(const std::string& filename, TextLines& lines)
: m_lines(lines)
{
// construct output data file
m_datafilename = filename;
std::string::size_type dotpos = m_datafilename.rfind('.');
if (dotpos != std::string::npos)
m_datafilename = m_datafilename.substr(0, dotpos);
m_datafilename += "-data.txt";
// open output data file
if (!gopt_check_output)
{
m_datafile = new std::ofstream(m_datafilename.c_str());
if (!m_datafile->good()) {
OUT("Fatal error opening datafile " << m_datafilename << ": " << strerror(errno));
delete m_datafile;
return;
}
}
else
{
// open temporary data file
m_datafile = new std::ostringstream();
}
m_dataindex = 0;
// write data file preamble
{
std::ostream& df = *m_datafile;
df << std::string(80, '#') << std::endl
<< '#' << std::endl
<< "# DO NOT EDIT THIS FILE MANUALLY!" << std::endl
<< "# ALL CHANGES WILL BE LOST WHEN RECREATED!" << std::endl
<< '#' << std::endl
<< "# The data in this file was generated by sqlplot-tools" << std::endl
<< "# by processing \"" << filename << "\"." << std::endl
<< '#' << std::endl
<< std::string(80, '#') << std::endl << std::endl;
}
// process lines in place
if (process() != EXIT_SUCCESS)
exit(EXIT_FAILURE);
// verify processed output against file
if (gopt_check_output)
{
std::ifstream in(m_datafilename.c_str());
if (!in.good()) {
OUT("Error reading " << m_datafilename << ": " << strerror(errno));
exit(EXIT_FAILURE);
}
std::string checkdata = read_stream(in);
std::ostringstream* oss = (std::ostringstream*)m_datafile;
if (checkdata != oss->str())
{
OUT("Mismatch to expected output data file:");
simple_diff(oss->str(), checkdata);
OUT_THROW("Mismatch to expected output data file " << m_datafilename);
}
else
{
OUT("Good match to expected output data file " << m_datafilename);
}
}
delete m_datafile;
}
//! Process Gnuplot file
void sp_gnuplot(const std::string& filename, TextLines& lines)
{
SpGnuplot sp(filename, lines);
}