From 194b0af8b8945212efe0ea76579e166274184389 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 17 Jun 2008 17:48:09 +0200 Subject: MLKStream#-readChar: Do something sane. --- MLKStream.m | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/MLKStream.m b/MLKStream.m index e59dee4..28a2f65 100644 --- a/MLKStream.m +++ b/MLKStream.m @@ -72,14 +72,21 @@ _charCached = NO; return ch; } - + buffer = NULL; - for (i = 0; i++;) + for (i = 0;; i++) { NSString *tmpstr; + ssize_t bytes_read; buffer = realloc (buffer, i+1); - [_input read:(buffer+i) maxLength:1]; + bytes_read = [_input read:(buffer+i) maxLength:1]; + // NSLog (@"%d bytes read", bytes_read); + if (!bytes_read) + { + [[MLKError errorWithMessage:@"Tried to read beyond end of file."] raise]; + } + tmpstr = [[NSString alloc] initWithBytesNoCopy:buffer length:(i+1) encoding:_encoding @@ -87,17 +94,21 @@ if ([tmpstr length] == 1) { retval = [tmpstr characterAtIndex:0]; + [tmpstr release]; + return retval; } - [tmpstr release]; } - return retval; + return -1; } -(void) unreadChar:(unichar)ch { if (_charCached) [[MLKError errorWithMessage:@"Attempted to UNREAD-CHAR twice in a row."] raise]; + + _charCached = YES; + _cachedChar = ch; } -(BOOL) isEOF -- cgit v1.2.3