-// Hash Functions/Structures {{{
-extern "C" uint32_t hashlittle(const void *key, size_t length, uint32_t initval = 0);
-
-union SplitHash {
- uint32_t u32;
- uint16_t u16[2];
-};
-// }}}
-
-static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
-
-static _finline NSString *CydiaURL(NSString *path) {
- char page[26];
- page[0] = 'h'; page[1] = 't'; page[2] = 't'; page[3] = 'p'; page[4] = 's';
- page[5] = ':'; page[6] = '/'; page[7] = '/'; page[8] = 'c'; page[9] = 'y';
- page[10] = 'd'; page[11] = 'i'; page[12] = 'a'; page[13] = '.'; page[14] = 's';
- page[15] = 'a'; page[16] = 'u'; page[17] = 'r'; page[18] = 'i'; page[19] = 'k';
- page[20] = '.'; page[21] = 'c'; page[22] = 'o'; page[23] = 'm'; page[24] = '/';
- page[25] = '\0';
- return [[NSString stringWithUTF8String:page] stringByAppendingString:path];
-}
-
-static _finline void UpdateExternalStatus(uint64_t newStatus) {
- int notify_token;
- if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) {
- notify_set_state(notify_token, newStatus);
- notify_cancel(notify_token);
- }
- notify_post("com.saurik.Cydia.status");
-}
-
-/* [NSObject yieldToSelector:(withObject:)] {{{*/
-@interface NSObject (Cydia)
-- (id) yieldToSelector:(SEL)selector withObject:(id)object;
-- (id) yieldToSelector:(SEL)selector;
-@end
-
-@implementation NSObject (Cydia)
-
-- (void) doNothing {
-}
-
-- (void) _yieldToContext:(NSMutableArray *)context { _pooled
- SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
- id object([[context objectAtIndex:1] nonretainedObjectValue]);
- volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
-
- /* XXX: deal with exceptions */
- id value([self performSelector:selector withObject:object]);
-
- NSMethodSignature *signature([self methodSignatureForSelector:selector]);
- [context removeAllObjects];
- if ([signature methodReturnLength] != 0 && value != nil)
- [context addObject:value];
-
- stopped = true;
-
- [self
- performSelectorOnMainThread:@selector(doNothing)
- withObject:nil
- waitUntilDone:NO
- ];
-}
-
-- (id) yieldToSelector:(SEL)selector withObject:(id)object {
- volatile bool stopped(false);
-
- NSMutableArray *context([NSMutableArray arrayWithObjects:
- [NSValue valueWithPointer:selector],
- [NSValue valueWithNonretainedObject:object],
- [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
- nil]);
-
- NSThread *thread([[[NSThread alloc]
- initWithTarget:self
- selector:@selector(_yieldToContext:)
- object:context
- ] autorelease]);
-
- [thread start];
-
- NSRunLoop *loop([NSRunLoop currentRunLoop]);
- NSDate *future([NSDate distantFuture]);
- NSString *mode([loop currentMode] ?: NSDefaultRunLoopMode);
-
- while (!stopped && [loop runMode:mode beforeDate:future]);
-
- return [context count] == 0 ? nil : [context objectAtIndex:0];
-}
-
-- (id) yieldToSelector:(SEL)selector {
- return [self yieldToSelector:selector withObject:nil];
-}
-
-@end
-/* }}} */
-
-/* Cydia Alert View {{{ */
-@interface CYAlertView : UIAlertView {
- unsigned button_;
-}
-
-- (int) yieldToPopupAlertAnimated:(BOOL)animated;
-
-@end
-
-@implementation CYAlertView
-
-- (id) initWithTitle:(NSString *)title buttons:(NSArray *)buttons defaultButtonIndex:(int)index {
- if ((self = [super init]) != nil) {
- [self setTitle:title];
- [self setDelegate:self];
- for (NSString *button in buttons) [self addButtonWithTitle:button];
- [self setCancelButtonIndex:index];
- } return self;
-}
-
-- (void) _updateFrameForDisplay {
- [super _updateFrameForDisplay];
- if ([self cancelButtonIndex] == -1) {
- NSArray *buttons = [self buttons];
- if ([buttons count]) {
- UIImage *background = [[buttons objectAtIndex:0] backgroundForState:0];
- for (UIThreePartButton *button in buttons)
- [button setBackground:background forState:0];
- }
- }
-}
-
-- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
- button_ = buttonIndex + 1;
-}
-
-- (void) dismiss {
- [self dismissWithClickedButtonIndex:-1 animated:YES];
-}
-
-- (int) yieldToPopupAlertAnimated:(BOOL)animated {
- [self setRunsModal:YES];
- button_ = 0;
- [self show];
- return button_;
-}
-
-@end
-/* }}} */
-
-/* NSForcedOrderingSearch doesn't work on the iPhone */
-static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch;
-static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch;
-static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareCaseInsensitive | kCFCompareNonliteral | kCFCompareLocalized | kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;