-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
467 lines (421 loc) · 13.7 KB
/
graph.cpp
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
/**************************************************/
/* Author: Lukas Lazarek */
/* Copyright (C) 2016 Lukas Lazarek */
/* Please see LICENSE.txt for full license info */
/**************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
#include "asciigraph.h"
#define DEBUG if(debug)
void fileGraph(const std::string &path, const bool debug);
void streamGraph(std::istream &in, const bool debug);
int main(int argc, char *argv[]){
bool debug = false;
for(int i = 1; i < argc; ++i){
if(argv[i][0] == '-'){
switch(argv[i][1]){
case 'h':
std::cout << "asciigraph is a utility to produce simple graphs"
" of arbitrary data in ascii. The format for running asciigraph"
" is as follows:\n\n"
"\tasciigraph [-d] <-h | -s | -f </absolute/path/to/file> >\n\n"
"The meaning of the switches are...\n\n"
"-d\tEnable debug output logging to stderr."
" *NOTE* This will break graphs unless stderr is redirected"
" elsewhere from the asciigraph's output.\n"
"-h\tDisplay this help message.\n"
"-s\tPull graph data directly from stdin.\n"
"-f\tPull graph data from the specified file.\n\n"
"Please read the readme for more information.\n\n" << std::endl;
break;
case 's':
DEBUG std::cerr << "Pulling data from stdin..." << std::endl;
try{
streamGraph(std::cin, debug);
}catch(const invalid_data &e){
std::cout << "The data provided is invalid, with error \""
<< e.what() << "\". Please read the readme for data"
" format requirements. Exiting..." << std::endl;
}
break;
case 'f':
DEBUG std::cerr << "Pulling data from file..." << std::endl;
if(argc > i + 1){
try{
fileGraph(argv[i + 1], debug);
}catch (const file_not_found &e){
std::cout << "Unable to open file, with error \"" << e.what()
<< "\". Please check the given path, that the file"
" exists, and its permissions. Exiting..."
<< std::endl;
}
}
else{
std::cout << "No file name supplied. Exiting..."
<< std::endl;
return 1;
}
break;
case 'd':
debug = true;
DEBUG std::cerr << "DEBUG turned on" << std::endl;
break;
default:
std::cout << "Invalid option supplied. For help, try \"-h\". Exiting..."
<< std::endl;
return 1;
}// end switch
}// end if
}// end for
}
/* fileGraph():
Graphs the data stored in the file path specified.
@params
const std::string &path The path of the file containing data to graph
const bool debug Print debug info?
@return
void
@throws
file_not_found File unable to be opened
*/
void fileGraph(const std::string &path, const bool debug){
std::ifstream file(path);
if(file.is_open()){
try{
streamGraph(file, debug);
}catch(const invalid_data &e){
std::cout << "The data provided is invalid, with error \""
<< e.what() << "\". Please read the readme"
" for data format requirements. Exiting..." << std::endl;
}
file.close();
}
else{
throw file_not_found("File not found");
}
}
/* streamGraph():
Graphs data obtained from the given istream.
@params
std::istream &in The stream from which to read data to graph
const bool debug Print debug info?
@return
void
@throws
invalid_data Data invalid format or invalid limits
*/
void streamGraph(std::istream &in, const bool debug){
int xmin = -1, xmax = -1;
int ymin = 0, ymax = 0;
int xstep = 1, ystep = 1;
int hmax = 0;
bool xmin_set = false, xmax_set = false,
ymin_set = false, ymax_set = false,
hmax_set = false, bar_graph = false,
BAR_ZERO_POINT = BAR_ZERO_POINT_DEFAULT;
char X_AXIS_CHAR = X_AXIS_CHAR_DEFAULT,
Y_AXIS_CHAR = Y_AXIS_CHAR_DEFAULT,
GUIDELINE_CHAR = GUIDELINE_CHAR_DEFAULT,
POINT_CHAR = POINT_CHAR_DEFAULT;
int X_LABEL_DENSITY = X_LABEL_DENSITY_DEFAULT,
GUIDELINE_DENSITY = GUIDELINE_DENSITY_DEFAULT,
WIDTH_PAD = WIDTH_PAD_DEFAULT;
std::string X_AXIS_LABEL = X_AXIS_LABEL_DEFAULT,
Y_AXIS_LABEL = Y_AXIS_LABEL_DEFAULT;
std::vector<std::pair<int, int>> pts;
std::string line;
bool file_continues = getline(in, line);
/* Handle graph options if any */
try{
while(line != "" &&
file_continues &&
(line.c_str()[0] == '#' || line.c_str()[0] == ';')){
if(line.c_str()[0] == ';'){
DEBUG std::cerr << "skipping comment..." << std::endl;
}
else if(line.compare(1, 5, "ystep") == 0){
ystep = std::stoi(line.substr(7));
if(ystep <= 0) ystep = 1;
DEBUG std::cerr << "Set ystep to " << ystep << std::endl;
}
else if(line.compare(1, 4, "ymin") == 0){
ymin = std::stoi(line.substr(6));
ymin_set = true;
DEBUG std::cerr << "Set ymin to " << ymin << std::endl;
}
else if(line.compare(1, 4, "ymax") == 0){
ymax = std::stoi(line.substr(6));
ymax_set = true;
DEBUG std::cerr << "Set ymax to " << ymax << std::endl;
}
else if(line.compare(1, 4, "xmin") == 0){
xmin = std::stoi(line.substr(6));
xmin_set = true;
DEBUG std::cerr << "Set xmin to " << xmin << std::endl;
}
else if(line.compare(1, 4, "xmax") == 0){
xmax = std::stoi(line.substr(6));
xmax_set = true;
DEBUG std::cerr << "Set xmax to " << xmax << std::endl;
}
else if(line.compare(1, 4, "hmax") == 0){
hmax = std::stoi(line.substr(6));
hmax_set = true;
DEBUG std::cerr << "Set hmax to " << hmax << std::endl;
}
else if(line.compare(1, 11, "X_AXIS_CHAR") == 0){
X_AXIS_CHAR = line.substr(13, 1).c_str()[0];
DEBUG std::cerr << "Set X_AXIS_CHAR to " << X_AXIS_CHAR << std::endl;
}
else if(line.compare(1, 11, "Y_AXIS_CHAR") == 0){
Y_AXIS_CHAR = line.substr(13, 1).c_str()[0];
DEBUG std::cerr << "Set Y_AXIS_CHAR to " << Y_AXIS_CHAR << std::endl;
}
else if(line.compare(1, 14, "GUIDELINE_CHAR") == 0){
GUIDELINE_CHAR = line.substr(16, 1).c_str()[0];
DEBUG std::cerr << "Set GUIDELINE_CHAR to " << GUIDELINE_CHAR
<< std::endl;
}
else if(line.compare(1, 10, "POINT_CHAR") == 0){
POINT_CHAR = line.substr(12, 1).c_str()[0];
DEBUG std::cerr << "Set POINT_CHAR to " << POINT_CHAR << std::endl;
}
else if(line.compare(1, 15, "X_LABEL_DENSITY") == 0){
X_LABEL_DENSITY = std::stoi(line.substr(17));
DEBUG std::cerr << "Set X_LABEL_DENSITY to " << X_LABEL_DENSITY
<< std::endl;
}
else if(line.compare(1, 17, "GUIDELINE_DENSITY") == 0){
GUIDELINE_DENSITY = std::stoi(line.substr(19));
DEBUG std::cerr << "Set GUIDELINE_DENSITY to " << GUIDELINE_DENSITY
<< std::endl;
}
else if(line.compare(1, 12, "X_AXIS_LABEL") == 0){
X_AXIS_LABEL = line.substr(14);
DEBUG std::cerr << "Set X_AXIS_LABEL to " << X_AXIS_LABEL
<< std::endl;
}
else if(line.compare(1, 12, "Y_AXIS_LABEL") == 0){
Y_AXIS_LABEL = line.substr(14);
DEBUG std::cerr << "Set Y_AXIS_LABEL to " << Y_AXIS_LABEL
<< std::endl;
}
else if(line.compare(1, 3, "bar") == 0){
bar_graph = true;
DEBUG std::cerr << "Switching to bar graph mode."
<< std::endl;
}
else if(line.compare(1, 14, "BAR_ZERO_POINT") == 0){
BAR_ZERO_POINT = true;
DEBUG std::cerr << "Set BAR_ZERO_POINT to " << BAR_ZERO_POINT
<< std::endl;
}
else if(line.compare(1, 9, "WIDTH_PAD") == 0){
WIDTH_PAD = std::stoi(line.substr(11));
DEBUG std::cerr << "Set WIDTH_PAD to " << WIDTH_PAD
<< std::endl;
}
else{
DEBUG std::cerr << "Skipping invalid option: \"" << line
<< "\"" << std::endl;
}
getline(in, line);
}
}catch(const std::invalid_argument &e){
throw invalid_data("invalid option settings");
}catch(const std::out_of_range &e){
throw invalid_data("invalid option settings");
}
if(line == "" || !file_continues) return;
std::size_t pos = line.find(",");
// Check graph type
if(!bar_graph){
/************************/
/** Standard data plot **/
/************************/
// Which kind? Basic (y) or scatter (x, y)?
if(pos != std::string::npos){
/*******************/
/** scatter input **/
/*******************/
DEBUG std::cerr << "parsing data as scatter input" << std::endl;
// Interpret "val1, val2" as point: (x, y)
for(int i = 0; line != "" && file_continues;
++i, file_continues = getline(in, line)){
// Check if comment
if (line.c_str()[0] == ';'){
DEBUG std::cerr << "skipping comment..." << std::endl;
continue;
}
// Parse line
pos = line.find(",");
DEBUG std::cerr << "parsing line {" << line << "}" << std::endl;
int x, y;
try{
x = std::stoi(line.substr(0, pos));
y = std::stoi(line.substr(pos + 1));
}catch(const std::invalid_argument &e){
throw invalid_data("invalid format");
}
DEBUG std::cerr << "into x=" << x << "\ty=" << y << std::endl;
if(i == 0){
if(!xmin_set) xmin = x;
if(!xmax_set) xmax = x;
if(!ymin_set) ymin = y;
if(!ymax_set) ymax = y;
}
if(!xmin_set && x < xmin) xmin = x;
if(!xmax_set && x > xmax) xmax = x;
if(!ymin_set && y < ymin) ymin = y;
if(!ymax_set && y > ymax) ymax = y;
pts.push_back(std::pair<int, int>(x, y));
DEBUG std::cerr << "getting next line..." << std::endl;
}
// Ensure graph height <= hmax
if(hmax_set){
int minstep_fit = (ymax - ymin)/hmax + 1;
if(ystep < minstep_fit){
DEBUG std::cerr << "Adjusting ystep to " << minstep_fit
<< " in order to satisfy hmax" << std::endl;
ystep = minstep_fit;
}
}
std::cout << "\n\n";
try{
asciigraph ag(pts, xmin, xmax, xstep, ymin, ymax, ystep,
debug,
X_AXIS_CHAR, Y_AXIS_CHAR, GUIDELINE_CHAR, POINT_CHAR,
X_LABEL_DENSITY, GUIDELINE_DENSITY, X_AXIS_LABEL,
Y_AXIS_LABEL, WIDTH_PAD);
ag(std::cout);
}catch(const std::logic_error &e){
throw invalid_data("invalid limit values");
}
}// end if(pos != std::string::npos)
else{
/*****************/
/** basic input **/
/*****************/
DEBUG std::cerr << "parsing data as basic input" << std::endl;
// Interpret "val1" as value to be graphed against integer counter from 0
int i = 0;
for(; line != "" && file_continues;
++i, file_continues = getline(in, line)){
// Check if comment
if (line.c_str()[0] == ';'){
DEBUG std::cerr << "skipping comment..." << std::endl;
continue;
}
// Parse line
int y;
try{
y = std::stoi(line);
}catch(const std::invalid_argument &e){
throw invalid_data("invalid format");
}
if(i == 0){
if(!ymin_set) ymin = y;
if(!ymax_set) ymax = y;
}
if(!ymin_set && y < ymin) ymin = y;
if(!ymax_set && y > ymax) ymax = y;
DEBUG std::cerr << "parsing line {" << line << "}" << " into ("
<< i << ", " << y << ")" << std::endl;
pts.push_back(std::pair<int, int>(i, y));
}
DEBUG std::cerr << "min: " << ymin << ", max: " << ymax << std::endl;
// Ensure graph height <= hmax
if(hmax_set){
int minstep_fit = (ymax - ymin)/hmax + 1;
if(ystep < minstep_fit){
DEBUG std::cerr << "Adjusting ystep to " << minstep_fit
<< " in order to satisfy hmax" << std::endl;
ystep = minstep_fit;
}
}
std::cout << "\n\n";
try{
if(!xmin_set) xmin = 0;
if(!xmax_set) xmax = i - 1;
asciigraph ag(pts, xmin, xmax, xstep, ymin, ymax, ystep,
debug,
X_AXIS_CHAR, Y_AXIS_CHAR, GUIDELINE_CHAR, POINT_CHAR,
X_LABEL_DENSITY, GUIDELINE_DENSITY, X_AXIS_LABEL,
Y_AXIS_LABEL, WIDTH_PAD);
ag(std::cout);
}catch(const std::logic_error &e){
throw invalid_data("invalid limit values");
}
}
}// end if(!bar_graph)
else{
/***************/
/** bar graph **/
/***************/
DEBUG std::cerr << "parsing data as bar graph" << std::endl;
X_AXIS_LABEL += "\n\n== LEGEND ==";
// lines in format "val, label"
int i = 0;
for(; line != "" && file_continues;
++i, file_continues = getline(in, line)){
// Check if comment
if (line.c_str()[0] == ';'){
DEBUG std::cerr << "skipping comment..." << std::endl;
continue;
}
// Parse line
pos = line.find(",");
DEBUG std::cerr << "parsing line {" << line << "}" << std::endl;
int y;
std::string label;
try{
y = std::stoi(line.substr(0, pos));
label = line.substr(pos + 1);
}catch(const std::invalid_argument &e){
throw invalid_data("invalid format");
}
DEBUG std::cerr << "into [" << label << ": " << y << "]" << std::endl;
if(i == 0){
if(!ymin_set) ymin = y;
if(!ymax_set) ymax = y;
}
if(!ymin_set && y < ymin) ymin = y;
if(!ymax_set && y > ymax) ymax = y;
pts.push_back(std::pair<int, int>(i, y));
X_AXIS_LABEL += "\n" + std::to_string(i) + " =" + label;
DEBUG std::cerr << "getting next line..." << std::endl;
}
// Ensure graph height <= hmax
if(hmax_set){
int minstep_fit = (ymax - ymin)/hmax + 1;
if(ystep < minstep_fit){
DEBUG std::cerr << "Adjusting ystep to " << minstep_fit
<< " in order to satisfy hmax" << std::endl;
ystep = minstep_fit;
}
}
std::cout << "\n\n";
try{
// Set bar graph defaults (if not explicitly user-set)
if(X_LABEL_DENSITY == X_LABEL_DENSITY_DEFAULT){
X_LABEL_DENSITY = 1;
}
if(!ymin_set && ymin > 0){
ymin = 0;
}
if(!xmin_set) xmin = 0;
if(!xmax_set) xmax = i - 1;
asciigraph ag(pts, xmin, xmax, xstep, ymin, ymax, ystep,
debug,
X_AXIS_CHAR, Y_AXIS_CHAR, GUIDELINE_CHAR, POINT_CHAR,
X_LABEL_DENSITY, GUIDELINE_DENSITY, X_AXIS_LABEL,
Y_AXIS_LABEL, WIDTH_PAD, BAR_ZERO_POINT);
ag(std::cout, true);
}catch(const std::logic_error &e){
throw invalid_data("invalid limit values");
}
}
}