This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNibwareFileManager.m
More file actions
137 lines (109 loc) · 3.16 KB
/
NibwareFileManager.m
File metadata and controls
137 lines (109 loc) · 3.16 KB
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
//
// NibwareFileManager.m
// pingle
//
// Created by robertsanders on 1/11/09.
// Copyright 2009 Robert Sanders. All rights reserved.
//
#include <stdlib.h>
#import "NibwareFileManager.h"
static NSMutableArray *_tempFiles;
@implementation NibwareFileManager
+ (NibwareFileManager*) singleton
{
static NibwareFileManager *sharedSingleton;
@synchronized(self)
{
if (!sharedSingleton)
sharedSingleton = [[NibwareFileManager alloc] init];
return sharedSingleton;
}
// shutup Xcode
return sharedSingleton;
}
+ (void) atexit
{
NSLog(@"removing temporary files for NibwareFileManager");
for (NSString *name in _tempFiles)
{
NSLog(@"deleting app-scope temp file %@", name);
unlink([name cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
void NibwareFileManagerAtexit()
{
[NibwareFileManager atexit];
}
+ (id) initialize
{
_tempFiles = [[NSMutableArray alloc] init];
atexit(NibwareFileManagerAtexit);
return self;
}
- (NibwareFileManager *)init
{
return self;
}
- (NSString *) makeTempFileName:(NSString *)format
{
static BOOL seeded = NO;
NSString *tmpdir = NSTemporaryDirectory();
if (! seeded) {
sranddev();
}
NSString *rndstr = [[NSNumber numberWithInt:rand()] stringValue];
return [[NSString stringWithFormat:@"%@%@",
tmpdir,
[NSString stringWithFormat:format, rndstr]]
stringByStandardizingPath];
}
- (NSString *) makeTempFileName
{
return [self makeTempFileName:@"tmp.nw.%@"];
}
- (void) registerApplicationScopeFile:(NSString*)fileName
{
if (fileName) {
[_tempFiles addObject:fileName];
}
}
- (NSFileHandle *) createApplicationScopeTempFile
{
NSString *fileName = [self makeTempFileName];
[self registerApplicationScopeFile:fileName];
if (![[NSFileManager defaultManager] createFileAtPath:fileName contents:[NSData data] attributes:nil]) {
return nil;
}
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[file truncateFileAtOffset:0];
return file;
}
#define ADD_MAP_ENTRY(a,b) do { id key = (a), value = (b); \
if (toFlag) { id tmp = key; key = value; value = tmp; } \
[dict setObject:value forKey:key]; } while(0)
- (NSDictionary*) getRelativeMap:(BOOL) toFlag
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
ADD_MAP_ENTRY(@"%%NSHOMEDIR%%", NSHomeDirectory());
return dict;
}
- (NSString *) convertPath:(NSString *)path withDict:(NSDictionary *)dict
{
for (NSString *fromString in dict) {
if ([path hasPrefix:fromString]) {
return [NSString stringWithFormat:@"%@%@",
[dict objectForKey:fromString],
[path substringFromIndex:[fromString length]]];
}
}
return path;
}
- (NSString *) convertToAppRelativePath:(NSString *)path
{
return [self convertPath:path withDict:[self getRelativeMap:YES]];
}
- (NSString *) convertFromAppRelativePath:(NSString *)path
{
return [self convertPath:path withDict:[self getRelativeMap:NO]];
}
@end