timekeeper

My first (abandoned unfinished) web application for time tracking
git clone git://jacobedwards.org/timekeeper
Log | Files | Refs | README

export.c (2111B)


      1 #define const
      2 
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include "common.h"
      7 #include "../times.h"
      8 
      9 /* useful for scripting and such */
     10 enum kcgi_err
     11 serialize_epoch(struct pagedata *pd, time_t time)
     12 {
     13 	return khttp_printf(&pd->req, "%lld", time);
     14 }
     15 
     16 enum kcgi_err
     17 serialize_ustr(struct pagedata *pd, time_t time)
     18 {
     19 	char buf[64];
     20 
     21 	if (!khttp_epoch2ustr(time, buf, sizeof(buf)))
     22 		return KCGI_SYSTEM;
     23 	return khttp_puts(&pd->req, buf);
     24 }
     25 
     26 enum kcgi_err
     27 pageexport(struct pagedata *pd)
     28 {
     29 	enum kcgi_err status;
     30 	struct timesheet *times, *ts;
     31 	int n;
     32 	char *tmp;
     33 	enum kcgi_err (*serialize)(struct pagedata *, time_t) = serialize_ustr;
     34 	unsigned int period;
     35 
     36 	if (!pd->user)
     37 		return tk_prompt_login(pd);
     38 
     39 	if (pd->req.fieldmap[KeyFormat]) {
     40 		/*
     41 		 * format.timeformat
     42 		 * i.e. csv.epoch
     43 		 */
     44 		tmp = pd->req.fieldmap[KeyFormat]->parsed.s;
     45 		if (strcmp(tmp, ".epoch") != 0 && strcmp(tmp, "tsv.epoch") != 0)
     46 			return errorpage(pd, KHTTP_415);
     47 		serialize = serialize_epoch;
     48 	}
     49 
     50 	period = pd->req.fieldmap[KeyPeriod] ? pd->req.fieldmap[KeyPeriod]->parsed.i : 0;
     51 	if (gettimes(pd, pd->user->hash, period, &times))
     52 		return errorpage(pd, KHTTP_500);
     53 
     54 	if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS],
     55 		"%s", khttps[KHTTP_200])) != KCGI_OK ||
     56 	    (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE],
     57 		"text/tsv")) != KCGI_OK ||
     58 	    (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_DISPOSITION],
     59 		"attachment; filename=\"timekeeper-period-%d.tsv\"", period)) != KCGI_OK ||
     60 	    (status = khttp_body(&pd->req)) != KCGI_OK) {
     61 		free(times);
     62 		return status;
     63 	}
     64 
     65 	if (!times)
     66 		return KCGI_OK;
     67 
     68 	for (ts = times; ts; ts = ts->next) {
     69 		for (n = 0; n < 4; ++n) {
     70 			if (n && (status = khttp_putc(&pd->req, '\t')) != KCGI_OK) {
     71 				freetimesheet(times);
     72 				return status;
     73 			}
     74 
     75 			if (ts->set & timeflagmap[n]) {
     76 				status = serialize(pd, ts->times[n]);
     77 				if (status != KCGI_OK) {
     78 					freetimesheet(times);
     79 					return status;
     80 				}
     81 			}
     82 		}
     83 		if ((status = khttp_putc(&pd->req, '\n')) != KCGI_OK) {
     84 			freetimesheet(times);
     85 			return status;
     86 		}
     87 	}
     88 
     89 	freetimesheet(times);
     90 	return KCGI_OK;
     91 }