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,12 +89,17 @@
;;; Parse task string and return list:
;;; '((list of path elements) start-date stop-date duration)
(define (parse-task-string str)
(let ((str (string-trim-both str)))
(if (or (zero? (string-length str))
(equal? (string-ref str 0) #\#))
#f
(let ((path
(let ((path-end (string-index str #\:)))
(if (not path-end) '()
(if (not path-end) #f
(path-split str 0 path-end))))
(dates (substring/find str #\[ #\])))
(if (null? dates) '()
(if (or (not path) (not dates))
#f
(let ((date-start (string->date (car dates) date-format)))
(let-values (((date-end duration)
(if (null? (cdr dates))
@ -104,7 +109,7 @@
(date->time-utc date-end)
(date->time-utc date-start))))
(values date-end duration)))))
(list path date-start date-end duration))))))
(list path date-start date-end duration))))))))
;;; Parse timesheet file and return list of tasks
(define (read-timesheet filename)
@ -114,7 +119,8 @@
(let ((line (get-line port)))
(if (eof-object? line)
(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
(define (date-difference d1 d2)