Re: reading in text files
Re: reading in text files
- Subject: Re: reading in text files
- From: Bob Savage <email@hidden>
- Date: Sat, 02 Feb 2002 16:44:33 -0600
on 2/2/02 9:00 AM, Ondra Cada wrote:
>
-(NSData*)readDataOfLength:(unsigned)len orUpToDelimiterFrom:(NSData*)delims
>
delimiterFound:(int*)indexInDelimsOrNegative;
Okay, Ondra, hesitations aside, *if* someone wanted to implement something
like this, as an addition to what Apple provides (so the purists would be
unsullied ;), would this be a reasonable approach:
Note: don't expect this to work, I'm just typing this into the email - bob
/************************************************************************/
//
// in NSFileHandle <delimiting>
//
-(NSData*)readDataOfLength:(unsigned)len
orUpToDelimiterFrom:(NSData*)delims
delimiterFound:(int*)indexInDelimsOrNegative // why not a BOOL?
/* "Read only up to delimiter, or length if delimiter not found" */
{
BOOL delimFOUND = NO;
unsigned long long oldOffset = [self offsetInFile];
int minicursor = 0; // used in seaching fileD for delims
NSData *fileD = [self readDataOfLength:len];
NSData *tempD, *retD
// check that anything was there
if (!fileD) {
indexInDelimsOrNegative = -1;
return fileD;
}
// scan fileD for delims
// - Ondra, was delims supposed to be a single lump of data
// to look for, or is it a series of delimiters? I'm assuming
// a single lump below.
//
while ( (!delimFOUND)
&& (minicursor < ([fileD length] - [delims length])) )
{
tempD = [fileD subdataWithRange:(minicursor, [delims length])];
delimFOUND = [tempD isEqualToData: delims];
minicursor++; // please tell me we can do it faster than this!
}
if (delimFOUND) {
// if we found delims -
// create a new NSData object from the beginning up to that point
retD = [fileD subdataWithRange:(0, minicursor - 1];
// seek to location after there
[self seekToFileOffset:oldOffset
+ [retD length]
+ [delims length] ];
indexInDelimsOrNegative = +1;
}
else {
retD = tempD; // send the whole thing
indexInDelimsOrNegative = -1;
}
return retD;
}
/************************************************************************/