Initial Cocoa (Objective-C) API.
5 // Created by Jens Alfke on 9/21/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
10 #import "MYOttoman_internal.h"
16 #include "VersionDictionary.h"
20 @interface MYOttoman ()
21 - (void) _versionsChanged;
27 class ObjCOwnedOttoman :public Ottoman {
29 ObjCOwnedOttoman (MYOttoman *owner)
33 ObjCOwnedOttoman (MYOttoman *owner, NSURL *fileURL, bool writeable)
34 :Ottoman(fileURL.path.fileSystemRepresentation, writeable),
39 virtual void versionsChanged() {
40 [_owner _versionsChanged];
49 using namespace Mooseyard;
52 static BOOL ErrorToNSError (const File::Error &x, NSError **outError) {
54 *outError = [NSError errorWithDomain: NSPOSIXErrorDomain
62 @interface MYOttoman ()
63 @property (readonly) Ottoman* ottoman;
67 @implementation MYOttoman
71 return [self initWithURL: nil writeable: YES error: nil];
74 - (id) initWithURL: (NSURL*)fileURL
75 writeable: (BOOL)writeable
76 error: (NSError**)outError
82 NSAssert([fileURL isFileURL], @"MYOttoman only supports file: URLs");
83 _ottoman = new ObjCOwnedOttoman(self, fileURL, writeable);
85 _ottoman = new ObjCOwnedOttoman(self);
87 } catch (const File::Error &x) {
88 ErrorToNSError(x,outError);
94 _currentVersion = [[MYCurrentVersionDictionary alloc]
95 _initWithOverlayDictionary: self.ottoman->currentVersion()];
102 [_lastVersion release];
103 [_currentVersion release];
104 delete (Ottoman*)_ottoman;
109 delete (Ottoman*)_ottoman;
114 delete (Ottoman*)_ottoman;
119 - (Ottoman*) ottoman {
120 Assert(_ottoman, @"MYOttoman has already been closed");
121 return (Ottoman*) _ottoman;
125 const char *path = self.ottoman->filename();
126 return path ?[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]] :nil;
130 - (MYVersionDictionary*) lastVersion {
132 _lastVersion = [[MYVersionDictionary alloc] _initWithVersionDictionary: self.ottoman->lastVersion()];
136 - (MYCurrentVersionDictionary*) currentVersion {
137 return _currentVersion;
140 - (void) _versionsChanged {
141 [_lastVersion autorelease];
147 return self.ottoman->needsSync();
151 - (BOOL) sync: (NSError**)outError {
152 if (outError) *outError = nil;
154 return self.ottoman->sync();
155 } catch (const File::Error &x) {
156 return ErrorToNSError(x,outError);
160 - (BOOL) save: (NSError**)outError {
161 if (outError) *outError = nil;
163 return self.ottoman->save();
164 } catch (const File::Error &x) {
165 return ErrorToNSError(x,outError);
169 - (BOOL) saveAndCompact: (NSError**)outError {
170 if (outError) *outError = nil;
172 return self.ottoman->saveAndCompact();
173 } catch (const File::Error &x) {
174 return ErrorToNSError(x,outError);
178 - (BOOL) saveAs: (NSURL*)newFileURL
179 overwriteAllowed: (BOOL)overwriteAllowed
180 error: (NSError**)outError
182 NSParameterAssert(newFileURL!=nil);
183 NSAssert([newFileURL isFileURL], @"MYOttoman only supports file: URLs");
184 if (outError) *outError = nil;
186 self.ottoman->saveAs(newFileURL.path.fileSystemRepresentation, overwriteAllowed);
188 } catch (const File::Error &x) {
189 return ErrorToNSError(x,outError);
194 - (BOOL) scavengeAndRepair: (BOOL)repair error: (NSError**)outError {
195 if (outError) *outError = nil;
197 return self.ottoman->scavenge(repair);
198 } catch (const File::Error &x) {
199 return ErrorToNSError(x,outError);