|
************************** SDStoreFile.h *******************************
@class SDStoreDirectory;
@interface SDStoreFile:SDStore
{
BOOL allowWriting;
NSString *filename;
NSString *pplFileName;
NSPPL *ppl;
}
- (id) initWithFile: (NSString *)aFilename;
- (id) initFromFile: (NSString *)aFilename forWriting: (BOOL) writeFlag;
- (void) createBlock:(int *)blockNumber withSize:(unsigned int)aSize;
- (NSString *)filename;
@end
*************************** SDStoreFile.m *******************************
///////////////////////////////////////////////////////////////////////////
//
// SDStoreFile
// kaj: Jun 4 1997
// Purpose: cover for IXStoreFile methods
// Implemented using an NSPPL (Persistent Property List)
//
///////////////////////////////////////////////////////////////////////////
#import "ixCover.h"
@implementation SDStoreFile:SDStore
#define PPL_FILE_NAME @"Store.ppl"
#define PRIVATE_INFO @"privateInfo"
///////////////////////////////////////////////////////////////////////////
//
// initWithFile:
//
// Initializes the IXStoreFile with filename as its storage file.
// filename is created and opened for reading and writing,
// and locked for exclusive access. This the designated initializer
// for the IXStoreFile class. Returns self.
//
///////////////////////////////////////////////////////////////////////////
- (id) initWithFile: (NSString *)aFilename
{
[super init];
if (!aFilename) {
[self release];
return nil;
}
filename = [[NSString allocWithZone:[self zone]] initWithString:aFilename];
// check to make sure directory doesn't exist
if ([[NSFileManager defaultManager] fileExistsAtPath:filename]) {
// should put up a warning panel that the file exists
[self release];
return nil;
} else {
// create directory
[[NSFileManager defaultManager] createDirectoryAtPath:filename attributes: nil];
// create a ppl in directory
ppl = [[NSPPL allocWithZone:[self zone]] initWithPath:[filename stringByAppendingPathComponent:PPL_FILE_NAME] create:YES readOnly:NO];
rootDictionary = [[ppl rootDictionary]retain];
[rootDictionary setObject:privateInfo forKey:PRIVATE_INFO];
allowWriting = YES;
transactionLevel = 0;
}
return self;
}
- (id) initFromFile: (NSString *)aFilename forWriting: (BOOL) writeFlag
{
BOOL isDirectory;
[super init];
if (!aFilename) {
[self release];
return nil;
}
filename = [[NSString allocWithZone:[self zone]] initWithString:aFilename];
// check to make sure directory exists
if (![[NSFileManager defaultManager] fileExistsAtPath:filename isDirectory:&isDirectory] || !isDirectory) {
// raise an exception
[self release];
return nil;
}
NS_DURING
// read the ppl
pplFileName = [filename stringByAppendingPathComponent:PPL_FILE_NAME];
// NOTE: I was unable to get a root dictionary from a PPL that was opened as readOnly. I don't know why. For right now, we'll just open everything has writeable; later, we may want to investigate further
// A possible solution is to create a copy PPL and use that; no changes will be reflected back in the original PPL
ppl = [[NSPPL allocWithZone:[self zone]] initWithPath:pplFileName create:NO readOnly:NO];
if (!ppl) {
[self release];
return nil;
}
// read the rootDictionary
rootDictionary = [[ppl rootDictionary] retain];
NS_HANDLER
// later, we may want to try to recover the PPL from the userInfo dictionary
[localException raise];
NS_ENDHANDLER
[pplFileName retain];
privateInfo = [[rootDictionary objectForKey:PRIVATE_INFO]retain];
// get the last block number
lastBlock = [self readLastBlockNumber];
allowWriting = writeFlag;
transactionLevel = 0;
return self;
}
- (void) createBlock:(int *)blockNumber withSize:(unsigned int)aSize
{
*blockNumber = ++lastBlock;
[self recordLastBlockNumber];
}
- (void) commitTransaction
{
[super commitTransaction];
[ppl save];
}
- (void) dealloc
{
[rootDictionary release];
[ppl release];
[pplFileName release];
[filename release];
[super dealloc];
}
- (NSString *)filename
{
return filename;
}
@end
|
|