summaryrefslogtreecommitdiff
path: root/src/toilet.c
blob: 6d95427bcec364af966bd7b56f44865a86840bff (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
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
#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <limits.h>
#include <tgmath.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "util/debug.h"
#include "util/error.h"
#include "util/loop_files.h"
#include "util/map.h"
#include "util/versie.h"

#include "io/read_file.h"

static int modeMap;

struct finfo {
  size_t values[3];
  char *fname;
};
static struct finfo *finfo = NULL;
static size_t finfolen = 0;
static size_t finfocap = 0;

static void usage(FILE *f) {
  fprintf(f,
      "Gebruik: toilet [-nchV] [BESTAND]...\n"
      "\n"
      "Toon de hoeveelheid regels, woorden en beten for elk BESTAND\n"
      "\n"
      "  -c          Geef het aantal beten weer\n"
      "  -w          Geef het aantal woorden weer\n"
      "  -l          Geef het aantal regels weer\n"
      "  -h          Toon deze hulptekst\n"
      "  -V          Toon versienummer\n");
}

enum MODE {
  M_LINES = 1 << 0,
  M_WORDS = 1 << 1,
  M_BYTES = 1 << 2,
};

// Returns pointer to argument array containing the file names
static char** parse_options(int argc, char **argv, int *modeMap) {
  int opt;
  while ((opt = getopt(argc, argv, "cwlhV")) != -1) {
    switch (opt) {
      case 'c':
        *modeMap |= M_BYTES;
        break;

      case 'w':
        *modeMap |= M_WORDS;
        break;

      case 'l':
        *modeMap |= M_LINES;
        break;

      case 'h':
        usage(stdout);
        exit(0);

      case 'V':
        drukkedoos_print_versie(stdout);
        exit(0);

      case '?':
        fprintf(stderr, "toilet: Ongeldige optie: -%c\n", optopt);
        usage(stderr);
        exit(1);
    }
  }

  return argv + optind;
}

static size_t get_count(enum MODE mode, struct filebuf *fb) {
  switch (mode) {
    case M_BYTES:
      return fb->sz;

    case M_WORDS: {
#define IN(i) (i < fb->sz)

      size_t words = 0;

      // (c) Tom Forging
      for (size_t i = 0; IN(i);) {
        size_t previ = i;
        while (IN(i) && !isspace(fb->buf[i])) i++;
        words += i != previ;
        while (IN(i) && isspace(fb->buf[i])) i++;
      }

      return words;

#undef IN
    }

    case M_LINES: {
      size_t lines = 0;
      size_t i = 0;

      while (i != fb->sz) {
        if (fb->buf[i] == '\n') lines++;
        i++;
      }

      // handle case if file does not have trailing newline
      if (fb->buf[i - 1] != '\n') {
        lines++;
      }

      return lines;
    }

    default:
      assert(false);
  }
}

static int mlog2(int val) {
#define C(n) case (1 << (n)): return (n);

  switch (val) {
    C(0) C(1) C(2)
  }
  assert(false);

#undef C
}

static void init_finfo(int argc) {
  assert(finfo == NULL);
  argc += 1; // one more for the total tally
  if (argc < 8) argc = 8;

  finfo = calloc(argc, sizeof(struct finfo));
  finfocap = argc;
}

static struct finfo *add_fname(char *fname) {
  assert(finfo != NULL);

  if (finfolen == finfocap) {
    finfocap *= 2;
    finfo = realloc(finfo, sizeof(struct finfo)*finfocap);
  }

  struct finfo *ptr = finfo + finfolen;
  ptr->values[0] = 0;
  ptr->values[1] = 0;
  ptr->values[2] = 0;
  ptr->fname = fname;

  finfolen++;
  return ptr;
}

static int process(struct filebuf *fb, char *fname, bool) {
  struct finfo *finfo = add_fname(fname);

  for (enum MODE mode = 1; mode <= M_BYTES; mode <<= 1) {
    if (mode & modeMap) {
      finfo->values[mlog2(mode)] = get_count(mode, fb);
    }
  }
  free_filebuf(fb);
  return 0;
}

// TODO: be smarter, toilet doesn't have to read the whole file in memory (for
// unmappable files)

int entry_toilet(int argc, char **argv) {
  init_finfo(argc);

  modeMap = 0;
  char **args = parse_options(argc, argv, &modeMap);
  if (modeMap == 0) {
    modeMap = INT_MAX;
  }

  const int res = loop_files(args, process);

  // tally file totals
  {
    struct finfo *ptr = add_fname("total");

    for (size_t i = 0; i < 3; i++) {
      for (size_t j = 0; j < finfolen; j++) {
        ptr->values[i] += finfo[j].values[i];
      }
    }
  }

  // calculate required padding
  int pad[3] = {0};
  for (size_t i = 0; i < finfolen; i++) {
    for (size_t j = 0; j < 3; j++) {
      const int sz = ceill(log10l(finfo[i].values[j])) + 1;
      if (sz > pad[j]) pad[j] = sz;
    }
  }

  // print all results
  for (size_t i = 0; i < finfolen; i++) {
    struct finfo *ptr = finfo + i;
    for (size_t j = 0; j < 3; j++) {
      printf("% *li ", pad[j], ptr->values[j]);
    }
    printf("%s\n", ptr->fname);
  }


  return res;
}