Cheap Base64 Encoding in Mac OS X

With the help of the following, I was able to put together a base64-encoding technique on Mac OS X in Objective C that doesn’t rely on OpenSSL to get its job done. Works on small pieces of data, which is all I need it for, e. g. the Authorization header on a HTTP request:

NSData *xmlData;
NSString *error;
NSString *base64String;

xmlData = [NSPropertyListSerialization dataFromPropertyList:[@"userid:password" dataUsingEncoding:NSUTF8StringEncoding]
format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
NSScanner *theScanner = [NSScanner scannerWithString:[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]];

[theScanner scanUpToString:@"<data>" intoString:NULL];
[theScanner scanString:@"<data>" intoString:NULL];
[theScanner scanUpToString:@"</data>" intoString:&base64String];
NSLog(@"Base64: %@n", base64String);

CppUnit Head Start

I was building a new project to do some experiments with wchar_t this morning. (Look here for the reason.) It seemed to me it took way longer than it should simply to get a CppUnit project setup in Xcode with a single test case.  (It was primarily due to C++ and pilot error; I gotta say I’m gonna get a cold constantly moving between Ruby and C++.)

Consequently, I have built a skeleton CppUnit Xcode project. This provides you with a single class with a test method which asserts FALSE, demonstrating that the test process is working properly. It’s up to you to do something interesting with this head start.

Prerequisite for all platforms: install CppUnit.

If you’re on Mac OS X, with any luck you can simply build-and-go and you’ll see the results of the test (failure) in the debugger console.

If you’re on Linux, just disregard everything Xcode-related; you can still build and run the code from the command line (see the README file).

If you’re on Windows, gosh I’m sorry. 🙂 Nevertheless, you should be able to load these files into Visual Studio, set your header search directory to wherever CppUnit is installed, add the CppUnit library to your project, build and run.

Please let me know if you have any problems with this so I can keep it updated and hopefully save others a little time during CppUnit ramp up.  Now GO WRITE A TEST!

Building log4cpp Universal Binary on Mac OS X

Here’s the configure command I used to build a universal binary for log4cpp on Mac OS X:

$ env CFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.5.sdk -arch i386 -arch ppc" \
LDFLAGS="-arch i386 -arch ppc" \
CXXFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.5.sdk -arch i386 -arch ppc" \
./configure --disable-dependency-tracking

Duplicating Lines in Xcode

I’ve been working in TextMate for Rails development for the past year now. Recently, I’ve been doing a lot of Mac OS X and iPhone development in Objective-C. Of course, this puts me into Xcode for the vast majority of my day.

The one keyboard shortcut I missed the most in Xcode from TextMate was ctrl-shift-D or “Duplicate Lines.” It turns out it’s insanely simple to add this to your Xcode environment.

  1. In Xcode, go to the Script menu (the stylized S to the left of Help) and select Edit User Scripts…
  2. Decide where you want to place your new command (I put it at the bottom of the Text menu). Click there. Then click the + menu (lower-left corner of the window) and select New Shell Script.
  3. Double-click where it says “Shell Script” to edit the name of your new command. Double-clicking to the right of the name allows you to assign a keystroke to the command. I used control-shift-D — same as TextMate.
  4. The menus should read as follows:
    • Input: Selection
    • Directory: Selection
    • Output: Insert after Selection
    • Errors: Ignore Errors
  5. Now click in the script area of the window and type in the following new line:
    cat <&0

Here’s how mine ended up looking:

That’s it! Close the window and enjoy.

Xcode View Project Window Shortcut

Yes, it would probably help if I spent more quality time working with Xcode, especially since I’ve been using it for probably four years now. Nevertheless, I just discovered an abundantly useful command key shortcut for when you’re buried in windows and just want to see the main project window: ⌘0 (command-zero) brings the main project window to the front. This equates to selecting the “Project” item from the “Project” menu.

A Short Lesson in Unicode, nvarchar, and MS SQL Server

Even if you have a column in a table that is specified as nvarchar, you still need to tip off SQL Server that you are handing it a string that contains Unicode when you’re setting values in the column. Witnesseth:


1> create table interjunk(id int not null, inter nvarchar(255))
2> go
1> insert into interjunk(id, inter) values (1, 'Unicode string ψ')
2> insert into interjunk(id, inter) values (2, N'Unicode string ψ')
3> go
(1 row affected)
(1 row affected)
1> select * from interjunk
2> go -m vert
id: 1
inter: Unicode string ?

id: 2
inter: Unicode string ψ

(2 rows affected)

CppUnit on OS X

I have been trying to get a simple C++ test to run using CppUnit on OS X. Unfortunately, I was running into problems left-and-right, no matter what approach I tried (Xcode or GNU build tools).

Finally, I stumbled across the following bit of advice.

I rebuilt CppUnit from sources using the two specified command lines (./configure and make are two separate commands in case it is not clear from the example). Then I did a clean and build-and-go in my Xcode project and IT FINALLY WORKED.