This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCLibrary.m
190 lines (155 loc) · 3.49 KB
/
CCLibrary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// CCLibrary.m
// Violet
//
//
#import "CCLibrary.h"
static NSString * const LibraryFolderName = @"Violet.violetlib";
static NSString * const LibraryExtension = @"violetlib";
@implementation CCLibrary
+ (NSString *)libraryExtension
{
return LibraryExtension;
}
+ (NSString *)homeDirectory
{
return NSHomeDirectory();
}
+ (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *result = nil;
if ([paths count] < 1)
{
NSLog(@"[Error] Documents folder missing");
result = NSHomeDirectory();
}
else if([paths count] > 1)
{
NSLog(@"[Error] Multiple Documents folders returned: %@", paths);
result = NSHomeDirectory();
}
else
{
result = [paths objectAtIndex:0];
}
return result;
}
- (id) init
{
self = [super init];
if (self != nil)
{
_path = nil;
_libraryIsOpen = NO;
}
return self;
}
- (void) dealloc
{
[_path release];
[super dealloc];
}
- (NSString *)path
{
NSString *result = nil;
if (_libraryIsOpen)
{
result = _path;
}
return result;
}
- (BOOL)openLibraryAtPath:(NSString *)path createIfMissing:(BOOL)create
{
BOOL result = NO;
NSString *libraryFolder = nil;
if(!path)
{
libraryFolder = [[CCLibrary documentsDirectory] stringByAppendingPathComponent:LibraryFolderName];
BOOL isDirectory = NO;
if([[NSFileManager defaultManager] fileExistsAtPath:libraryFolder isDirectory:&isDirectory])
{
if(isDirectory)
{
NSLog(@"[Info] Library folder exists at path: %@", libraryFolder);
result = YES;
}
else
{
NSLog(@"[Error] Default documents directory appear to be a file");
}
}
else if(create)
{
NSError *error = nil;
BOOL created = [[NSFileManager defaultManager] createDirectoryAtPath:libraryFolder
withIntermediateDirectories:YES
attributes:nil
error:&error];
if(!created)
{
NSLog(@"[Error] Unable to create documents directory at path %@ -- %@", libraryFolder, error);
}
else
{
NSLog(@"[Info] Created library folder at path: %@", libraryFolder);
result = YES;
}
}
}
else
{
BOOL isDirectory = NO;
if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory])
{
if(isDirectory)
{
libraryFolder = path;
result = YES;
}
}
else if(create)
{
NSError *error = nil;
BOOL created = [[NSFileManager defaultManager] createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:nil
error:&error];
if(!created)
{
NSLog(@"[Error] Unable to create documents directory at path %@ -- %@", libraryFolder, error);
}
else
{
NSLog(@"[Info] Created library folder at path: %@", libraryFolder);
libraryFolder = path;
result = YES;
}
}
}
if(result)
{
NSLog(@"[Info] Opening library at path: %@", libraryFolder);
_path = [libraryFolder retain];
_libraryIsOpen = YES;
}
return result;
}
- (NSString *)addFileToLibrary:(NSString *)path
{
NSString *result = nil;
if(_libraryIsOpen)
{
NSString *destination = [_path stringByAppendingPathComponent:[path lastPathComponent]];
NSError *error = nil;
BOOL copied = [[NSFileManager defaultManager] copyItemAtPath:path toPath:destination error:&error];
if(!copied)
{
NSLog(@"[Error] Adding file from path '%@' to path '%@' failed: %@", path, destination, error);
destination = nil;
}
result = destination;
}
return result;
}
@end