Archive for June, 2008

Cocoa iCal Framework for Tiger

There is an iCal API available for Leopard. For Tiger there is no API but you can try using the Private framework (CALCore.framework). These frameworks are supposedly not to be used (hence private). Apple provides no documentation and updates can break your code.

Useful links:

Cocoa: Archiving objects, delegating the “Application”

How do you delegate the application? In Xcode 2.5 the Interface Builder has
no object named NSApplication in the Instances tab. Apparently the icon “File’s
Owner” is the same as your application. Delegate that object to your controller
and implement any of the NSApplication delegate methods.

Archiving:

Your object must implement the NSCoding protocol to be archivable. All you have
to do is something like this:

file.h


#import < Cocoa/Cocoa.h >

#import < Foundation/NSCoder.h >

@interface FTModel : NSObject < NSCoding >

{

@public

int myInt;

NSString *myWord;

}

-(void) encodeWithCoder:(NSCoder *) coder;

-(id) initWithCoder:(NSCoder *) coder;

@end

file.m


#import "file.h"

@implementation FTModel

-(void) encodeWithCoder:(NSCoder *) coder

{

[coder encodeInt:myInt forKey:@"favNum"];

[coder encodeObject:myWord forKey:@"favWord"];

}

-(id) initWithCoder:(NSCoder *) coder

{

if(self=[super init]) {

myNum = [coder decodeIntForKey:@"favNum"] ;

//You have to retain this obj or you are in trouble

myWord = [[coder decodeObjectForKey:@"favWord"] retain];

}

return self;

}

@end

And in the place where you are going to archive the FTModel object:


FTModel *ftModel;

.

.

.

//archive object to file

[NSKeyedArchiver archiveRootObject:ftModel toFile:fileName];

//read the object

ftModel= [[NSKeyedUnarchiver unarchiveObjectWithFile:fileName] retain];

Once you implement the NSCode protocol the above calls to NSKeyedArchiver just
work.

Computing parity in C

Fast way to compute the parity of a 32-bit number in C. Here is how it works:

Parity of a bit-vector is just an XOR of all bits in the vector. The first part of this function does just that – keep XORing the top half of the bit vector with the bottom half. Till we get down to 4 bits.

Then we right shift a “magic number” to get the parity. This magic number is organized so that we always get the right answer.

0×6996 in binary is 0110 1001 1001 0110

For example, if the final value of i after all the XORing is 0001 or 0010 then the parity should be 1. Thats what you’ll get when you shift the above number by 1 or 2.

Note: Instead of using the magic number you can simply continue to XOR. The magic number trick saves a couple of shift-xor operations.


  i ^= (i >> 2);
  i = (i ^ (i >>1)) & 1;


int computeParity(int i) {
  //xor top 16 bits with bottom 16 bits
  i ^= i >> 16;
  //xor top 8 bits with bottom 8 bits
  i ^= i >> 8;
  //xor top 4 bits with bottom 4 bits, mask out 4 bottom bits
  i = (i ^ (i >> 4)) & 0xf;

  //shift "magic number" to get parity
  return (0x6996 >> i) & 1;
}

Bash Vi command line editing

Useful howto on bash Vi command line editing HERE.

Highlights:
>set -o vi

sets vi cmd line editing mode

It drops you into insert mode. Hitting Esc will get you into command mode like usual. Many vi editing commands work. Search (/foo) does not search the current command line. Instead it searches your bash history. Hitting ‘n’ after a search finds the next match as usual. The substitute cmd (s/foo/bar) does not work.

Hitting ‘v’ when in command mode opens up vi with your command line. Now you are in regular vi – the usual editing works. Once done editing ‘:wq’ will quit vi and execute the command.

Tags: , , ,

“Full Body Scans” – Coming soon to an airport near you





The US TSA blog says they have started trial deployments of Millimeter wave imaging devices at Phoenix, Baltimore, LA, and NY (JFK).