-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempfile-apple.m
76 lines (68 loc) · 2.23 KB
/
tempfile-apple.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
#import <Foundation/Foundation.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "tempfile.h"
struct TempFile* make_temp_file(const char* format) {
char* temp_file;
size_t suffix_len;
// Need to manually create a NSAutoreleasePool to release memory since this
// isn't a standard OS X app.
@autoreleasepool {
NSString* temp_dir_ns = NSTemporaryDirectory();
if(temp_dir_ns == nil) {
return NULL;
}
// Replace wildcard ('%') with 'XXXXXX' for mkstemp(). This is a
// hundred times easier using Cocoa than fiddling with C-strings in the
// Windows/Linux version ;)
NSString* format_ns = [NSString stringWithUTF8String:format];
NSRange wildcard_range = [format_ns rangeOfString: @"%"];
if(wildcard_range.location == NSNotFound) {
return NULL;
}
suffix_len = strlen(
[[format_ns substringFromIndex:
wildcard_range.location + 1
] UTF8String]
);
format_ns = [format_ns stringByReplacingCharactersInRange:
wildcard_range withString: @"XXXXXX"
];
// Concatenate the directory and filename
NSString* temp_file_ns = [temp_dir_ns stringByAppendingPathComponent:
format_ns
];
// Finish up with Cocoa and get a C-string to use with mkstemp().
size_t temp_file_len = strlen([temp_file_ns UTF8String]);
temp_file = malloc((temp_file_len + 1) * sizeof(char));
strcpy(temp_file, [temp_file_ns UTF8String]);
}
// Open the temporary file
int fd;
if(suffix_len == 0) {
fd = mkstemp(temp_file);
} else {
fd = mkstemps(temp_file, suffix_len);
}
if(fd == -1) {
free(temp_file);
return NULL;
}
FILE* file = fdopen(fd, "w+b");
if(file == NULL) {
close(fd);
unlink(temp_file);
free(temp_file);
return NULL;
}
setvbuf(file, NULL, _IOFBF, FileBufferLen);
struct TempFile* result = malloc(sizeof(struct TempFile));
result->file = file;
result->filename = temp_file;
return result;
}
void free_temp_file(struct TempFile* temp) {
free((char*)temp->filename);
free(temp);
}