~rostiger/logbook

ref: 8de4a4569a3e47fd5ead2a496e135fc47b62ec41 logbook/main.c -rw-r--r-- 7.7 KiB
8de4a456rostiger Logging 6 months ago
                                                                                
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "standard/standard.h"
#include <stdio.h>
#include <time.h>

#define STRMEM 4096 * 96
#define LOGMEM 4000
#define LOGPATH "logs.md"

typedef struct Block {
  int len;
  char data[STRMEM];
} Block;

typedef struct Time {
  int m, h;
} Time;

typedef struct Log {
  int date;
  Time start, end;
  char *name, *comment;
} Log;

typedef struct Project {
  int len;
  char *name;
  Time total;
  Log entries[LOGMEM];
} Project;

typedef struct Logbook {
  int len, plen;
  Time total;
  Log logs[LOGMEM];
  Project projects[LOGMEM];
} Logbook;

char *push(Block *b, char *s) {
  int i = 0, o = b->len;
  while (s[i])
    b->data[b->len++] = s[i++];
  b->data[b->len++] = '\0';
  return &b->data[o];
}

int error(char *msg, const char *err) {
  printf("Error %s: %s\n", msg, err);
  return 0;
}

int errorid(char *msg, char *val, int id) {
  printf("Error: %s:%d(%s)\n", msg, id, val);
  return 0;
}

Log *makelog(Log *l, char *name) {
  l->date = 0;
  l->name = name;
  return l;
}

Project *makeProject(Project *p, char *name) {
  p->name = name;
  p->len = 0;
  return p;
}

int project_index(char *name, Logbook *logbook) {
  int i;
  if (!&logbook->plen)
    return -1;
  for (i = 0; i < logbook->plen; i++) {
    if (scmp(name, logbook->projects[i].name))
      return i;
  }
  return -1;
}

int ispl(int i, int index, int len) {
  int c = 0;
  char src[128], dst[32];
  sprintf(src, "%d", i);
  dst[0] = '\0';
  while (c < len) {
    dst[c] = src[index + c];
    c++;
  }
  dst[len] = '\0';
  return sint(dst, len + 1);
}

char *stpad(char *src, char *dst, int len, char rune, int right) {
  int srclen, pad, j = 0;
  srclen = slen(src);
  pad = len - srclen;
  dst[0] = '\0';
  while (j <= len) {
    if (!right) {
      if (j < pad)
        dst[j] = rune;
      else
        dst[j] = src[j - pad];
    } else {
      if (j < srclen)
        dst[j] = src[j];
      else
        dst[j] = rune;
    }
    j++;
  }
  dst[j] = '\0';
  return dst;
}

char *ipad(int i, char *dst, int len, char rune, int right) {
  char src[128];
  sprintf(src, "%d", i);
  return stpad(src, dst, len, rune, right);
}

#pragma - mark time

time_t ymdstrtime(int y, int m, int d) {
  struct tm stime;
  stime.tm_year = y - 1900;
  stime.tm_mday = d;
  stime.tm_mon = m - 1;
  stime.tm_hour = 0;
  stime.tm_min = 0;
  stime.tm_sec = 1;
  stime.tm_isdst = -1;
  return mktime(&stime);
}

time_t itotime(int i) {
  int year, month, day;
  year = ispl(i, 0, 2) + 2000;
  month = ispl(i, 2, 2);
  day = ispl(i, 4, 2);
  return ymdstrtime(year, month, day);
}

Time time_diff(Time start, Time end) {
  Time t;
  if (end.m - start.m < 0) {
    t.m = start.m - end.m;
    start.h++;
  } else
    t.m = end.m - start.m;
  t.h = end.h - start.h > 0 ? end.h - start.h : 0;
  return t;
}

Time time_add(Time a, Time b) {
  Time t = a;
  t.m = a.m + b.m;
  t.h = a.h + b.h;
  while (t.m >= 60) {
    t.m -= 60;
    t.h++;
  }
  return t;
}

#pragma - mark entries

void ptotal(Project *p) {
  char name[64], h[16], m[16], e[16];
  stpad(p->name, name, 22, '.', 1);
  ipad(p->total.h, h, 4, ' ', 0);
  ipad(p->total.m, m, 2, '0', 0);
  ipad(p->len, e, 5, ' ', 0);
  printf("%s | %s:%s |%s | %d |\n", name, h, m, e, p->entries[p->len - 1].date);
}

void pentries(Project *p, int start, int end) {
  int i;
  char sh[16], sm[16], eh[16], em[16], h[16], m[16];
  Time dur;
  for (i = 0; i < p->len; i++) {
    Log l = p->entries[i];
    if (l.date < start || l.date > end)
      continue;
    dur = time_diff(l.start, l.end);
    ipad(l.start.h, sh, 2, '0', 0);
    ipad(l.start.m, sm, 2, '0', 0);
    ipad(l.end.h, eh, 2, '0', 0);
    ipad(l.end.m, em, 2, '0', 0);
    ipad(dur.h, h, 2, '0', 0);
    ipad(dur.m, m, 2, '0', 0);
    printf("%d: %s%s - %s%s (%s:%s) - %s\n", l.date, sh, sm, eh, em, h, m,
           l.comment);
  }
}

