Jens Alfke avatar Jens Alfke committed d52f6b0

Added NSData categories for GZip and Mnemonicode.
Minor tweaks elsewhere.

Comments (0)

Files changed (8)

CollectionUtils.h

 
 void setObj( id *var, id value );
 BOOL ifSetObj( id *var, id value );
-void setString( NSString **var, NSString *value );
-BOOL ifSetString( NSString **var, NSString *value );
+void setObjCopy( id *var, id valueToCopy );
+BOOL ifSetObjCopy( id *var, id value );
+
+static inline void setString( NSString **var, NSString *value ) {setObjCopy(var,value);}
+static inline BOOL ifSetString( NSString **var, NSString *value ) {return ifSetObjCopy(var,value);}
 
 BOOL kvSetSet( id owner, NSString *property, NSMutableSet *set, NSSet *newSet );
 BOOL kvAddToSet( id owner, NSString *property, NSMutableSet *set, id objToAdd );

CollectionUtils.m

     }
 }
 
-
-void setString( NSString **var, NSString *value )
-{
-    if( value != *var ) {
+void setObjCopy( id *var, id valueToCopy ) {
+    if( valueToCopy != *var ) {
         [*var release];
-        *var = [value copy];
+        *var = [valueToCopy copy];
     }
 }
 
-
-BOOL ifSetString( NSString **var, NSString *value )
+BOOL ifSetObjCopy( id *var, id value )
 {
-    if( value != *var && ![value isEqualToString: *var] ) {
+    if( value != *var && ![value isEqual: *var] ) {
         [*var release];
         *var = [value copy];
         return YES;
     va_start(args,where);
     where = [[NSString alloc] initWithFormat: where arguments: args];
     va_end(args);
-    if( sExceptionReporter ) {
-        Warn(@"Exception caught in %@:\n\t%@",where,x);
+    Warn(@"Exception caught in %@:\n\t%@\n%@",where,x,x.my_callStack);
+    if( sExceptionReporter )
         sExceptionReporter(x);
-    }else
-        Warn(@"Exception caught in %@:\n\t%@\n%@",where,x,x.my_callStack);
     [where release];
 }
 

NSData+Mnemonic.h

+//
+//  NSData+Mnemonic.h
+//  Cloudy
+//
+//  Created by Jens Alfke on 6/24/09.
+//  Copyright 2009 Jens Alfke. All rights reserved.
+//
+
+#import <Foundation/NSData.h>
+
+
+@interface NSData (MYMnemonic)
+
+- (NSString*) my_mnemonic;
+
+@end

NSData+Mnemonic.m

+//
+//  NSData+Mnemonic.m
+//  Cloudy
+//
+//  Created by Jens Alfke on 6/24/09.
+//  Copyright 2009 Jens Alfke. All rights reserved.
+//
+
+#import "NSData+Mnemonic.h"
+#import "mnemonic.h"
+
+
+@implementation NSData (Mnemonic)
+
+- (NSString*) my_mnemonic {
+    NSMutableData *chars = [NSMutableData dataWithLength: 10*mn_words_required(self.length)];
+    if (!chars)
+        return nil;
+    int result = mn_encode((void*)self.bytes, self.length,
+                           chars.mutableBytes, chars.length,
+                           MN_FDEFAULT);
+    if (result != 0) {
+        Warn(@"Mnemonic encoder failed: err=%i",result);
+        return nil;
+    }
+    return [[[NSString alloc] initWithUTF8String: chars.mutableBytes] autorelease];
+}
+
+@end
+//
+//  NSData+gzip.h
+//  Cloudy
+//
+//  Created by Jens Alfke on 6/27/09.
+//  Copyright 2009 Jens Alfke. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+@interface NSData (GZip)
+
+- (NSData*) my_gzippedWithCompression: (int)compression;
+- (NSData*) my_gunzipped;
+
+@end
+//
+//  NSData+gzip.m
+//  Cloudy
+//
+//  Created by Jens Alfke on 6/27/09.
+//  Copyright 2009 Jens Alfke. All rights reserved.
+//
+
+#import "NSData+gzip.h"
+#import "GTMNSData+zlib.h"
+
+@implementation NSData (gzip)
+
+- (NSData*) my_gzippedWithCompression: (int)compression {
+    return [NSData gtm_dataByGzippingBytes: self.bytes
+                                    length: self.length
+                          compressionLevel: compression];
+}
+
+- (NSData*) my_gunzipped {
+    return [NSData gtm_dataByInflatingBytes: self.bytes length: self.length];
+}
+
+@end
                                     CAssert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
                                 }while(0)
 
+#define AssertNil(VAL)          AssertEq((VAL),nil)
+#define CAssertNil(VAL)         CAssertEq((VAL),nil)
+
 #define AssertAbstractMethod()  _AssertAbstractMethodFailed(self,_cmd);
 
 // Nasty internals ...
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.