Add some resistance for timesheet file syntax error

This commit is contained in:
Nikolay Puzanov 2014-04-17 20:21:16 +04:00
parent 57d05af68f
commit d086efdafe

View File

@ -89,22 +89,27 @@
;;; Parse task string and return list: ;;; Parse task string and return list:
;;; '((list of path elements) start-date stop-date duration) ;;; '((list of path elements) start-date stop-date duration)
(define (parse-task-string str) (define (parse-task-string str)
(let ((path (let ((str (string-trim-both str)))
(let ((path-end (string-index str #\:))) (if (or (zero? (string-length str))
(if (not path-end) '() (equal? (string-ref str 0) #\#))
(path-split str 0 path-end)))) #f
(dates (substring/find str #\[ #\]))) (let ((path
(if (null? dates) '() (let ((path-end (string-index str #\:)))
(let ((date-start (string->date (car dates) date-format))) (if (not path-end) #f
(let-values (((date-end duration) (path-split str 0 path-end))))
(if (null? (cdr dates)) (dates (substring/find str #\[ #\])))
(values #f #f) (if (or (not path) (not dates))
(let* ((date-end (string->date (cadr dates) date-format)) #f
(duration (time-difference (let ((date-start (string->date (car dates) date-format)))
(date->time-utc date-end) (let-values (((date-end duration)
(date->time-utc date-start)))) (if (null? (cdr dates))
(values date-end duration))))) (values #f #f)
(list path date-start date-end duration)))))) (let* ((date-end (string->date (cadr dates) date-format))
(duration (time-difference
(date->time-utc date-end)
(date->time-utc date-start))))
(values date-end duration)))))
(list path date-start date-end duration))))))))
;;; Parse timesheet file and return list of tasks ;;; Parse timesheet file and return list of tasks
(define (read-timesheet filename) (define (read-timesheet filename)
@ -114,7 +119,8 @@
(let ((line (get-line port))) (let ((line (get-line port)))
(if (eof-object? line) (if (eof-object? line)
(reverse recs) (reverse recs)
(loop (cons (parse-task-string line) recs)))))))) (loop (let ((item (parse-task-string line)))
(if item (cons item recs) recs)))))))))
;;; Return difference of two dates ;;; Return difference of two dates
(define (date-difference d1 d2) (define (date-difference d1 d2)