Skip to content

Commit 79f626a

Browse files
committed
• SSYLinearFileWriter now works with ARC (or not).
• SSYLinearFileWriter now creates parent directory if necessary to write its file at specified path.
1 parent 5f51dc9 commit 79f626a

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

SSYLinearFileWriter.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/*!
1010
@brief Closes the file of any existing singleton, and
1111
creates a new one set to write to a given path
12+
13+
@details Creates parent directory path if necessary, deleting any regular
14+
file which may exist at the parent directory path
1215
*/
1316
+ (void)setToPath:(NSString*)path ;
1417

SSYLinearFileWriter.m

+30-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,30 @@ + (SSYLinearFileWriter*)sharedFileWriter {
3333
}
3434

3535
- (void)setPath:(NSString*)path {
36-
NSFileManager* fileManager = [NSFileManager defaultManager] ;
37-
[fileManager createFileAtPath:path
36+
NSFileManager* fileManager = [NSFileManager defaultManager];
37+
38+
NSString* parentPath = [path stringByDeletingLastPathComponent];
39+
BOOL parentDirectoryIsDirectory = NO;
40+
BOOL parentDirectoryExists = [fileManager fileExistsAtPath:parentPath
41+
isDirectory:&parentDirectoryIsDirectory];
42+
BOOL needsCreateDirectory = NO;
43+
if (parentDirectoryExists) {
44+
if (!parentDirectoryIsDirectory) {
45+
[fileManager removeItemAtPath:parentPath
46+
error:NULL];
47+
needsCreateDirectory = YES;
48+
}
49+
} else {
50+
needsCreateDirectory = YES;
51+
}
52+
if (needsCreateDirectory) {
53+
[fileManager createDirectoryAtPath:parentPath
54+
withIntermediateDirectories:YES
55+
attributes:nil
56+
error:NULL];
57+
}
58+
59+
[fileManager createFileAtPath:path
3860
contents:[NSData data]
3961
attributes:nil] ;
4062

@@ -56,8 +78,10 @@ - (void)writeLine:(NSString*)line {
5678

5779
+ (void)setToPath:(NSString*)path {
5880
if (sharedFileWriter) {
59-
[sharedFileWriter release] ;
60-
sharedFileWriter = nil ;
81+
#if !__has_feature(objc_arc)
82+
[sharedFileWriter release] ;
83+
#endif
84+
sharedFileWriter = nil ;
6185
}
6286
[[SSYLinearFileWriter sharedFileWriter] setPath:path] ;
6387
}
@@ -70,11 +94,13 @@ + (void)close {
7094
[[SSYLinearFileWriter sharedFileWriter] setFileHandle:nil] ;
7195
}
7296

97+
#if !__has_feature(objc_arc)
7398
- (void)dealloc {
7499
[m_fileHandle release] ;
75100

76101
[super dealloc] ;
77102
}
103+
#endif
78104

79105
@end
80106

0 commit comments

Comments
 (0)