1/* 2 [The "BSD license"] 3 Copyright (c) 2005-2009 Terence Parr 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/** This is a char buffer stream that is loaded from a file 30 * all at once when you construct the object. This looks very 31 * much like an ANTLReader or ANTLRInputStream, but it's a special case 32 * since we know the exact size of the object to load. We can avoid lots 33 * of data copying. 34 */ 35 36#import "ANTLRFileStream.h" 37 38@implementation ANTLRFileStream 39 40@synthesize fileName; 41 42+ (id) newANTLRFileStream:(NSString*)fileName 43{ 44 return [[ANTLRFileStream alloc] init:fileName]; 45} 46 47+ (id) newANTLRFileStream:(NSString *)aFileName encoding:(NSStringEncoding)encoding 48{ 49 return [[ANTLRFileStream alloc] init:aFileName encoding:encoding]; 50} 51 52- (id) init:(NSString *)aFileName 53{ 54 self = [super init]; 55 if ( self != nil ) { 56 fileName = aFileName; 57 [self load:aFileName encoding:NSUTF8StringEncoding]; 58 } 59 return self; 60} 61 62- (id) init:(NSString *) aFileName encoding:(NSStringEncoding)encoding 63{ 64 self = [super init]; 65 if ( self != nil ) { 66 fileName = aFileName; 67 [self load:aFileName encoding:encoding]; 68 } 69 return self; 70} 71 72- (NSString *) getSourceName 73{ 74 return fileName; 75} 76 77- (void) load:(NSString *)aFileName encoding:(NSStringEncoding)encoding 78{ 79 if ( aFileName==nil ) { 80 return; 81 } 82 NSError *error; 83 NSData *retData = nil; 84 NSFileHandle *fh; 85 @try { 86 NSString *fn = [aFileName stringByStandardizingPath]; 87 NSURL *f = [NSURL fileURLWithPath:fn]; 88 fh = [NSFileHandle fileHandleForReadingFromURL:f error:&error]; 89 if ( fh==nil ) { 90 return; 91 } 92 int numRead=0; 93 int p1 = 0; 94 retData = [fh readDataToEndOfFile]; 95 numRead = [retData length]; 96#pragma mark fix these NSLog calls 97 NSLog( @"read %d chars; p was %d is now %d", n, p1, (p1+numRead) ); 98 p1 += numRead; 99 n = p1; 100 data = [[NSString alloc] initWithData:retData encoding:NSASCIIStringEncoding]; 101#pragma mark fix these NSLog calls 102 NSLog( @"n=%d", n ); 103 } 104 @finally { 105 [fh closeFile]; 106 } 107} 108 109@end 110