summaryrefslogtreecommitdiffstats
path: root/src/cumsum.c
blob: 56486617357e40d3b923d12bfde026c0632555eb (plain)
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
/*
 * 
 */


#include "cumsum.h"

/*
 * Global configuration variables. These are set based on command-line
 * arguments, and read-only beyond that point.
 */
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) {
  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 '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;
    }
  }

  if (ARG_UINT_INPUT && ARG_FP_INPUT) {
    fprintf(stderr, "Error: the -u and -f flags are mutually exclusive.\n");
    error = true;
  }

  if (!error) {
    arg_index = optind;
  }

  return arg_index;
}

static void help() {
  fprintf(stderr, "Usage:\ncumsum [-f|-u] [filename]\n");
}

void print_sum(Number sum) {
  if (ARG_FP_INPUT) {
    fprintf(stdout, "%lf\n", sum.d);
  } else if (ARG_UINT_INPUT) {
    fprintf(stdout, "%ld\n", sum.u);
  } else {
    fprintf(stdout, "%ld\n", sum.i);
  }
}

static int read_data_fp(FILE *file, Number *num) {
  double val;
  while (fscanf(file, "%lf ", &val) != EOF) {
    num->d += val;
  }

  return 1;
}

static int read_data_int(FILE *file, Number *num) {
  int64_t val;
  while (fscanf(file, "%ld ", &val) != EOF) {
    num->i += val;
  }

  return 1;
}

static int read_data_uint(FILE *file, Number *num) {
  uint64_t val;
  while (fscanf(file, "%ld ", &val) != EOF) {
    num->u += val;
  }

  return 1;
}

static int process_data(FILE *file) {
  int rc = 1;
  
  Number sum = {};

  /* FIXME: this could probably use a type-based macro to collapse the 
            if statements into a single macro call
   */
  if (ARG_FP_INPUT) {
    rc = read_data_fp(file, &sum);
  } else if (ARG_UINT_INPUT) {
    rc = read_data_uint(file, &sum);
  } else {
    rc = read_data_int(file, &sum);
  }

  if (rc) {
    print_sum(sum);
  }

process_data_end:
  return rc;
}


int main(int argc, char **argv) {
  int rc = EXIT_SUCCESS;
  int file_index = 0;
  if (!(file_index = parse_options(argc, argv))) {
    help();
    rc = EXIT_FAILURE;
    goto program_exit;
  }

  /* if the -h argument is supplied, print usage and exit successfully */
  if (ARG_HELP) {
    help();
    goto program_exit;
  }

  /* open the input file, if one is specified, otherwise default to stdin */
  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]);
      rc = EXIT_FAILURE;
      goto program_exit;
    }
  } else {
    input_file = stdin;
  }

  if (!process_data(input_file)) {
    rc = EXIT_FAILURE;
  }

close_file:
  fclose(input_file);

program_exit:
  exit(rc);
}