1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/bindings/Cocoa/MYOttoman.mm Thu Sep 24 21:47:06 2009 -0700
1.3 @@ -0,0 +1,205 @@
1.4 +//
1.5 +// MYOttoman.mm
1.6 +// Ottoman
1.7 +//
1.8 +// Created by Jens Alfke on 9/21/09.
1.9 +// Copyright 2009 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "MYOttoman.h"
1.13 +#import "MYOttoman_internal.h"
1.14 +extern "C" {
1.15 +#import "Test.h"
1.16 +}
1.17 +
1.18 +#include "Ottoman.h"
1.19 +#include "VersionDictionary.h"
1.20 +#include "File.h"
1.21 +
1.22 +
1.23 +@interface MYOttoman ()
1.24 +- (void) _versionsChanged;
1.25 +@end
1.26 +
1.27 +
1.28 +namespace Mooseyard {
1.29 +
1.30 + class ObjCOwnedOttoman :public Ottoman {
1.31 + public:
1.32 + ObjCOwnedOttoman (MYOttoman *owner)
1.33 + :_owner(owner)
1.34 + { }
1.35 +
1.36 + ObjCOwnedOttoman (MYOttoman *owner, NSURL *fileURL, bool writeable)
1.37 + :Ottoman(fileURL.path.fileSystemRepresentation, writeable),
1.38 + _owner(owner)
1.39 + { }
1.40 +
1.41 + protected:
1.42 + virtual void versionsChanged() {
1.43 + [_owner _versionsChanged];
1.44 + }
1.45 + private:
1.46 + MYOttoman *_owner;
1.47 + };
1.48 +
1.49 +}
1.50 +
1.51 +
1.52 +using namespace Mooseyard;
1.53 +
1.54 +
1.55 +static BOOL ErrorToNSError (const File::Error &x, NSError **outError) {
1.56 + if (outError) {
1.57 + *outError = [NSError errorWithDomain: NSPOSIXErrorDomain
1.58 + code: x.code
1.59 + userInfo: nil];
1.60 + }
1.61 + return NO;
1.62 +}
1.63 +
1.64 +
1.65 +@interface MYOttoman ()
1.66 +@property (readonly) Ottoman* ottoman;
1.67 +@end
1.68 +
1.69 +
1.70 +@implementation MYOttoman
1.71 +
1.72 +
1.73 +- (id) init {
1.74 + return [self initWithURL: nil writeable: YES error: nil];
1.75 +}
1.76 +
1.77 +- (id) initWithURL: (NSURL*)fileURL
1.78 + writeable: (BOOL)writeable
1.79 + error: (NSError**)outError
1.80 +{
1.81 + self = [super init];
1.82 + if (self) {
1.83 + try {
1.84 + if (fileURL) {
1.85 + NSAssert([fileURL isFileURL], @"MYOttoman only supports file: URLs");
1.86 + _ottoman = new ObjCOwnedOttoman(self, fileURL, writeable);
1.87 + } else {
1.88 + _ottoman = new ObjCOwnedOttoman(self);
1.89 + }
1.90 + } catch (const File::Error &x) {
1.91 + ErrorToNSError(x,outError);
1.92 + [self release];
1.93 + return nil;
1.94 + }
1.95 +
1.96 + if (writeable)
1.97 + _currentVersion = [[MYCurrentVersionDictionary alloc]
1.98 + _initWithOverlayDictionary: self.ottoman->currentVersion()];
1.99 + }
1.100 + return self;
1.101 +}
1.102 +
1.103 +- (void) dealloc
1.104 +{
1.105 + [_lastVersion release];
1.106 + [_currentVersion release];
1.107 + delete (Ottoman*)_ottoman;
1.108 + [super dealloc];
1.109 +}
1.110 +
1.111 +- (void) finalize {
1.112 + delete (Ottoman*)_ottoman;
1.113 + [super finalize];
1.114 +}
1.115 +
1.116 +- (void) close {
1.117 + delete (Ottoman*)_ottoman;
1.118 + _ottoman = nil;
1.119 +}
1.120 +
1.121 +
1.122 +- (Ottoman*) ottoman {
1.123 + Assert(_ottoman, @"MYOttoman has already been closed");
1.124 + return (Ottoman*) _ottoman;
1.125 +}
1.126 +
1.127 +- (NSURL*) URL {
1.128 + const char *path = self.ottoman->filename();
1.129 + return path ?[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]] :nil;
1.130 +}
1.131 +
1.132 +
1.133 +- (MYVersionDictionary*) lastVersion {
1.134 + if (!_lastVersion)
1.135 + _lastVersion = [[MYVersionDictionary alloc] _initWithVersionDictionary: self.ottoman->lastVersion()];
1.136 + return _lastVersion;
1.137 +}
1.138 +
1.139 +- (MYCurrentVersionDictionary*) currentVersion {
1.140 + return _currentVersion;
1.141 +}
1.142 +
1.143 +- (void) _versionsChanged {
1.144 + [_lastVersion autorelease];
1.145 + _lastVersion = nil;
1.146 +}
1.147 +
1.148 +
1.149 +- (BOOL) needsSync {
1.150 + return self.ottoman->needsSync();
1.151 +}
1.152 +
1.153 +
1.154 +- (BOOL) sync: (NSError**)outError {
1.155 + if (outError) *outError = nil;
1.156 + try {
1.157 + return self.ottoman->sync();
1.158 + } catch (const File::Error &x) {
1.159 + return ErrorToNSError(x,outError);
1.160 + }
1.161 +}
1.162 +
1.163 +- (BOOL) save: (NSError**)outError {
1.164 + if (outError) *outError = nil;
1.165 + try {
1.166 + return self.ottoman->save();
1.167 + } catch (const File::Error &x) {
1.168 + return ErrorToNSError(x,outError);
1.169 + }
1.170 +}
1.171 +
1.172 +- (BOOL) saveAndCompact: (NSError**)outError {
1.173 + if (outError) *outError = nil;
1.174 + try {
1.175 + return self.ottoman->saveAndCompact();
1.176 + } catch (const File::Error &x) {
1.177 + return ErrorToNSError(x,outError);
1.178 + }
1.179 +}
1.180 +
1.181 +- (BOOL) saveAs: (NSURL*)newFileURL
1.182 + overwriteAllowed: (BOOL)overwriteAllowed
1.183 + error: (NSError**)outError
1.184 +{
1.185 + NSParameterAssert(newFileURL!=nil);
1.186 + NSAssert([newFileURL isFileURL], @"MYOttoman only supports file: URLs");
1.187 + if (outError) *outError = nil;
1.188 + try {
1.189 + self.ottoman->saveAs(newFileURL.path.fileSystemRepresentation, overwriteAllowed);
1.190 + return YES;
1.191 + } catch (const File::Error &x) {
1.192 + return ErrorToNSError(x,outError);
1.193 + }
1.194 +}
1.195 +
1.196 +
1.197 +- (BOOL) scavengeAndRepair: (BOOL)repair error: (NSError**)outError {
1.198 + if (outError) *outError = nil;
1.199 + try {
1.200 + return self.ottoman->scavenge(repair);
1.201 + } catch (const File::Error &x) {
1.202 + return ErrorToNSError(x,outError);
1.203 + }
1.204 +}
1.205 +
1.206 +
1.207 +@end
1.208 +