void pentriestotal(Project *p, int start, int end) {
  int i, entries;
  Time total, dur;
  time_t first, last;
  int days;
  total.h = 0;
  total.m = 0;
  entries = 0;
  for (i = 0; i < p->len; i++) {
    Log l = p->entries[i];
    if (l.date < start || l.date > end)
      continue;
    dur = time_diff(l.start, l.end);
    total = time_add(total, dur);
    entries++;
  }
  first = itotime(start);
  last = itotime(end);
  days = difftime(last, first) / 86400 + 1;
  puts("--------------------------------------------------+");
  printf("Project:\t%s\n", p->name);
  printf("Date Range:\t%d - %d (%d days)\n", start, end, days);
  printf("Entries:\t%d logs\n", entries);
  printf("Time:\t\t%d:", total.h);
  if (total.m > 0)
    printf("%d\n", total.m);
  else
    puts("00\n");
}

#pragma - mark parsing

int parse_logbook(FILE *fp, Block *block, Logbook *logbook) {
  int len, i;
  char *name;
  char line[256], buf1[256], buf2[256];
  Log *l = &logbook->logs[logbook->len];
  Project *p = &logbook->projects[logbook->plen];
  Time diff;
  while (fgets(line, 256, fp)) {
    len = slen(strm(line));
    if (len < 14 || line[0] == ';')
      continue;
    if (logbook->len >= LOGMEM)
      return errorid("Increase memory", "Logbook", logbook->len);
    if (len > 120)
      return errorid("Log is too long", line, len);
    name = strm(sstr(line, buf1, 17, 15));
    l = makelog(&logbook->logs[logbook->len++], push(block, name));
    if (len >= 0)
      l->date = sint(line, 6);
    if (len >= 7) {
      l->start.h = sint(line + 7, 2);
      l->start.m = sint(line + 9, 2);
    }
    if (len >= 12) {
      l->end.h = sint(line + 12, 2);
      l->end.m = sint(line + 14, 2);
    }
    if (len >= 32)
      l->comment = push(block, strm(sstr(line, buf2, 32, 72)));
    i = project_index(name, logbook);
    if (i < 0) {
      p = makeProject(&logbook->projects[logbook->plen++], push(block, name));
    } else
      p = &logbook->projects[i];
    p->entries[p->len] = *l;
    diff = time_diff(l->start, l->end);
    p->total = time_add(p->total, diff);
    logbook->total = time_add(logbook->total, diff);
    p->len++;
  }
  return 1;
}

int parse(Block *block, Logbook *logs) {
  FILE *flogs = fopen(LOGPATH, "r");
  if (!flogs || !parse_logbook(flogs, block, logs)) {
    fclose(flogs);
    return error("Parsing", "Logbook");
  }
  fclose(flogs);
  return 1;
}

Block block;
Logbook logbook;

int main(int argc, char *argv[]) {
  int i, j, start, end;
  Project p;
  if (!parse(&block, &logbook))
    return error("Failure", "Parsing");
  puts("        _                                          ");
  puts("      _//         /        /                       ");
  puts("      /   __ _,  /____ __ /_                       ");
  puts("     /___(_)(_)_/_)(_)(_)/ <_                      ");
  puts("             /|                                    ");
  puts("            |/    v.1.9                            ");
  puts("--------------------------------------------------+");
  if (argc > 1) {
    j = project_index(argv[1], &logbook);
    if (j < 0)
      error("Couldn't find project in logbook", argv[1]);
    else {
      p = logbook.projects[j];
      if (argv[2] && slen(argv[2]) == 6)
        start = sint(argv[2], 6);
      else
        start = p.entries[p.len - 1].date;
      if (argv[3] && slen(argv[3]) == 6)
        end = sint(argv[3], 6);
      else
        end = p.entries[0].date;
      p = logbook.projects[j];
      pentries(&p, start, end);
      pentriestotal(&p, start, end);
    }
  } else {
    puts("Project                 |  hh:mm  |    # |  First |");
    puts("--------------------------------------------------+");
    for (i = 0; i < logbook.plen; i++) {
      p = logbook.projects[i];
      ptotal(&p);
    }
    puts("--------------------------------------------------+");
    printf("Projects......................... %d\n", logbook.plen);
    printf("Entries.......................... %d\n", logbook.len);
    printf("Tracked Time..................... %d:%d\n", logbook.total.h,
           logbook.total.m);
  }
  return 0;
}