forked from iimgal/StudyiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIIFunction.m
More file actions
106 lines (83 loc) · 3.05 KB
/
Copy pathIIFunction.m
File metadata and controls
106 lines (83 loc) · 3.05 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
//
// IIFunction.m
// StudyiOS
//
// Created by on 11-10-14.
// Copyright (c) 2011年 ZhangYiCheng. All rights reserved.
//
#import "IIFunction.h"
@implementation IIFunction
// 检查URL格式,并补完
+ (NSURL *)smartURLForString:(NSString *)str
{
NSURL * result;
NSString * trimmedStr;
NSRange schemeMarkerRange;
NSString * scheme;
assert(str != nil);
result = nil;
trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
schemeMarkerRange = [trimmedStr rangeOfString:@"://"];
if (schemeMarkerRange.location == NSNotFound) {
result = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", trimmedStr]];
} else {
scheme = [trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
assert(scheme != nil);
if ( ([scheme compare:@"http" options:NSCaseInsensitiveSearch] == NSOrderedSame)
|| ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
result = [NSURL URLWithString:trimmedStr];
} else {
// It looks like this is some unsupported URL scheme.
}
}
}
return result;
}
// 得到临时文件路径
+ (NSString *)pathForTemporaryFileWithPrefix:(NSString *)prefix
{
NSString * result;
CFUUIDRef uuid;
CFStringRef uuidStr;
uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
uuidStr = CFUUIDCreateString(NULL, uuid);
assert(uuidStr != NULL);
result = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@", prefix, uuidStr]];
assert(result != nil);
CFRelease(uuidStr);
CFRelease(uuid);
return result;
}
// 检查文件类型是否匹配
+ (BOOL)isHTTPContentType:(NSString *)prefixStr contextType:(NSString *)contextType
{
BOOL result;
NSRange foundRange;
result = NO;
foundRange = [contextType rangeOfString:prefixStr options:NSAnchoredSearch | NSCaseInsensitiveSearch];
if (foundRange.location != NSNotFound) {
assert(foundRange.location == 0); // because it's anchored
if (foundRange.length == contextType.length) {
result = YES;
} else {
unichar nextChar;
nextChar = [contextType characterAtIndex:foundRange.length];
result = nextChar <= 32 || nextChar >= 127 || (strchr("()<>@,;:\\<>/[]?={}", nextChar) != NULL);
}
/*
From RFC 2616:
token = 1*<any CHAR except CTLs or separators>
separators = "(" | ")" | "<" | ">" | "@"
| "," | ";" | ":" | "\" | <">
| "/" | "[" | "]" | "?" | "="
| "{" | "}" | SP | HT
media-type = type "/" subtype *( ";" parameter )
type = token
subtype = token
*/
}
return result;
}
@end