forked from shaojiankui/JKCategories
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NSData+zlib NSDictionary+URL NSDictionary+XML NSRunLoop+PerformBlock NSMutableString+Ruby NSString+Matcher NSString+XML NSURL+QueryDictionary UIView+GestureCallback
- Loading branch information
Jakey
authored and
Jakey
committed
Aug 7, 2015
1 parent
bbc32da
commit 5c3b926
Showing
28 changed files
with
2,922 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// NSData+APNSToken.h | ||
// IOS-Categories | ||
// | ||
// Created by Jakey on 15/8/7. | ||
// Copyright © 2015年 www.skyfox.org. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSData (APNSToken) | ||
- (NSString *)APNSToken; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// NSData+APNSToken.m | ||
// IOS-Categories | ||
// | ||
// Created by Jakey on 15/8/7. | ||
// Copyright © 2015年 www.skyfox.org. All rights reserved. | ||
// | ||
|
||
#import "NSData+APNSToken.h" | ||
|
||
@implementation NSData (APNSToken) | ||
- (NSString *)APNSToken { | ||
return [[[[self description] | ||
stringByReplacingOccurrencesOfString: @"<" withString: @""] | ||
stringByReplacingOccurrencesOfString: @">" withString: @""] | ||
stringByReplacingOccurrencesOfString: @" " withString: @""]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
//https://github.com/bitbasenyc/nsdata-zlib | ||
@interface NSData (zlib) | ||
|
||
/** | ||
ZLib error domain | ||
*/ | ||
extern NSString *const BBSZlibErrorDomain; | ||
/** | ||
When a zlib error occurs, querying this key in the @p userInfo dictionary of the | ||
@p NSError object will return the underlying zlib error code. | ||
*/ | ||
extern NSString *const BBSZlibErrorInfoKey; | ||
|
||
typedef NS_ENUM(NSUInteger, BBSZlibErrorCode) { | ||
BBSZlibErrorCodeFileTooLarge = 0, | ||
BBSZlibErrorCodeDeflationError = 1, | ||
BBSZlibErrorCodeInflationError = 2, | ||
BBSZlibErrorCodeCouldNotCreateFileError = 3, | ||
}; | ||
|
||
/** | ||
Apply zlib compression. | ||
@param error If an error occurs during compression, upon return contains an | ||
NSError object describing the problem. | ||
@returns An NSData instance containing the result of applying zlib | ||
compression to this instance. | ||
*/ | ||
- (NSData *)bbs_dataByDeflatingWithError:(NSError *__autoreleasing *)error; | ||
|
||
/** | ||
Apply zlib decompression. | ||
@param error If an error occurs during decompression, upon return contains an | ||
NSError object describing the problem. | ||
@returns An NSData instance containing the result of applying zlib | ||
decompression to this instance. | ||
*/ | ||
- (NSData *)bbs_dataByInflatingWithError:(NSError *__autoreleasing *)error; | ||
|
||
/** | ||
Apply zlib compression and write the result to a file at path | ||
@param path The path at which the file should be written | ||
@param error If an error occurs during compression, upon return contains an | ||
NSError object describing the problem. | ||
@returns @p YES if the compression succeeded; otherwise, @p NO. | ||
*/ | ||
- (BOOL)bbs_writeDeflatedToFile:(NSString *)path | ||
error:(NSError *__autoreleasing *)error; | ||
|
||
/** | ||
Apply zlib decompression and write the result to a file at path | ||
@param path The path at which the file should be written | ||
@param error If an error occurs during decompression, upon return contains an | ||
NSError object describing the problem. | ||
@returns @p YES if the compression succeeded; otherwise, @p NO. | ||
*/ | ||
- (BOOL)bbs_writeInflatedToFile:(NSString *)path | ||
error:(NSError *__autoreleasing *)error; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
@import Foundation; | ||
|
||
#import <zlib.h> | ||
|
||
#import "NSData+zlib.h" | ||
|
||
static const uInt CHUNK_SIZE = 65536; | ||
|
||
NSString *const BBSZlibErrorDomain = @"se.bitba.ZlibErrorDomain"; | ||
NSString *const BBSZlibErrorInfoKey = @"zerror"; | ||
|
||
@implementation NSData (zlib) | ||
|
||
- (NSData *)bbs_dataByInflatingWithError:(NSError *__autoreleasing *)error | ||
{ | ||
if (![self length]) return [self copy]; | ||
NSMutableData *outData = [NSMutableData data]; | ||
[self inflate:^(NSData *toAppend) { | ||
[outData appendData:toAppend]; | ||
} | ||
error:error]; | ||
return outData; | ||
} | ||
|
||
- (NSData *)bbs_dataByDeflatingWithError:(NSError *__autoreleasing *)error | ||
{ | ||
if (![self length]) return [self copy]; | ||
NSMutableData *outData = [NSMutableData data]; | ||
[self deflate:^(NSData *toAppend) { | ||
[outData appendData:toAppend]; | ||
} | ||
error:error]; | ||
return outData; | ||
} | ||
|
||
// Adapted from http://www.zlib.net/zpipe.c | ||
- (BOOL)inflate:(void (^)(NSData *))processBlock | ||
error:(NSError *__autoreleasing *)error | ||
{ | ||
z_stream stream; | ||
stream.zalloc = Z_NULL; | ||
stream.zfree = Z_NULL; | ||
stream.opaque = Z_NULL; | ||
stream.avail_in = 0; | ||
stream.next_in = Z_NULL; | ||
|
||
int ret = inflateInit(&stream); | ||
if (ret != Z_OK) { | ||
if (error) *error = [NSError errorWithDomain:BBSZlibErrorDomain | ||
code:BBSZlibErrorCodeInflationError | ||
userInfo:@{BBSZlibErrorInfoKey : @(ret)}]; | ||
return NO; | ||
} | ||
Bytef *source = (Bytef *)[self bytes]; // yay | ||
uInt offset = 0; | ||
uInt len = (uInt)[self length]; | ||
|
||
do { | ||
stream.avail_in = MIN(CHUNK_SIZE, len - offset); | ||
if (stream.avail_in == 0) break; | ||
stream.next_in = source + offset; | ||
offset += stream.avail_in; | ||
do { | ||
Bytef out[CHUNK_SIZE]; | ||
stream.avail_out = CHUNK_SIZE; | ||
stream.next_out = out; | ||
ret = inflate(&stream, Z_NO_FLUSH); | ||
switch (ret) { | ||
case Z_NEED_DICT: | ||
case Z_DATA_ERROR: | ||
case Z_MEM_ERROR: | ||
case Z_STREAM_ERROR: | ||
inflateEnd(&stream); | ||
if (error) *error = [NSError errorWithDomain:BBSZlibErrorDomain | ||
code:BBSZlibErrorCodeInflationError | ||
userInfo:@{BBSZlibErrorInfoKey : @(ret)}]; | ||
return NO; | ||
} | ||
processBlock([NSData dataWithBytesNoCopy:out | ||
length:CHUNK_SIZE - stream.avail_out | ||
freeWhenDone:NO]); | ||
} while (stream.avail_out == 0); | ||
} while (ret != Z_STREAM_END); | ||
|
||
inflateEnd(&stream); | ||
return YES; | ||
} | ||
|
||
// Adapted from http://www.zlib.net/zpipe.c | ||
- (BOOL)deflate:(void (^)(NSData *))processBlock | ||
error:(NSError *__autoreleasing *)error | ||
{ | ||
z_stream stream; | ||
stream.zalloc = Z_NULL; | ||
stream.zfree = Z_NULL; | ||
stream.opaque = Z_NULL; | ||
|
||
int ret = deflateInit(&stream, 9); | ||
if (ret != Z_OK) { | ||
if (error) *error = [NSError errorWithDomain:BBSZlibErrorDomain | ||
code:BBSZlibErrorCodeDeflationError | ||
userInfo:@{BBSZlibErrorInfoKey : @(ret)}]; | ||
return NO; | ||
} | ||
Bytef *source = (Bytef *)[self bytes]; // yay | ||
uInt offset = 0; | ||
uInt len = (uInt)[self length]; | ||
int flush; | ||
|
||
do { | ||
stream.avail_in = MIN(CHUNK_SIZE, len - offset); | ||
stream.next_in = source + offset; | ||
offset += stream.avail_in; | ||
flush = offset > len - 1 ? Z_FINISH : Z_NO_FLUSH; | ||
do { | ||
Bytef out[CHUNK_SIZE]; | ||
stream.avail_out = CHUNK_SIZE; | ||
stream.next_out = out; | ||
ret = deflate(&stream, flush); | ||
if (ret == Z_STREAM_ERROR) { | ||
if (error) *error = [NSError errorWithDomain:BBSZlibErrorDomain | ||
code:BBSZlibErrorCodeDeflationError | ||
userInfo:@{BBSZlibErrorInfoKey : @(ret)}]; | ||
return NO; | ||
} | ||
processBlock([NSData dataWithBytesNoCopy:out | ||
length:CHUNK_SIZE - stream.avail_out | ||
freeWhenDone:NO]); | ||
} while (stream.avail_out == 0); | ||
} while (flush != Z_FINISH); | ||
deflateEnd(&stream); | ||
return YES; | ||
} | ||
|
||
- (BOOL)bbs_writeDeflatedToFile:(NSString *)path | ||
error:(NSError *__autoreleasing *)error | ||
{ | ||
NSFileHandle *f = createOrOpenFileAtPath(path, error); | ||
if (!f) return NO; | ||
BOOL success = YES; | ||
if ([self length]) { | ||
success = [self deflate: | ||
^(NSData *toAppend) { | ||
[f writeData:toAppend]; | ||
} | ||
error:error]; | ||
} else { | ||
[f writeData:self]; | ||
} | ||
[f closeFile]; | ||
return success; | ||
} | ||
|
||
- (BOOL)bbs_writeInflatedToFile:(NSString *)path | ||
error:(NSError *__autoreleasing *)error | ||
{ | ||
NSFileHandle *f = createOrOpenFileAtPath(path, error); | ||
if (!f) return NO; | ||
BOOL success = YES; | ||
if ([self length]) { | ||
success = [self inflate: | ||
^(NSData *toAppend) { | ||
[f writeData:toAppend]; | ||
} | ||
error:error]; | ||
} else { | ||
[f writeData:self]; | ||
} | ||
[f closeFile]; | ||
return success; | ||
} | ||
|
||
static NSFileHandle *createOrOpenFileAtPath(NSString *path, NSError *__autoreleasing *error) | ||
{ | ||
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { | ||
BOOL success = [[NSFileManager defaultManager] createFileAtPath:path | ||
contents:nil | ||
attributes:nil]; | ||
if (!success) { | ||
if (error) *error = [NSError errorWithDomain:BBSZlibErrorDomain | ||
code:BBSZlibErrorCodeCouldNotCreateFileError | ||
userInfo:nil]; | ||
return nil; | ||
} | ||
} | ||
return [NSFileHandle fileHandleForWritingAtPath:path]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// NSDictionary+URL.h | ||
// IOS-Categories | ||
// | ||
// Created by Jakey on 15/8/7. | ||
// Copyright © 2015年 www.skyfox.org. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSDictionary (URL) | ||
+ (NSDictionary *)dictionaryWithURLQuery:(NSString *)query; | ||
- (NSString *)URLQueryString; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// NSDictionary+URL.m | ||
// IOS-Categories | ||
// | ||
// Created by Jakey on 15/8/7. | ||
// Copyright © 2015年 www.skyfox.org. All rights reserved. | ||
// | ||
|
||
#import "NSDictionary+URL.h" | ||
|
||
@implementation NSDictionary (URI) | ||
+ (NSDictionary *)dictionaryWithURLQuery:(NSString *)query | ||
{ | ||
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | ||
NSArray *parameters = [query componentsSeparatedByString:@"&"]; | ||
for(NSString *parameter in parameters) { | ||
NSArray *contents = [parameter componentsSeparatedByString:@"="]; | ||
if([contents count] == 2) { | ||
NSString *key = [contents objectAtIndex:0]; | ||
NSString *value = [contents objectAtIndex:1]; | ||
value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | ||
if (key && value) { | ||
[dict setObject:value forKey:key]; | ||
} | ||
} | ||
} | ||
return [NSDictionary dictionaryWithDictionary:dict]; | ||
} | ||
|
||
- (NSString *)URLQueryString | ||
{ | ||
NSMutableString *string = [NSMutableString string]; | ||
for (NSString *key in [self allKeys]) { | ||
if ([string length]) { | ||
[string appendString:@"&"]; | ||
} | ||
CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)[[self objectForKey:key] description], | ||
NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]", | ||
kCFStringEncodingUTF8); | ||
[string appendFormat:@"%@=%@", key, escaped]; | ||
CFRelease(escaped); | ||
} | ||
return string; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// NSDictionary+XML.h | ||
// IOS-Categories | ||
// | ||
// Created by Jakey on 15/8/7. | ||
// Copyright © 2015年 www.skyfox.org. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSDictionary (XML) | ||
- (NSString *)XMLString; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// NSDictionary+XML.m | ||
// IOS-Categories | ||
// | ||
// Created by Jakey on 15/8/7. | ||
// Copyright © 2015年 www.skyfox.org. All rights reserved. | ||
// | ||
|
||
#import "NSDictionary+XML.h" | ||
|
||
@implementation NSDictionary (XML) | ||
|
||
- (NSString *)XMLString { | ||
|
||
NSString *xmlStr = @"<xml>"; | ||
|
||
for (NSString *key in self.allKeys) { | ||
|
||
NSString *value = [self objectForKey:key]; | ||
|
||
xmlStr = [xmlStr stringByAppendingString:[NSString stringWithFormat:@"<%@>%@</%@>", key, value, key]]; | ||
} | ||
|
||
xmlStr = [xmlStr stringByAppendingString:@"</xml>"]; | ||
|
||
return xmlStr; | ||
} | ||
|
||
@end |
Oops, something went wrong.