Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -25,10 +25,11 @@ * The OFFile class provides functions to read, write and manipulate files. */ @interface OFFile: OFStream { FILE *fp; + BOOL close; } /** * \param path The path to the file to open as a string * \param mode The mode in which the file should be opened as a string @@ -35,10 +36,17 @@ * \return A new autoreleased OFFile */ + fileWithPath: (OFString*)path mode: (OFString*)mode; +/** + * \param fp A file pointer, returned from for example fopen(). + * It is not closed when the OFFile object is deallocated! + * \return A new autoreleased OFFile + */ ++ fileWithFilePointer: (FILE*)fp; + /** * \return An OFFile singleton for stdin */ + standardInput; @@ -120,10 +128,18 @@ * \return An initialized OFFile */ - initWithPath: (OFString*)path mode: (OFString*)mode; +/** + * Initializes an already allocated OFFile. + * + * \param fp A file pointer, returned from for example fopen(). + * It is not closed when the OFFile object is deallocated! + */ +- initWithFilePointer: (FILE*)fp; + /** * Reads from the file into a buffer. * * \param buf The buffer into which the data is read * \param size The size of the data that should be read. @@ -148,7 +164,6 @@ ofSize: (size_t)size fromBuffer: (const char*)buf; @end @interface OFFileSingleton: OFFile -- initWithFilePointer: (FILE*)fp; @end Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -32,10 +32,15 @@ mode: (OFString*)mode { return [[[self alloc] initWithPath: path mode: mode] autorelease]; } + ++ fileWithFilePointer: (FILE*)fp_ +{ + return [[[self alloc] initWithFilePointer: fp_] autorelease]; +} + standardInput { if (of_file_stdin == nil) of_file_stdin = [[OFFileSingleton alloc] @@ -139,17 +144,28 @@ [super dealloc]; @throw [OFOpenFileFailedException newWithClass: c path: path mode: mode]; } + + close = YES; + + return self; +} + +- initWithFilePointer: (FILE*)fp_ +{ + self = [super init]; + + fp = fp_; return self; } - (void)dealloc { - if (fp != NULL) + if (close == YES && fp != NULL) fclose(fp); [super dealloc]; } @@ -224,19 +240,10 @@ return self; } @end @implementation OFFileSingleton -- initWithFilePointer: (FILE*)fp_ -{ - self = [super init]; - - fp = fp_; - - return self; -} - - initWithPath: (OFString*)path mode: (OFString*)mode { @throw [OFNotImplementedException newWithClass: isa selector: _cmd];