From 9f06467f57576cd557ee172f10f5e12bba7f1f9e Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 28 Aug 2008 16:55:53 +0200 Subject: Add class MLKFileHandleStream. --- GNUmakefile | 3 +- MLKDynamicContext.m | 24 ++++++++++++++-- MLKFileHandleStream.h | 35 +++++++++++++++++++++++ MLKFileHandleStream.m | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ MLKStreamStream.h | 2 -- 5 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 MLKFileHandleStream.h create mode 100644 MLKFileHandleStream.m diff --git a/GNUmakefile b/GNUmakefile index 7aaa0f0..1990162 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -67,7 +67,8 @@ ToiletKit_OBJC_FILES = functions.m globals.m MLKArray.m \ MLKCommaReader.m MLKCompiledClosure.m MLKCons.m \ MLKDoubleFloat.m \ MLKDispatchingMacroCharacterReader.m \ - MLKDynamicContext.m MLKEnvironment.m MLKFloat.m \ + MLKDynamicContext.m MLKEnvironment.m \ + MLKFileHandleStream.m MLKFloat.m \ MLKForeignProcedure.m MLKForm.m MLKInteger.m \ MLKInterpretedClosure.m MLKInterpreter.m \ MLKLexicalContext.m MLKLexicalEnvironment.m \ diff --git a/MLKDynamicContext.m b/MLKDynamicContext.m index 015af47..70979cf 100644 --- a/MLKDynamicContext.m +++ b/MLKDynamicContext.m @@ -25,11 +25,14 @@ #import #import "MLKBackquoteReader.h" +#import "MLKBinaryStreamCharacterStream.h" +#import "MLKCharacterStream.h" #import "MLKCommaReader.h" #import "MLKCons.h" #import "MLKDispatchingMacroCharacterReader.h" #import "MLKDynamicContext.h" #import "MLKEnvironment.h" +#import "MLKFileHandleStream.h" #import "MLKPackage.h" #import "MLKParenReader.h" #import "MLKQuoteReader.h" @@ -213,6 +216,21 @@ static MLKDynamicContext *global_context; // FIXME: Initialise stuff. #define INIT(VARNAME, VALUE) [vars setObject:VALUE forKey:[cl intern:VARNAME]] + MLKFileHandleStream *ferrstream, *foutstream; + MLKCharacterStream *errstream, *outstream; + ferrstream = [[MLKFileHandleStream alloc] + initWithFileHandle:[NSFileHandle fileHandleWithStandardError]]; + foutstream = [[MLKFileHandleStream alloc] + initWithFileHandle:[NSFileHandle fileHandleWithStandardOutput]]; + errstream = [[MLKBinaryStreamCharacterStream alloc] + initWithBinaryStream:ferrstream]; + outstream = [[MLKBinaryStreamCharacterStream alloc] + initWithBinaryStream:foutstream]; + LAUTORELEASE (ferrstream); + LAUTORELEASE (foutstream); + LAUTORELEASE (errstream); + LAUTORELEASE (outstream); + INIT(@"*BREAK-ON-SIGNALS*", NIL); INIT(@"*COMPILE-FILE-PATHNAME*", NIL); INIT(@"*COMPILE-FILE-TRUENAME*", NIL); @@ -221,7 +239,7 @@ static MLKDynamicContext *global_context; // INIT(@"*DEBUG-IO*", ); INIT(@"*DEBUGGER-HOOK*", NIL); // INIT(@"*DEFAULT-PATHNAME-DEFAULTS*", ); - // INIT(@"*ERROR-OUTPUT*", ); + INIT(@"*ERROR-OUTPUT*", errstream); INIT(@"*FEATURES*", [MLKCons cons:[keyword intern:@"ETOILET"] with:[MLKCons @@ -260,9 +278,9 @@ static MLKDynamicContext *global_context; INIT(@"*READ-SUPPRESS*", NIL); //FIXME: Support in reader INIT(@"*READTABLE*", readtable); // INIT(@"*STANDARD-INPUT*", ); - // INIT(@"*STANDARD-OUTPUT*", ); + INIT(@"*STANDARD-OUTPUT*", outstream); // INIT(@"*TERMINAL-IO*", ); - // INIT(@"*TRACE-OUTPUT* ", ); + INIT(@"*TRACE-OUTPUT* ", outstream); [vars setObject:NIL forKey:[[MLKPackage findPackage:@"TOILET-SYSTEM"] intern:@"*SYSTEM-INITIALISED-P*"]]; diff --git a/MLKFileHandleStream.h b/MLKFileHandleStream.h new file mode 100644 index 0000000..9d5de5f --- /dev/null +++ b/MLKFileHandleStream.h @@ -0,0 +1,35 @@ +/* -*- mode: objc; coding: utf-8 -*- */ +/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime. + * Copyright (C) 2008 Matthias Andreas Benkard. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#import "MLKBinaryStream.h" + +#import + +@interface MLKFileHandleStream : MLKBinaryStream +{ + NSFileHandle *_fileHandle; + BOOL _closeWhenDone; +} + +-(id) initWithFileHandle:(NSFileHandle *)fileHandle; +-(id) initWithFileHandle:(NSFileHandle *)fileHandle + closeWhenDone:(BOOL)closeWhenDone; + +-(uint8_t) readOctet; +-(void) writeOctet:(uint8_t)octet; +@end diff --git a/MLKFileHandleStream.m b/MLKFileHandleStream.m new file mode 100644 index 0000000..2376dc0 --- /dev/null +++ b/MLKFileHandleStream.m @@ -0,0 +1,78 @@ +/* -*- mode: objc; coding: utf-8 -*- */ +/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime. + * Copyright (C) 2008 Matthias Andreas Benkard. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#import "MLKFileHandleStream.h" +#import "runtime-compatibility.h" +#import "util.h" + +#import +#import + +#include +#include +#include + + +@implementation MLKFileHandleStream +-(id) initWithFileHandle:(NSFileHandle *)fileHandle +{ + return [self initWithFileHandle:fileHandle + closeWhenDone:NO]; +} + +-(id) initWithFileHandle:(NSFileHandle *)fileHandle + closeWhenDone:(BOOL)closeWhenDone +{ + self = [super init]; + LASSIGN (_fileHandle, fileHandle); + _closeWhenDone = closeWhenDone; + return self; +} + +-(uint8_t) readOctet +{ + NSData *data; + + data = [_fileHandle readDataOfLength:1]; + + if ([data length] == 0) + { + [NSException raise:@"MLKStreamError" + format:@"Tried to read beyond end of file."]; + } + + return *(uint8_t*)[data bytes]; +} + +-(void) writeOctet:(uint8_t)octet +{ + [_fileHandle writeData:[NSData dataWithBytesNoCopy:&octet + length:1 + freeWhenDone:NO]]; +} + +-(void) dealloc +{ + if (_closeWhenDone) + { + [_fileHandle close]; + } + LDESTROY (_fileHandle); + [super dealloc]; +} +@end diff --git a/MLKStreamStream.h b/MLKStreamStream.h index 3bd4f1b..6fabb50 100644 --- a/MLKStreamStream.h +++ b/MLKStreamStream.h @@ -19,13 +19,11 @@ #import "MLKBinaryStream.h" #import -#import @interface MLKStreamStream : MLKBinaryStream { NSInputStream *_input; NSOutputStream *_output; - NSStringEncoding _encoding; BOOL _closeInputWhenDone; BOOL _closeOutputWhenDone; } -- cgit v1.2.3