1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
Copyright (c) 1998-2005 Benhur Stein
This file is part of Paj.
Paj is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
Paj 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 Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License
along with Paj; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include "PajeFileReader.h"
#include "../General/FoundationAdditions.h"
#include "../General/UniqueString.h"
#include "../General/Macros.h"
#define LINE_SIZE 512
@implementation FileReader
- (id)initWithController:(PajeTraceController *)c
{
self = [super initWithController:c];
if (self != nil) {
chunkInfo = [[NSMutableArray alloc] init];
currentChunk = 0;
hasMoreData = NO;
userChunkSize = CHUNK_SIZE;
}
return self;
}
- (void)dealloc
{
[inputFilename release];
[inputFile release];
[chunkInfo release];
[super dealloc];
}
- (NSString *)traceDescription
{
return [inputFilename stringByAbbreviatingWithTildeInPath];
}
// A new chunk will start.
// Position the file in the good position, if not yet there.
- (void)startChunk:(int)chunkNumber
{
if (chunkNumber != currentChunk) {
if (chunkNumber >= [chunkInfo count]) {
// cannot position in an unread place
NSLog(@"Chunk after end: %d (%lu)", chunkNumber, [chunkInfo count]);
[self raise:@"Cannot start unknown chunk"];
}
unsigned long /*long*/ position;
position = [[chunkInfo objectAtIndex:chunkNumber] longLongValue];
if (1||[inputFile seekToEndOfFile] > position) {
[inputFile seekToFileOffset:position];
hasMoreData = YES;
} else {
hasMoreData = NO;
}
currentChunk = chunkNumber;
} else {
// let's register the first chunk position
if ([chunkInfo count] == 0) {
unsigned long /*long*/ position;
position = [inputFile offsetInFile];
[chunkInfo addObject:[NSNumber numberWithLongLong:position]];
}
}
// keep the ball rolling (tell other components)
[super startChunk:chunkNumber];
}
// The current chunk has ended.
- (void)endOfChunkLast:(BOOL)last
{
currentChunk++;
if (!last) {
// if we're at the end of the known world, let's register its position
if (currentChunk == [chunkInfo count]) {
unsigned long /*long*/ position;
position = [inputFile offsetInFile];
[chunkInfo addObject:[NSNumber numberWithLongLong:position]];
}
}
[super endOfChunkLast:last];
}
- (void)raise:(NSString *)reason
{
NSDebugLog(@"PajeFileReader: '%@' in file '%@', bytes read %lld",
reason, inputFilename, [inputFile offsetInFile]);
[[NSException exceptionWithName:@"PajeReadFileException"
reason:reason
userInfo:
[NSDictionary dictionaryWithObjectsAndKeys:
inputFilename, @"File Name",
[NSNumber numberWithUnsignedLongLong:[inputFile offsetInFile]],
@"BytesRead",
nil]
] raise];
}
- (NSString *)inputFilename
{
return inputFilename;
}
- (NSNumber *) readingPorcentage
{
double size = (double)sizeOfFile;
double current = (double)[inputFile offsetInFile];
double porcentage = current/size;
return [NSNumber numberWithDouble: porcentage];
}
- (void)setInputFilename:(NSString *)filename
{
if (inputFilename != nil) {
[self raise:@"Already has an open file"];
}
Assign(inputFilename, filename);
Assign(inputFile, [NSFileHandle fileHandleForReadingAtPath:filename]);
//defining the size of the file
sizeOfFile = [inputFile seekToEndOfFile];
[inputFile seekToFileOffset: 0];
if (inputFile == nil) {
[self raise:@"Couldn't open file"];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"UseCompression"]) {
#ifdef GNUSTEP
[inputFile useCompression];
#endif
}
hasMoreData = YES;
}
- (void)inputEntity:(id)entity
{
[self raise:@"Configuration error:" " PajeFileReader should be first component"];
}
- (BOOL)canEndChunk
{
NSMutableData *data;
unsigned int length;
unsigned long /*long*/ offsetInFile;
if (![self hasMoreData]) {
return YES;
}
offsetInFile = [inputFile offsetInFile];
// need to create a NSMutableData from a NSData
NSData *dataRead = [inputFile readDataOfLength:LINE_SIZE];
data = [NSMutableData dataWithData: dataRead];
length = [data length];
if (length < LINE_SIZE) {
hasMoreData = NO;
}
const char *bytes;
char *eol;
bytes = [data bytes];
eol = memchr(bytes, '\n', length);
if (eol != NULL) {
unsigned int newlength;
newlength = eol - bytes + 1;
if (newlength != length) {
length = newlength;
hasMoreData = YES;
[inputFile seekToFileOffset:offsetInFile + length];
[data setLength:length];
}
}
NSDebugMLLog(@"tim", @"data: %@\nchunk length: %u has more:%d",
[data class], [data length], hasMoreData);
if (length > 0) {
if ([super canEndChunkBefore:data]) {
[inputFile seekToFileOffset:offsetInFile];
return YES;
} else {
[inputFile seekToFileOffset:offsetInFile + length];
return NO;
}
} else {
return YES;
}
}
- (void)readNextChunk
{
NSMutableData *data;
unsigned int length;
if (![self hasMoreData]) {
return;
}
int nextChunk = currentChunk +1;
if (nextChunk < [chunkInfo count]) {
// this chunk has already been read, we should know its size
unsigned long /*long*/ nextChunkPosition;
nextChunkPosition = [[chunkInfo objectAtIndex:nextChunk] longLongValue];
unsigned long /*long*/ chunkSize;
chunkSize = nextChunkPosition - [inputFile offsetInFile];
// need to create a NSMutableData from a NSData
NSData *dataRead = [inputFile readDataOfLength:chunkSize];
data = [NSMutableData dataWithData: dataRead];
length = [data length];
if (length != chunkSize) {
[self raise:@"Cannot reread chunk! Has file been altered?"];
}
[self outputEntity:data];
} else {
// first time reading this chunk.
// must determine its correct size (must end in a line boundary
// and in a date-changing event).
// need to create a NSMutableData from a NSData
NSData *dataRead = [inputFile readDataOfLength:userChunkSize];
data = [NSMutableData dataWithData: dataRead];
length = [data length];
if (length < userChunkSize) {
hasMoreData = NO;
} else {
char *bytes;
int i;
int offset = 0;
bytes = (char *)[data bytes];
for (i = length-1; i >= 0 && bytes[i] != '\n'; i--) {
offset++;
}
if ((i >= 0) && (offset > 0)) {
[inputFile seekToFileOffset:[inputFile offsetInFile] - offset];
length -= offset;
[data setLength:length];
}
}
NSDebugMLLog(@"tim", @"data: %@\nchunk length: %u has more:%d",
[data class], [data length], hasMoreData);
if (length > 0) {
[self outputEntity:data];
while (![self canEndChunk]) {
;
}
}
}
}
- (BOOL)hasMoreData
{
return hasMoreData;
}
- (void) setUserChunkSize: (unsigned long long)cs
{
userChunkSize = cs;
}
@end
|