SBDP01/reader.c
2018-10-21 13:51:28 +02:00

101 lines
2.2 KiB
C

#include <stdlib.h>
#include "tape.h"
#include "record.h"
#include "common.h"
#define WANT_SUMMARY 1
#define WANT_IOSTAT 2
#define WANT_EXPORT 4
char* filename;
char flags;
void help(const char* name)
{
printf(
"Reads records from tape.\n"
"Usage:\n"
"\t%s [options] <tape>\n"
"Options:\n"
"\t-q|v|vv - verbosity level, q for quiet, v for vervose vv for debug\n"
"\t-s - summary of records (R) and runs (S)\n"
"\t-i - summary IO stats\n"
"\t-e - data in export format\n"
, name
);
}
void init_args(int args, char* argv[])
{
optparse_t options;
optparse_init(&options, argv);
for (char opt; opt != -1; opt = optparse(&options, "qvise")) {
switch (opt) {
case 'q':
verbosity--;
break;
case 'v':
verbosity++;
break;
case 'i':
flags |= WANT_IOSTAT;
break;
case 'e':
flags |= WANT_EXPORT;
break;
case 's':
flags |= WANT_SUMMARY;
break;
}
}
filename = optparse_arg(&options);
}
int main(int argc, char* argv[])
{
if (argc < 2) {
help(argv[0]);
return 0;
}
init_args(argc, argv);
tape_t* tape = tape_open(filename, "rb");
record_t record, last = { 0.0, 0.0 };
size_t records = 0, series = 1, current = 0;;
while (tape_read(tape, &record, sizeof(record_t)) > 0) {
if (record_compare(last, record) > 0) {
printfv(VERBOSITY_VERBOSE, "R: %zu -------------------------------------\n", current);
series++;
current = 0;
}
if (flags & WANT_EXPORT) {
printfv(VERBOSITY_NORMAL, "%lf %lf", record.x, record.y);
} else {
printfv(VERBOSITY_NORMAL, "|r| = %lf r = (%lf, %lf)\n", record_length(record), record.x, record.y);
}
records++;
current++;
last = record;
}
tape_close(tape);
if (flags & WANT_SUMMARY) {
printf("R: %zu S: %zu\n", records, series);
}
if (flags & WANT_IOSTAT) {
iostats();
}
return EXIT_SUCCESS;
}