diff options
-rw-r--r-- | pnm/read.scm | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/pnm/read.scm b/pnm/read.scm index 5cfec94..e3cb84b 100644 --- a/pnm/read.scm +++ b/pnm/read.scm @@ -49,8 +49,11 @@ (unexpected-char-error)) n)) (define (read-whitespaces*) - (when (eof-object? (read-whitespaces in)) - (unexpected-eof-error))) + (let ((result (read-whitespaces in))) + (when (eof-object? result) + (unexpected-eof-error)) + (unless result + (error "(pnm read) image-read: Unexpected character (expected whitespace)")))) (let ((magic-number (read-magic-number*))) (read-whitespaces*) @@ -105,11 +108,22 @@ (cond ((eof-object? u8) (eof-object)) ((newline? u8) (read-u8 in) - (read-whitespaces in)) + (skip-whitespace in)) (else (read-u8 in) (read-comment in))))) + (define (skip-whitespace in) + (let ((u8 (peek-u8 in))) + (cond ((eof-object? u8) #t) + ((whitespace? u8) + (read-u8 in) + (skip-whitespace in)) + ((comment? u8) + (read-u8 in) + (read-comment in)) + (else #t)))) + (define (digit? u8) (and (<= 48 u8) (<= u8 57))) @@ -134,11 +148,8 @@ (cond ((eof-object? u8) (eof-object)) ((whitespace? u8) (read-u8 in) - (read-whitespaces in)) - ((comment? u8) - (read-u8 in) - (read-comment in)) - (else #t)))) + (skip-whitespace in)) + (else #f)))) (define (read-number in) (call-with-current-continuation |