Skip to content

Commit 9f06467

Browse files
author
Matthias Andreas Benkard
committed
Add class MLKFileHandleStream.
1 parent 3a5710b commit 9f06467

5 files changed

+136
-6
lines changed

GNUmakefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ ToiletKit_OBJC_FILES = functions.m globals.m MLKArray.m \
6767
MLKCommaReader.m MLKCompiledClosure.m MLKCons.m \
6868
MLKDoubleFloat.m \
6969
MLKDispatchingMacroCharacterReader.m \
70-
MLKDynamicContext.m MLKEnvironment.m MLKFloat.m \
70+
MLKDynamicContext.m MLKEnvironment.m \
71+
MLKFileHandleStream.m MLKFloat.m \
7172
MLKForeignProcedure.m MLKForm.m MLKInteger.m \
7273
MLKInterpretedClosure.m MLKInterpreter.m \
7374
MLKLexicalContext.m MLKLexicalEnvironment.m \

MLKDynamicContext.m

+21-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
#import <Foundation/NSThread.h>
2626

2727
#import "MLKBackquoteReader.h"
28+
#import "MLKBinaryStreamCharacterStream.h"
29+
#import "MLKCharacterStream.h"
2830
#import "MLKCommaReader.h"
2931
#import "MLKCons.h"
3032
#import "MLKDispatchingMacroCharacterReader.h"
3133
#import "MLKDynamicContext.h"
3234
#import "MLKEnvironment.h"
35+
#import "MLKFileHandleStream.h"
3336
#import "MLKPackage.h"
3437
#import "MLKParenReader.h"
3538
#import "MLKQuoteReader.h"
@@ -213,6 +216,21 @@ +(void) initialize
213216
// FIXME: Initialise stuff.
214217
#define INIT(VARNAME, VALUE) [vars setObject:VALUE forKey:[cl intern:VARNAME]]
215218

