Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add target/action queue. Misc. updates.

  • Loading branch information...
commit 0bf3810c600a62f1bfebd9593bfe869cba188fa3 1 parent ddb7c11
@ldandersen authored
View
29 Additions/NSMutableString+STAdditions.h
@@ -4,34 +4,21 @@
// Created by Buzz Andersen on 2/19/11.
// Copyright 2011 System of Touch. All rights reserved.
//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
#import <Foundation/Foundation.h>
@interface NSMutableString (STAdditions)
+// Predicates
- (void)appendPredicateCondition:(NSString *)predicateCondition;
+- (void)appendPredicateConditionWithOperator:(NSString *)inOperator string:(NSString *)inPredicateConditionString;
+
+- (void)appendPredicateConditionWithFormat:(NSString *)inPredicateConditionString, ...;
+- (void)appendPredicateConditionWithOperator:(NSString *)inOperator format:(NSString *)inPredicateCondition, ...;
+- (void)appendPredicateConditionWithOperator:(NSString *)inOperator format:(NSString *)inPredicateCondition arguments:(va_list)inArguments;
+
+// Paths
- (void)appendPathComponent:(NSString *)inPathComponent;
- (void)appendPathComponent:(NSString *)inPathComponent queryString:(NSString *)inQueryString;
- (void)appendPathComponents:(NSArray *)inPathComponents;
View
70 Additions/NSMutableString+STAdditions.m
@@ -4,42 +4,57 @@
// Created by Buzz Andersen on 2/19/11.
// Copyright 2011 System of Touch. All rights reserved.
//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
#import "STUtils.h"
@implementation NSMutableString (STAdditions)
-- (void)appendPredicateCondition:(NSString *)predicateCondition;
+#pragma mark Predicates
+
+- (void)appendPredicateCondition:(NSString *)inPredicateConditionString;
+{
+ [self appendPredicateConditionWithOperator:@"AND" string:inPredicateConditionString];
+}
+
+- (void)appendPredicateConditionWithOperator:(NSString *)inOperator string:(NSString *)inPredicateConditionString;
{
if (self.length) {
- [self appendString:@" AND "];
+ [self appendFormat:@" %@ ", inOperator];
}
- [self appendString:predicateCondition];
+ [self appendString:inPredicateConditionString];
+}
+
+- (void)appendPredicateConditionWithFormat:(NSString *)inPredicateConditionString, ...;
+{
+ va_list args;
+ va_start(args, inPredicateConditionString);
+
+ [self appendPredicateConditionWithOperator:@"AND" format:inPredicateConditionString arguments:args];
+
+ va_end(args);
}
+- (void)appendPredicateConditionWithOperator:(NSString *)inOperator format:(NSString *)inPredicateConditionString, ...;
+{
+ va_list args;
+ va_start(args, inPredicateConditionString);
+
+ [self appendPredicateConditionWithOperator:inOperator format:inPredicateConditionString arguments:args];
+
+ va_end(args);
+}
+
+- (void)appendPredicateConditionWithOperator:(NSString *)inOperator format:(NSString *)inPredicateConditionString arguments:(va_list)inArguments;
+{
+ NSString *formattedString = [[NSString alloc] initWithFormat:inPredicateConditionString arguments:inArguments];
+ [self appendPredicateConditionWithOperator:inOperator string:formattedString];
+ [formattedString release];
+}
+
+#pragma mark Paths
+
- (void)appendPathComponent:(NSString *)inPathComponent;
{
[self appendPathComponent:inPathComponent queryString:nil];
@@ -47,7 +62,12 @@ - (void)appendPathComponent:(NSString *)inPathComponent;
- (void)appendPathComponent:(NSString *)inPathComponent queryString:(NSString *)inQueryString;
{
- if (!inPathComponent.length || [inPathComponent isEqualToString:@"/"]) {
+ if (!inPathComponent.length) {
+ return;
+ }
+
+ if ([inPathComponent isEqualToString:@"/"]) {
+ [self appendString:inPathComponent];
return;
}
View
33 Additions/NSOutputStream+STAdditions.h
@@ -1,40 +1,19 @@
//
// NSOutputStream+STAdditions.h
+// Hipflask
//
// Created by Buzz Andersen on 4/10/12.
// Copyright (c) 2012 System of Touch. All rights reserved.
//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
-
#import <Foundation/Foundation.h>
@interface NSOutputStream (STAdditions)
-- (void)writeUTF8StringWithFormat:(NSString *)inString, ...;
-- (void)writeUTF8String:(NSString *)inUTF8String;
-- (void)writeString:(NSString *)inString usingEncoding:(NSStringEncoding)inEncoding;
-- (void)writeData:(NSData *)inData;
-- (void)writeFileDataAtPath:(NSString *)inPath withBufferSize:(NSInteger)inBufferSize;
+- (NSInteger)writeUTF8StringWithFormat:(NSString *)inString, ...;
+- (NSInteger)writeUTF8String:(NSString *)inUTF8String;
+- (NSInteger)writeString:(NSString *)inString usingEncoding:(NSStringEncoding)inEncoding;
+- (NSInteger)writeData:(NSData *)inData;
+- (NSInteger)writeFileDataAtPath:(NSString *)inPath withBufferSize:(NSInteger)inBufferSize;
@end
View
70 Additions/NSOutputStream+STAdditions.m
@@ -1,88 +1,82 @@
//
// NSOutputStream+STAdditions.m
-// Hipflask
//
// Created by Buzz Andersen on 4/10/12.
// Copyright (c) 2012 System of Touch. All rights reserved.
//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
#import "STUtils.h"
@implementation NSOutputStream (STAdditions)
-- (void)writeUTF8StringWithFormat:(NSString *)inString, ...;
+- (NSInteger)writeUTF8StringWithFormat:(NSString *)inString, ...;
{
va_list args;
va_start(args, inString);
NSString *formattedString = [[NSString alloc] initWithFormat:inString arguments:args];
- [self writeUTF8String:formattedString];
+ NSInteger bytesWritten = [self writeUTF8String:formattedString];
[formattedString release];
va_end(args);
+
+ return bytesWritten;
}
-- (void)writeUTF8String:(NSString *)inUTF8String;
+- (NSInteger)writeUTF8String:(NSString *)inUTF8String;
{
- [self writeString:inUTF8String usingEncoding:NSUTF8StringEncoding];
+ return [self writeString:inUTF8String usingEncoding:NSUTF8StringEncoding];
}
-- (void)writeString:(NSString *)inString usingEncoding:(NSStringEncoding)inEncoding;
+- (NSInteger)writeString:(NSString *)inString usingEncoding:(NSStringEncoding)inEncoding;
{
NSUInteger byteLength = [inString lengthOfBytesUsingEncoding:inEncoding];
-
if (!byteLength) {
- return;
+ return -1;
}
- char *buffer = malloc(byteLength);
+ uint8_t *buffer = malloc(byteLength);
+ NSInteger bytesWritten = 0;
- if ([inString getBytes:buffer maxLength:byteLength usedLength:NULL encoding:inEncoding options:NSStringEncodingConversionExternalRepresentation range:NSMakeRange(0,byteLength) remainingRange:NULL]) {
- [self write:(const uint8_t *)buffer maxLength:byteLength];
+ size_t usedByteLength;
+ if ([inString getBytes:buffer maxLength:byteLength usedLength:&usedByteLength encoding:inEncoding options:NSStringEncodingConversionExternalRepresentation range:NSMakeRange(0,byteLength) remainingRange:NULL]) {
+ if (!self.hasSpaceAvailable) {
+ NSLog(@"Output stream has no space available.");
+ }
+
+ bytesWritten = [self write:(const uint8_t *)buffer maxLength:usedByteLength];
}
free(buffer);
+
+ return bytesWritten;
}
-- (void)writeData:(NSData *)inData;
+- (NSInteger)writeData:(NSData *)inData;
{
- [self write:[inData bytes] maxLength:inData.length];
+ return [self write:[inData bytes] maxLength:inData.length];
}
-- (void)writeFileDataAtPath:(NSString *)inPath withBufferSize:(NSInteger)inBufferSize;
+- (NSInteger)writeFileDataAtPath:(NSString *)inPath withBufferSize:(NSInteger)inBufferSize;
{
if (!inPath.length || ![[NSFileManager defaultManager] fileExistsAtPath:inPath]) {
- return;
+ return -1;
}
- NSInputStream *fileInputStream = [NSInputStream inputStreamWithFileAtPath:inPath];
+ /*NSInputStream *fileInputStream = [NSInputStream inputStreamWithFileAtPath:inPath];
+ [fileInputStream open];
+
+ NSInteger bytesWritten = 0;
while ([fileInputStream hasBytesAvailable]) {
uint8_t fileBuffer[inBufferSize];
[fileInputStream read:fileBuffer maxLength:inBufferSize];
- [self write:fileBuffer maxLength:inBufferSize];
+ bytesWritten = [self write:fileBuffer maxLength:inBufferSize];
}
+
+ [fileInputStream close];*/
+
+ return [self writeData:[NSData dataWithContentsOfFile:inPath]];
}
@end
View
21 Additions/NSURL+STAdditions.h
@@ -4,27 +4,6 @@
// Created by Buzz Andersen on 12/29/09.
// Copyright 2011 System of Touch. All rights reserved.
//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
#import <Foundation/Foundation.h>
View
21 Additions/NSURL+STAdditions.m
@@ -4,27 +4,6 @@
// Created by Buzz Andersen on 12/29/09.
// Copyright 2011 System of Touch. All rights reserved.
//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
#import "STUtils.h"
View
30 Misc/STTargetActionQueue.h
@@ -0,0 +1,30 @@
+//
+// STTargetActionQueue.h
+//
+// Created by Buzz Andersen on 6/28/12.
+// Copyright (c) 2012 System of Touch. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface STTargetActionQueue : NSObject
+
+- (NSArray *)allKeys;
+- (void)addTarget:(id)inTarget action:(SEL)inAction forKey:(NSString *)inKey;
+- (void)removeTarget:(id)inTarget;
+- (void)removeTarget:(id)inTarget forKey:(NSString *)inKey;
+- (void)performActionsForKey:(NSString *)inKey;
+- (void)performActionsForKey:(NSString *)inKey withObject:(NSDictionary *)inObject;
+
+@end
+
+
+@interface STTargetAction : NSObject
+
+@property (nonatomic, retain) NSString *key;
+@property (nonatomic, assign) id target;
+@property (nonatomic, assign) SEL action;
+@property (nonatomic, copy) void(^actionBlock)();
+
+@end
View
183 Misc/STTargetActionQueue.m
@@ -0,0 +1,183 @@
+//
+// STTargetActionQueue.m
+//
+// Created by Buzz Andersen on 6/28/12.
+// Copyright (c) 2012 System of Touch. All rights reserved.
+//
+
+#import "STTargetActionQueue.h"
+#import "STUtils.h"
+
+
+@interface STTargetActionQueue ()
+
+@property (nonatomic, retain) NSMutableDictionary *targetActionInfo;
+
+- (void)_addTargetAction:(STTargetAction *)inTargetAction;
+
+@end
+
+
+@implementation STTargetActionQueue
+
+@synthesize targetActionInfo;
+
+#pragma mark Initialization
+
+- (void)dealloc;
+{
+ [targetActionInfo release];
+ [super dealloc];
+}
+
+#pragma mark Accessors
+
+- (NSMutableDictionary *)targetActionInfo;
+{
+ if (targetActionInfo == NULL) {
+ targetActionInfo = [[NSMutableDictionary alloc] init];
+ }
+
+ return targetActionInfo;
+}
+
+- (NSArray *)allKeys;
+{
+ return [self.targetActionInfo allKeys];
+}
+
+#pragma mark Target/Action Methods
+
+- (void)addTarget:(id)inTarget action:(SEL)inAction forKey:(NSString *)inKey;
+{
+ if (!inTarget || !inKey.length || !inAction) {
+ return;
+ }
+
+ STTargetAction *targetAction = [[[STTargetAction alloc] init] autorelease];
+ targetAction.key = inKey;
+ targetAction.target = inTarget;
+ targetAction.action = inAction;
+
+ [self _addTargetAction:targetAction];
+}
+
+- (void)addTarget:(id)inTarget actionBlock:(void(^)())inActionBlock forKey:(NSString *)inKey;
+{
+ if (!inTarget || !inKey.length || !inActionBlock) {
+ return;
+ }
+
+ STTargetAction *targetAction = [[[STTargetAction alloc] init] autorelease];
+ targetAction.key = inKey;
+ targetAction.actionBlock = inActionBlock;
+
+ [self _addTargetAction:targetAction];
+}
+
+- (void)_addTargetAction:(STTargetAction *)inTargetAction;
+{
+ if (!inTargetAction) {
+ return;
+ }
+
+ NSMutableArray *keyTargetActionsArray = (NSMutableArray *)[self.targetActionInfo objectForKey:inTargetAction.key];
+ if (!keyTargetActionsArray) {
+ // If we don't already have an array of target/actions for this
+ // key, create one and add it to the mapping.
+ keyTargetActionsArray = [[[NSMutableArray alloc] init] autorelease];
+ [keyTargetActionsArray addObject:inTargetAction];
+ [self.targetActionInfo setObject:keyTargetActionsArray forKey:inTargetAction.key];
+ return;
+ }
+
+ // If a target/action is specified and we already have an array of
+ // target/actions for this key, search to see if we already have this
+ // target present
+ if (inTargetAction.target) {
+ [self removeTarget:inTargetAction.target forKey:inTargetAction.key];
+ }
+
+ [keyTargetActionsArray addObject:inTargetAction];
+}
+
+- (void)removeTarget:(id)inTarget;
+{
+ // Iterate through all the keys and remove this target
+ // from each as appropriate
+ NSArray *keys = [self.targetActionInfo allKeys];
+ for (NSString *currentKey in keys) {
+ [self removeTarget:inTarget forKey:currentKey];
+ }
+}
+
+- (void)removeTarget:(id)inTarget forKey:(NSString *)inKey;
+{
+ NSMutableArray *keyTargetActionsArray = (NSMutableArray *)[self.targetActionInfo objectForKey:inKey];
+
+ NSInteger currentIndex = 0;
+ for (STTargetAction *currentTargetAction in keyTargetActionsArray) {
+ if (currentTargetAction.target == inTarget) {
+ // If the target is present for this key, remove it
+ [keyTargetActionsArray removeObjectAtIndex:currentIndex];
+ }
+
+ currentIndex++;
+ }
+}
+
+- (void)performActionsForKey:(NSString *)inKey;
+{
+ [self performActionsForKey:inKey withObject:nil];
+}
+
+- (void)performActionsForKey:(NSString *)inKey withObject:(NSDictionary *)inObject;
+{
+ if (!inKey.length) {
+ return;
+ }
+
+ NSMutableArray *keyTargetActionsArray = (NSMutableArray *)[self.targetActionInfo objectForKey:inKey];
+
+ for (STTargetAction *currentTargetAction in keyTargetActionsArray) {
+ // If the target/action specifies a block, execute it
+ // Otherwise, call the action selector on the target
+ if (currentTargetAction.actionBlock) {
+ if (inObject) {
+ currentTargetAction.actionBlock(inKey, inObject);
+ } else {
+ currentTargetAction.actionBlock(inKey);
+ }
+ } else if (currentTargetAction.target && currentTargetAction.action && [currentTargetAction.target respondsToSelector:currentTargetAction.action]) {
+ [currentTargetAction.target performSelectorOnMainThread:currentTargetAction.action withObject:inObject waitUntilDone:NO];
+ }
+ }
+}
+
+@end
+
+
+@implementation STTargetAction
+
+@synthesize key;
+
+@synthesize target;
+@synthesize action;
+
+@synthesize actionBlock;
+
+#pragma mark Initialization
+
+- (void)dealloc;
+{
+ [key release];
+
+ self.target = nil;
+ self.action = NULL;
+
+ [actionBlock release];
+
+ [super dealloc];
+}
+
+@end
View
1  STUtils.h
@@ -44,6 +44,7 @@
// Misc
#import "STKeychain.h"
#import "STRandomization.h"
+#import "STTargetActionQueue.h"
// UIKit
#if TARGET_OS_IPHONE
Please sign in to comment.
Something went wrong with that request. Please try again.