From beb3e03072c706554acdfdd38dc7fb920ff2bb41 Mon Sep 17 00:00:00 2001 From: "Douglas B. Rumbaugh" Date: Fri, 29 Aug 2025 18:24:43 -0400 Subject: Code cleanup+documentation --- src/cdf.c | 188 ++++++++++++++++++++++------------------------------------- src/cumsum.c | 63 ++++++++++---------- 2 files changed, 99 insertions(+), 152 deletions(-) (limited to 'src') diff --git a/src/cdf.c b/src/cdf.c index 45b8fb7..3bf39fe 100644 --- a/src/cdf.c +++ b/src/cdf.c @@ -1,8 +1,7 @@ /* - * + * */ - #include "cdf.h" /* @@ -14,35 +13,36 @@ static bool ARG_FP_INPUT = false; static bool ARG_UINT_INPUT = false; static bool ARG_HELP = false; -static int parse_options(int argc, char*const* argv) { +static int parse_options(int argc, char *const *argv) { int arg_index = 0; int arg; bool error = false; while ((arg = getopt(argc, argv, "frhu")) != -1) { switch (arg) { - case 'f': - ARG_FP_INPUT = true; - break; - case 'r': - ARG_REVERSE_CDF = true; - break; - case 'u': - ARG_UINT_INPUT = true; - case 'h': - ARG_HELP = true; - break; - case '?': - if (isprint(optopt)) { - fprintf(stderr, "Unknown option `-%c`.\n", optopt); - } else { - fprintf(stderr, "Unknown option character `\\x%x`.\n", optopt); - } - error = true; - break; - default: - error = true; - break; + case 'f': + ARG_FP_INPUT = true; + break; + case 'r': + ARG_REVERSE_CDF = true; + break; + case 'u': + ARG_UINT_INPUT = true; + break; + case 'h': + ARG_HELP = true; + break; + case '?': + if (isprint(optopt)) { + fprintf(stderr, "Unknown option `-%c`.\n", optopt); + } else { + fprintf(stderr, "Unknown option character `\\x%x`.\n", optopt); + } + error = true; + break; + default: + error = true; + break; } } @@ -58,13 +58,11 @@ static int parse_options(int argc, char*const* argv) { return arg_index; } -static void help() { - fprintf(stderr, "Usage:\ncdf [-f|-u] [-r] [filename]\n"); -} +static void help() { fprintf(stderr, "Usage:\ncdf [-f|-u] [-r] [filename]\n"); } static DistRecord *expand_array(DistRecord *records, size_t *capacity) { (*capacity) *= 2; - DistRecord *new = realloc(records, (*capacity*sizeof(DistRecord))); + DistRecord *new = realloc(records, (*capacity * sizeof(DistRecord))); if (!new) { fprintf(stderr, "ERROR: Memory allocation failed\n"); return nullptr; @@ -73,42 +71,26 @@ static DistRecord *expand_array(DistRecord *records, size_t *capacity) { return new; } -static int read_data_int(DistRecord **records, size_t capacity, FILE *file) { - size_t reccnt = 0; - while (fscanf(file, "%ld %ld\n", &(*records + reccnt)->count, - &(*records + reccnt)->data.i) != EOF) { - - reccnt++; - if (reccnt == capacity) { - if (!(*records = expand_array(*records, &capacity))) { - return -1; - } - } +static int parse_line(DistRecord *record, char *line) { + if (ARG_FP_INPUT) { + return sscanf(line, "%lld %lf", &record->count, &record->data.d); + } else if (ARG_UINT_INPUT) { + return sscanf(line, "%lld %llu", &record->count, &record->data.u); + } else { + return sscanf(line, "%lld %lld", &record->count, &record->data.i); } - - return reccnt; } -static int read_data_fp(DistRecord **records, size_t capacity, FILE *file) { +static ssize_t read_data(DistRecord **records, size_t capacity, FILE *file) { size_t reccnt = 0; - while (fscanf(file, "%ld %lf\n", &(*records + reccnt)->count, - &(*records + reccnt)->data.d) != EOF) { + char *line = nullptr; + size_t line_len = 0; - reccnt++; - if (reccnt == capacity) { - if (!(*records = expand_array(*records, &capacity))) { - return -1; - } + while (getline(&line, &line_len, file) != -1) { + if (parse_line(*records + reccnt, line) != 2) { + fprintf(stderr, "[W] Skipping invalid input line: %s\n", line); + continue; } - } - - return reccnt; -} - -static int read_data_uint(DistRecord **records, size_t capacity, FILE *file) { - size_t reccnt = 0; - while (fscanf(file, "%ld %ld\n", &(*records + reccnt)->count, - &(*records + reccnt)->data.u) != EOF) { reccnt++; if (reccnt == capacity) { @@ -121,50 +103,27 @@ static int read_data_uint(DistRecord **records, size_t capacity, FILE *file) { return reccnt; } -static int print_data_fp(DistRecord *records, long double *freqs, size_t cnt) { - if (ARG_REVERSE_CDF) { - long double total_freq = 1.0; - for (size_t i=0; idata.d); + } else if (ARG_UINT_INPUT) { + fprintf(stdout, "%.15Lf\t%llu\n", freq, record->data.u); } else { - for (size_t i=0; idata.i); } - - return 1; } -static int print_data_int(DistRecord *records, long double *freqs, size_t cnt) { +static int print_data(DistRecord *records, long double *freqs, size_t cnt) { if (ARG_REVERSE_CDF) { long double total_freq = 1.0; - for (size_t i=0; ii += val; } @@ -88,7 +85,7 @@ static int read_data_int(FILE *file, Number *num) { static int read_data_uint(FILE *file, Number *num) { uint64_t val; - while (fscanf(file, "%ld ", &val) != EOF) { + while (fscanf(file, "%lld ", &val) != EOF) { num->u += val; } @@ -97,10 +94,10 @@ static int read_data_uint(FILE *file, Number *num) { static int process_data(FILE *file) { int rc = 1; - + Number sum = {}; - /* FIXME: this could probably use a type-based macro to collapse the + /* FIXME: this could probably use a type-based macro to collapse the if statements into a single macro call */ if (ARG_FP_INPUT) { @@ -119,7 +116,6 @@ process_data_end: return rc; } - int main(int argc, char **argv) { int rc = EXIT_SUCCESS; int file_index = 0; @@ -139,7 +135,8 @@ int main(int argc, char **argv) { FILE *input_file; if (file_index < argc && strcmp(argv[file_index], "-") != 0) { if (!(input_file = fopen(argv[file_index], "r"))) { - fprintf(stderr, "Error: Unable to open input file %s\n", argv[file_index]); + fprintf(stderr, "Error: Unable to open input file %s\n", + argv[file_index]); rc = EXIT_FAILURE; goto program_exit; } -- cgit v1.2.3