219+
MLKFileHandleStream *ferrstream, *foutstream;
220+
MLKCharacterStream *errstream, *outstream;
221+
ferrstream = [[MLKFileHandleStream alloc]
222+
initWithFileHandle:[NSFileHandle fileHandleWithStandardError]];
223+
foutstream = [[MLKFileHandleStream alloc]
224+
initWithFileHandle:[NSFileHandle fileHandleWithStandardOutput]];
225+
errstream = [[MLKBinaryStreamCharacterStream alloc]
226+
initWithBinaryStream:ferrstream];
227+
outstream = [[MLKBinaryStreamCharacterStream alloc]
228+
initWithBinaryStream:foutstream];
229+
LAUTORELEASE (ferrstream);
230+
LAUTORELEASE (foutstream);
231+
LAUTORELEASE (errstream);
232+
LAUTORELEASE (outstream);
233+
216234
INIT(@"*BREAK-ON-SIGNALS*", NIL);
217235
INIT(@"*COMPILE-FILE-PATHNAME*", NIL);
218236
INIT(@"*COMPILE-FILE-TRUENAME*", NIL);
@@ -221,7 +239,7 @@ +(void) initialize
221239
// INIT(@"*DEBUG-IO*", );
222240
INIT(@"*DEBUGGER-HOOK*", NIL);
223241
// INIT(@"*DEFAULT-PATHNAME-DEFAULTS*", );
224-
// INIT(@"*ERROR-OUTPUT*", );
242+
INIT(@"*ERROR-OUTPUT*", errstream);
225243
INIT(@"*FEATURES*", [MLKCons
226244
cons:[keyword intern:@"ETOILET"]
227245
with:[MLKCons
@@ -260,9 +278,9 @@ +(void) initialize
260278
INIT(@"*READ-SUPPRESS*", NIL); //FIXME: Support in reader
261279
INIT(@"*READTABLE*", readtable);
262280
// INIT(@"*STANDARD-INPUT*", );
263-
// INIT(@"*STANDARD-OUTPUT*", );
281+
INIT(@"*STANDARD-OUTPUT*", outstream);
264282
// INIT(@"*TERMINAL-IO*", );
265-
// INIT(@"*TRACE-OUTPUT* ", );
283+
INIT(@"*TRACE-OUTPUT* ", outstream);
266284

267285
[vars setObject:NIL forKey:[[MLKPackage findPackage:@"TOILET-SYSTEM"]
268286
intern:@"*SYSTEM-INITIALISED-P*"]];

MLKFileHandleStream.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* -*- mode: objc; coding: utf-8 -*- */
2+
/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime.
3+
* Copyright (C) 2008 Matthias Andreas Benkard.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or (at
8+
* your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#import "MLKBinaryStream.h"
20+
21+
#import <Foundation/NSFileHandle.h>
22+
23+
@interface MLKFileHandleStream : MLKBinaryStream
24+
{
25+
NSFileHandle *_fileHandle;
26+
BOOL _closeWhenDone;
27+
}
28+
29+
-(id) initWithFileHandle:(NSFileHandle *)fileHandle;
30+
-(id) initWithFileHandle:(NSFileHandle *)fileHandle
31+
closeWhenDone:(BOOL)closeWhenDone;
32+
33+
-(uint8_t) readOctet;
34+
-(void) writeOctet:(uint8_t)octet;
35+
@end

MLKFileHandleStream.m

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* -*- mode: objc; coding: utf-8 -*- */
2+
/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime.
3+
* Copyright (C) 2008 Matthias Andreas Benkard.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or (at
8+
* your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful, but
11+
* WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#import "MLKFileHandleStream.h"
20+
#import "runtime-compatibility.h"
21+
#import "util.h"
22+
23+
#import <Foundation/NSData.h>
24+
#import <Foundation/NSException.h>
25+
26+
#include <unistd.h>
27+
#include <stdlib.h>
28+
#include <string.h>
29+
30+
31+
@implementation MLKFileHandleStream
32+
-(id) initWithFileHandle:(NSFileHandle *)fileHandle
33+
{
34+
return [self initWithFileHandle:fileHandle
35+
closeWhenDone:NO];
36+
}
37+
38+
-(id) initWithFileHandle:(NSFileHandle *)fileHandle
39+
closeWhenDone:(BOOL)closeWhenDone
40+
{
41+
self = [super init];
42+
LASSIGN (_fileHandle, fileHandle);
43+
_closeWhenDone = closeWhenDone;
44+
return self;
45+
}
46+
47+
-(uint8_t) readOctet
48+
{
49+
NSData *data;
50+
51+
data = [_fileHandle readDataOfLength:1];
52+
53+
if ([data length] == 0)
54+
{
55+
[NSException raise:@"MLKStreamError"
56+
format:@"Tried to read beyond end of file."];
57+
}
58+
59+
return *(uint8_t*)[data bytes];
60+
}
61+
62+
-(void) writeOctet:(uint8_t)octet
63+
{
64+
[_fileHandle writeData:[NSData dataWithBytesNoCopy:&octet
65+
length:1
66+
freeWhenDone:NO]];
67+
}
68+
69+
-(void) dealloc
70+
{
71+
if (_closeWhenDone)
72+
{
73+
[_fileHandle close];
74+
}
75+
LDESTROY (_fileHandle);
76+
[super dealloc];
77+
}
78+
@end

MLKStreamStream.h

-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
#import "MLKBinaryStream.h"
2020

2121
#import <Foundation/NSStream.h>
22-
#import <Foundation/NSString.h>
2322

2423
@interface MLKStreamStream : MLKBinaryStream
2524
{
2625
NSInputStream *_input;
2726
NSOutputStream *_output;
28-
NSStringEncoding _encoding;
2927
BOOL _closeInputWhenDone;
3028
BOOL _closeOutputWhenDone;
3129
}

0 commit comments

Comments
 (0)