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

80 lines
1.8 KiB
C

#include <stdlib.h>
#include "tape.h"
#include "record.h"
#include "common.h"
#define WANT_IOSTAT 1
#define WANT_SUMMARY 2
char* filename;
char flags;
void init_args(int args, char* argv[])
{
optparse_t options;
optparse_init(&options, argv);
for (char opt; opt != -1; opt = optparse(&options, "qsvi")) {
switch (opt) {
case 'q':
verbosity--;
break;
case 'v':
verbosity++;
break;
case 'i':
flags |= WANT_IOSTAT;
break;
case 's':
flags |= WANT_SUMMARY;
break;
}
}
filename = optparse_arg(&options);
}
void help(const char* name) {
printf(
"Appends records to given tape at the end.\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"
,
name
);
}
int main(int argc, const char* argv[])
{
if (argc < 2) {
help(argv[0]);
return 0;
}
tape_t* tape = tape_open(argv[1], TAPE_APPEND);
record_t record;
size_t records = 0;
while (scanf("%lf %lf", &record.x, &record.y) > 0) {
tape_write(tape, &record, sizeof(record_t));
printfv(VERBOSITY_NORMAL, "Record created: r = (%lf, %lf) |r| = %lf\n", record.x, record.y, record_length(record));
records++;
}
if (flags & WANT_SUMMARY) {
printfv(VERBOSITY_NORMAL, "Appended %zu record to %s.\n", records, argv[1]);
}
if (flags & WANT_IOSTAT) {
iostats();
}
tape_close(tape);
return EXIT_SUCCESS;
}