diff options
author | Matthias Andreas Benkard <matthias@benkard.de> | 2008-06-17 17:48:09 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <matthias@benkard.de> | 2008-06-17 17:48:09 +0200 |
commit | 194b0af8b8945212efe0ea76579e166274184389 (patch) | |
tree | 581957757d39c51ce29e59e614b8c5887f3ce7ed | |
parent | 4aa6da8ed37055e196204a45e3bac32039b395d9 (diff) |
MLKStream#-readChar: Do something sane.
-rw-r--r-- | MLKStream.m | 21 |
1 files 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 |