1// 2// ANTLRReaderStream.m 3// ANTLR 4// 5// Created by Alan Condit on 2/21/11. 6// Copyright 2011 Alan's MachineWorks. All rights reserved. 7// 8 9#import "ANTLRReaderStream.h" 10 11 12@implementation ANTLRReaderStream 13 14@synthesize fh; 15@synthesize size; 16@synthesize rbSize; 17 18static NSInteger READ_BUFFER_SIZE = 1024; 19static NSInteger INITIAL_BUFFER_SIZE = 1024; 20 21+ (NSInteger) READ_BUFFER_SIZE 22{ 23 return READ_BUFFER_SIZE; 24} 25 26+ (NSInteger) INITIAL_BUFFER_SIZE 27{ 28 return INITIAL_BUFFER_SIZE; 29} 30 31+ (id) newANTLRReaderStream 32{ 33 return [[ANTLRReaderStream alloc] init]; 34} 35 36+ (id) newANTLRReaderStream:(NSFileHandle *)r 37{ 38 return [[ANTLRReaderStream alloc] initWithReader:r size:INITIAL_BUFFER_SIZE readBufferSize:READ_BUFFER_SIZE]; 39} 40 41+ (id) newANTLRReaderStream:(NSFileHandle *)r size:(NSInteger)aSize 42{ 43 return [[ANTLRReaderStream alloc] initWithReader:r size:aSize readBufferSize:READ_BUFFER_SIZE]; 44} 45 46+ (id) newANTLRReaderStream:(NSFileHandle *)r size:(NSInteger)aSize readBufferSize:(NSInteger)aReadChunkSize 47{ 48// load(r, aSize, aReadChunkSize); 49 return [[ANTLRReaderStream alloc] initWithReader:r size:aSize readBufferSize:aReadChunkSize]; 50} 51 52- (id) init 53{ 54 self = [super init]; 55 if ( self != nil ) { 56 fh = nil; 57 rbSize = READ_BUFFER_SIZE; 58 size = INITIAL_BUFFER_SIZE; 59 } 60 return self; 61} 62 63- (id) initWithReader:(NSFileHandle *)r size:(NSInteger)aSize readBufferSize:(NSInteger)aReadChunkSize 64{ 65 self = [super init]; 66 if ( self != nil ) { 67 fh = r; 68 rbSize = aSize; 69 size = aReadChunkSize; 70 [self load:aSize readBufferSize:aReadChunkSize]; 71 } 72 return self; 73} 74 75- (void) load:(NSInteger)aSize readBufferSize:(NSInteger)aReadChunkSize 76{ 77 NSData *retData = nil; 78 if ( fh==nil ) { 79 return; 80 } 81 if ( aSize<=0 ) { 82 aSize = INITIAL_BUFFER_SIZE; 83 } 84 if ( aReadChunkSize<=0 ) { 85 aReadChunkSize = READ_BUFFER_SIZE; 86 } 87#pragma mark fix these NSLog calls 88 @try { 89 int numRead=0; 90 int p1 = 0; 91 retData = [fh readDataToEndOfFile]; 92 numRead = [retData length]; 93 NSLog( @"read %d chars; p was %d is now %d", n, p1, (p1+numRead) ); 94 p1 += numRead; 95 n = p1; 96 data = [[NSString alloc] initWithData:retData encoding:NSASCIIStringEncoding]; 97 NSLog( @"n=%d", n ); 98 } 99 @finally { 100 [fh closeFile]; 101 } 102} 103 104- (void)setUpStreamForFile:(NSString *)path { 105 // iStream is NSInputStream instance variable 106 NSInputStream *iStream = [[NSInputStream alloc] initWithFileAtPath:path]; 107// [iStream setDelegate:self]; 108 [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 109 forMode:NSDefaultRunLoopMode]; 110 [iStream open]; 111} 112 113- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode 114{ 115 NSMutableData *myData = nil; 116 NSNumber *bytesRead = [NSNumber numberWithInteger:0]; 117 switch(eventCode) { 118 case NSStreamEventHasBytesAvailable: 119 { 120 if(!myData) { 121 myData = [[NSMutableData data] retain]; 122 } 123 uint8_t buf[1024]; 124 unsigned int len = 0; 125 len = [(NSInputStream *)stream read:buf maxLength:1024]; 126 if(len) { 127 [myData appendBytes:(const void *)buf length:len]; 128 // bytesRead is an instance variable of type NSNumber. 129 bytesRead = [NSNumber numberWithInteger:[bytesRead intValue]+len]; 130 } else { 131 NSLog(@"no buffer!"); 132 } 133 break; 134 } 135 case NSStreamEventEndEncountered: 136 { 137 [stream close]; 138 [stream removeFromRunLoop:[NSRunLoop currentRunLoop] 139 forMode:NSDefaultRunLoopMode]; 140 [stream release]; 141 stream = nil; // stream is ivar, so reinit it 142 break; 143 } 144 // continued 145 } 146} 147 148- (void) close 149{ 150 [fh closeFile]; 151} 152 153@end 154