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
|
#import "CSFileHandle.h"
#include <sys/stat.h>
NSString *CSCannotOpenFileException=@"CSCannotOpenFileException";
NSString *CSFileErrorException=@"CSFileErrorException";
@implementation CSFileHandle
+(CSFileHandle *)fileHandleForReadingAtPath:(NSString *)path
{ return [self fileHandleForPath:path modes:@"rb"]; }
+(CSFileHandle *)fileHandleForWritingAtPath:(NSString *)path
{ return [self fileHandleForPath:path modes:@"wb"]; }
+(CSFileHandle *)fileHandleForPath:(NSString *)path modes:(NSString *)modes
{
if(!path) return nil;
#if defined(__COCOTRON__) // Cocotron
FILE *fileh=_wfopen([path fileSystemRepresentationW],
(const wchar_t *)[modes cStringUsingEncoding:NSUnicodeStringEncoding]);
#elif defined(__MINGW32__) // GNUstep under mingw32 - sort of untested
FILE *fileh=_wfopen((const wchar_t *)[path fileSystemRepresentation],
(const wchar_t *)[modes cStringUsingEncoding:NSUnicodeStringEncoding]);
#else // Cocoa or GNUstep under Linux
FILE *fileh=fopen([path fileSystemRepresentation],[modes UTF8String]);
#endif
if(!fileh) [NSException raise:CSCannotOpenFileException
format:@"Error attempting to open file \"%@\" in mode \"%@\".",path,modes];
CSFileHandle *handle=[[[CSFileHandle alloc] initWithFilePointer:fileh closeOnDealloc:YES name:path] autorelease];
if(handle) return handle;
fclose(fileh);
return nil;
}
-(id)initWithFilePointer:(FILE *)file closeOnDealloc:(BOOL)closeondealloc name:(NSString *)descname
{
if((self=[super initWithName:descname]))
{
fh=file;
close=closeondealloc;
multilock=nil;
parent=nil;
}
return self;
}
-(id)initAsCopyOf:(CSFileHandle *)other
{
if((self=[super initAsCopyOf:other]))
{
fh=other->fh;
close=NO;
parent=[other retain];
if(!other->multilock) [other _setMultiMode];
multilock=[other->multilock retain];
[multilock lock];
pos=other->pos;
[multilock unlock];
}
return self;
}
-(void)dealloc
{
if(fh&&close) fclose(fh);
[parent release];
[multilock release];
[super dealloc];
}
-(void)close
{
if(fh&&close) fclose(fh);
fh=NULL;
}
-(FILE *)filePointer { return fh; }
-(off_t)fileSize
{
#if defined(__MINGW32__)
struct _stati64 s;
if(_fstati64(fileno(fh),&s)) [self _raiseError];
#else
struct stat s;
if(fstat(fileno(fh),&s)) [self _raiseError];
#endif
return s.st_size;
}
-(off_t)offsetInFile
{
if(multilock) return pos;
else return ftello(fh);
}
-(BOOL)atEndOfFile
{
return [self offsetInFile]==[self fileSize];
/* if(multi) return pos==[self fileSize];
else return feof(fh);*/ // feof() only returns true after trying to read past the end
}
-(void)seekToFileOffset:(off_t)offs
{
if(multilock) { [multilock lock]; }
//if(offs>[self fileSize]) [self _raiseEOF];
if(fseeko(fh,offs,SEEK_SET)) [self _raiseError];
if(multilock) { pos=ftello(fh); [multilock unlock]; }
}
-(void)seekToEndOfFile
{
if(multilock) { [multilock lock]; }
if(fseeko(fh,0,SEEK_END)) [self _raiseError];
if(multilock) { pos=ftello(fh); [multilock unlock]; }
}
-(void)pushBackByte:(int)byte
{
if(multilock) [self _raiseNotSupported:_cmd];
if(ungetc(byte,fh)==EOF) [self _raiseError];
}
-(int)readAtMost:(int)num toBuffer:(void *)buffer
{
if(num==0) return 0;
if(multilock) { [multilock lock]; fseeko(fh,pos,SEEK_SET); }
int n=fread(buffer,1,num,fh);
if(n<=0&&!feof(fh)) [self _raiseError];
if(multilock) { pos=ftello(fh); [multilock unlock]; }
return n;
}
-(void)writeBytes:(int)num fromBuffer:(const void *)buffer
{
if(multilock) { [multilock lock]; fseeko(fh,pos,SEEK_SET); }
if(fwrite(buffer,1,num,fh)!=num) [self _raiseError];
if(multilock) { pos=ftello(fh); [multilock unlock]; }
}
-(void)_raiseError
{
if(feof(fh)) [self _raiseEOF];
else [[[[NSException alloc] initWithName:CSFileErrorException
reason:[NSString stringWithFormat:@"Error while attempting to read file \"%@\": %s.",name,strerror(errno)]
userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:errno] forKey:@"ErrNo"]] autorelease] raise];
}
-(void)_setMultiMode
{
if(!multilock)
{
multilock=[NSLock new];
pos=ftello(fh);
}
}
@end
|