jens@6
|
1 |
//
|
jens@6
|
2 |
// MYOttoman.mm
|
jens@6
|
3 |
// Ottoman
|
jens@6
|
4 |
//
|
jens@6
|
5 |
// Created by Jens Alfke on 9/21/09.
|
jens@6
|
6 |
// Copyright 2009 Jens Alfke. All rights reserved.
|
jens@6
|
7 |
//
|
jens@6
|
8 |
|
jens@6
|
9 |
#import "MYOttoman.h"
|
jens@6
|
10 |
#import "MYOttoman_internal.h"
|
jens@6
|
11 |
extern "C" {
|
jens@6
|
12 |
#import "Test.h"
|
jens@6
|
13 |
}
|
jens@6
|
14 |
|
jens@6
|
15 |
#include "Ottoman.h"
|
jens@6
|
16 |
#include "VersionDictionary.h"
|
jens@6
|
17 |
#include "File.h"
|
jens@6
|
18 |
|
jens@6
|
19 |
|
jens@6
|
20 |
@interface MYOttoman ()
|
jens@6
|
21 |
- (void) _versionsChanged;
|
jens@6
|
22 |
@end
|
jens@6
|
23 |
|
jens@6
|
24 |
|
jens@6
|
25 |
namespace Mooseyard {
|
jens@6
|
26 |
|
jens@6
|
27 |
class ObjCOwnedOttoman :public Ottoman {
|
jens@6
|
28 |
public:
|
jens@6
|
29 |
ObjCOwnedOttoman (MYOttoman *owner)
|
jens@6
|
30 |
:_owner(owner)
|
jens@6
|
31 |
{ }
|
jens@6
|
32 |
|
jens@6
|
33 |
ObjCOwnedOttoman (MYOttoman *owner, NSURL *fileURL, bool writeable)
|
jens@6
|
34 |
:Ottoman(fileURL.path.fileSystemRepresentation, writeable),
|
jens@6
|
35 |
_owner(owner)
|
jens@6
|
36 |
{ }
|
jens@6
|
37 |
|
jens@6
|
38 |
protected:
|
jens@6
|
39 |
virtual void versionsChanged() {
|
jens@6
|
40 |
[_owner _versionsChanged];
|
jens@6
|
41 |
}
|
jens@6
|
42 |
private:
|
jens@6
|
43 |
MYOttoman *_owner;
|
jens@6
|
44 |
};
|
jens@6
|
45 |
|
jens@6
|
46 |
}
|
jens@6
|
47 |
|
jens@6
|
48 |
|
jens@6
|
49 |
using namespace Mooseyard;
|
jens@6
|
50 |
|
jens@6
|
51 |
|
jens@6
|
52 |
static BOOL ErrorToNSError (const File::Error &x, NSError **outError) {
|
jens@6
|
53 |
if (outError) {
|
jens@6
|
54 |
*outError = [NSError errorWithDomain: NSPOSIXErrorDomain
|
jens@6
|
55 |
code: x.code
|
jens@6
|
56 |
userInfo: nil];
|
jens@6
|
57 |
}
|
jens@6
|
58 |
return NO;
|
jens@6
|
59 |
}
|
jens@6
|
60 |
|
jens@6
|
61 |
|
jens@6
|
62 |
@interface MYOttoman ()
|
jens@6
|
63 |
@property (readonly) Ottoman* ottoman;
|
jens@6
|
64 |
@end
|
jens@6
|
65 |
|
jens@6
|
66 |
|
jens@6
|
67 |
@implementation MYOttoman
|
jens@6
|
68 |
|
jens@6
|
69 |
|
jens@6
|
70 |
- (id) init {
|
jens@6
|
71 |
return [self initWithURL: nil writeable: YES error: nil];
|
jens@6
|
72 |
}
|
jens@6
|
73 |
|
jens@6
|
74 |
- (id) initWithURL: (NSURL*)fileURL
|
jens@6
|
75 |
writeable: (BOOL)writeable
|
jens@6
|
76 |
error: (NSError**)outError
|
jens@6
|
77 |
{
|
jens@6
|
78 |
self = [super init];
|
jens@6
|
79 |
if (self) {
|
jens@6
|
80 |
try {
|
jens@6
|
81 |
if (fileURL) {
|
jens@6
|
82 |
NSAssert([fileURL isFileURL], @"MYOttoman only supports file: URLs");
|
jens@6
|
83 |
_ottoman = new ObjCOwnedOttoman(self, fileURL, writeable);
|
jens@6
|
84 |
} else {
|
jens@6
|
85 |
_ottoman = new ObjCOwnedOttoman(self);
|
jens@6
|
86 |
}
|
jens@6
|
87 |
} catch (const File::Error &x) {
|
jens@6
|
88 |
ErrorToNSError(x,outError);
|
jens@6
|
89 |
[self release];
|
jens@6
|
90 |
return nil;
|
jens@6
|
91 |
}
|
jens@6
|
92 |
|
jens@6
|
93 |
if (writeable)
|
jens@6
|
94 |
_currentVersion = [[MYCurrentVersionDictionary alloc]
|
jens@6
|
95 |
_initWithOverlayDictionary: self.ottoman->currentVersion()];
|
jens@6
|
96 |
}
|
jens@6
|
97 |
return self;
|
jens@6
|
98 |
}
|
jens@6
|
99 |
|
jens@6
|
100 |
- (void) dealloc
|
jens@6
|
101 |
{
|
jens@6
|
102 |
[_lastVersion release];
|
jens@6
|
103 |
[_currentVersion release];
|
jens@6
|
104 |
delete (Ottoman*)_ottoman;
|
jens@6
|
105 |
[super dealloc];
|
jens@6
|
106 |
}
|
jens@6
|
107 |
|
jens@6
|
108 |
- (void) finalize {
|
jens@6
|
109 |
delete (Ottoman*)_ottoman;
|
jens@6
|
110 |
[super finalize];
|
jens@6
|
111 |
}
|
jens@6
|
112 |
|
jens@6
|
113 |
- (void) close {
|
jens@6
|
114 |
delete (Ottoman*)_ottoman;
|
jens@6
|
115 |
_ottoman = nil;
|
jens@6
|
116 |
}
|
jens@6
|
117 |
|
jens@6
|
118 |
|
jens@6
|
119 |
- (Ottoman*) ottoman {
|
jens@6
|
120 |
Assert(_ottoman, @"MYOttoman has already been closed");
|
jens@6
|
121 |
return (Ottoman*) _ottoman;
|
jens@6
|
122 |
}
|
jens@6
|
123 |
|
jens@6
|
124 |
- (NSURL*) URL {
|
jens@6
|
125 |
const char *path = self.ottoman->filename();
|
jens@6
|
126 |
return path ?[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]] :nil;
|
jens@6
|
127 |
}
|
jens@6
|
128 |
|
jens@6
|
129 |
|
jens@6
|
130 |
- (MYVersionDictionary*) lastVersion {
|
jens@6
|
131 |
if (!_lastVersion)
|
jens@6
|
132 |
_lastVersion = [[MYVersionDictionary alloc] _initWithVersionDictionary: self.ottoman->lastVersion()];
|
jens@6
|
133 |
return _lastVersion;
|
jens@6
|
134 |
}
|
jens@6
|
135 |
|
jens@6
|
136 |
- (MYCurrentVersionDictionary*) currentVersion {
|
jens@6
|
137 |
return _currentVersion;
|
jens@6
|
138 |
}
|
jens@6
|
139 |
|
jens@6
|
140 |
- (void) _versionsChanged {
|
jens@6
|
141 |
[_lastVersion autorelease];
|
jens@6
|
142 |
_lastVersion = nil;
|
jens@6
|
143 |
}
|
jens@6
|
144 |
|
jens@6
|
145 |
|
jens@6
|
146 |
- (BOOL) needsSync {
|
jens@6
|
147 |
return self.ottoman->needsSync();
|
jens@6
|
148 |
}
|
jens@6
|
149 |
|
jens@6
|
150 |
|
jens@6
|
151 |
- (BOOL) sync: (NSError**)outError {
|
jens@6
|
152 |
if (outError) *outError = nil;
|
jens@6
|
153 |
try {
|
jens@6
|
154 |
return self.ottoman->sync();
|
jens@6
|
155 |
} catch (const File::Error &x) {
|
jens@6
|
156 |
return ErrorToNSError(x,outError);
|
jens@6
|
157 |
}
|
jens@6
|
158 |
}
|
jens@6
|
159 |
|
jens@6
|
160 |
- (BOOL) save: (NSError**)outError {
|
jens@6
|
161 |
if (outError) *outError = nil;
|
jens@6
|
162 |
try {
|
jens@6
|
163 |
return self.ottoman->save();
|
jens@6
|
164 |
} catch (const File::Error &x) {
|
jens@6
|
165 |
return ErrorToNSError(x,outError);
|
jens@6
|
166 |
}
|
jens@6
|
167 |
}
|
jens@6
|
168 |
|
jens@6
|
169 |
- (BOOL) saveAndCompact: (NSError**)outError {
|
jens@6
|
170 |
if (outError) *outError = nil;
|
jens@6
|
171 |
try {
|
jens@6
|
172 |
return self.ottoman->saveAndCompact();
|
jens@6
|
173 |
} catch (const File::Error &x) {
|
jens@6
|
174 |
return ErrorToNSError(x,outError);
|
jens@6
|
175 |
}
|
jens@6
|
176 |
}
|
jens@6
|
177 |
|
jens@6
|
178 |
- (BOOL) saveAs: (NSURL*)newFileURL
|
jens@6
|
179 |
overwriteAllowed: (BOOL)overwriteAllowed
|
jens@6
|
180 |
error: (NSError**)outError
|
jens@6
|
181 |
{
|
jens@6
|
182 |
NSParameterAssert(newFileURL!=nil);
|
jens@6
|
183 |
NSAssert([newFileURL isFileURL], @"MYOttoman only supports file: URLs");
|
jens@6
|
184 |
if (outError) *outError = nil;
|
jens@6
|
185 |
try {
|
jens@6
|
186 |
self.ottoman->saveAs(newFileURL.path.fileSystemRepresentation, overwriteAllowed);
|
jens@6
|
187 |
return YES;
|
jens@6
|
188 |
} catch (const File::Error &x) {
|
jens@6
|
189 |
return ErrorToNSError(x,outError);
|
jens@6
|
190 |
}
|
jens@6
|
191 |
}
|
jens@6
|
192 |
|
jens@6
|
193 |
|
jens@6
|
194 |
- (BOOL) scavengeAndRepair: (BOOL)repair error: (NSError**)outError {
|
jens@6
|
195 |
if (outError) *outError = nil;
|
jens@6
|
196 |
try {
|
jens@6
|
197 |
return self.ottoman->scavenge(repair);
|
jens@6
|
198 |
} catch (const File::Error &x) {
|
jens@6
|
199 |
return ErrorToNSError(x,outError);
|
jens@6
|
200 |
}
|
jens@6
|
201 |
}
|
jens@6
|
202 |
|
jens@6
|
203 |
|
jens@6
|
204 |
@end
|
jens@6
|
205 |
|