So this evening, while those who had money were off seeing Zumanity (Cirque du Soleil, n3kk1d), I started hacking again. I have about 1/3 of a POP3 server written in Perl. I got sidetracked halfway through and started playing with Apple's AddressBook API in C.
What I learned is that I really like Perl. Perl hides a lot of stuff that you really shouldn't have to think about. Like strings, for example. I can't believe how amazingly painful it is to just print a list of the names of people in your address book. This is, I suspect, a byproduct of using the CoreFoundation classes for strings, but I'm forced to because that's what AddressBook does.
Check it out:
for (i=0; i < count; i++) {
ABPersonRef thisPerson;
CFStringRef firstNameCF, lastNameCF;
CFDataRef firstNameDR, lastNameDR;
CFIndex firstNameLength, lastNameLength;
thisPerson = (ABPersonRef) CFArrayGetValueAtIndex(array, i);
firstNameCF = ABRecordCopyValue(thisPerson, kABFirstNameProperty);
if (firstNameCF) {
firstNameLength = CFStringGetLength(firstNameCF);
firstNameDR = CFStringCreateExternalRepresentation(NULL, firstNameCF, CFStringGetSystemEncoding(), '?');
printf("%.*s ",
(int)CFDataGetLength(firstNameDR), CFDataGetBytePtr(firstNameDR));
CFRelease(firstNameDR);
CFRelease(firstNameCF);
}
lastNameCF = ABRecordCopyValue(thisPerson, kABLastNameProperty);
if (lastNameCF) {
lastNameLength = CFStringGetLength(lastNameCF);
lastNameDR = CFStringCreateExternalRepresentation(NULL, lastNameCF, CFStringGetSystemEncoding(), '?');
printf("%.*s",
(int)CFDataGetLength(lastNameDR), CFDataGetBytePtr(lastNameDR));
CFRelease(lastNameDR);
CFRelease(lastNameCF);
}
printf("\n");
}
All that, just to do the equivalent of:
foreach my $thisPerson (@array) {
$firstName = $thisPerson->{firstName} || "";
$lastName = $thisPerson->{lastName} || "";
print "$firstName $lastName\n";
}
How can anyone program in this ridiculous quagmire of strings, C strings, data references,
--Nat
Avoid the CoreFoundation (Score:3, Insightful)
Here's the same code using the AddressBook framework. Note how it's almost Perl-like in a perverse kind of way:
Not as easy as Perl, but not as fugly as the CF* version. Throw this into a "Foundation Tool", add the framework, and you're good to go.Reply to This
Re:Avoid the CoreFoundation (Score:2)
So the CoreFoundation is just a cunning plot to make Objective C seem attractive? Got it. :-)
--Nat
Re:Avoid the CoreFoundation (Score:2)
Building... (Score:2)
I have followed some guidelines from the 'OS X for Unix Geeks' and ADC documentation, but without any luck.
I am on 10.3 and I have the developer tools installed.
Re:Building... (Score:2)
You may want to get a copy of one of the Cocoa programming books from O'Reilly. I've got Simson Garfinkel's (mostly because he wrote the NeXTSTEP version, from which the O'Reilly title is derived). I
pop3 server (Score:1)
Re:pop3 server (Score:2)
--Nat
Re:pop3 server (Score:1)
Stas Bekman [stason.org] of
Re:pop3 server (Score:1)
like qpsmtpd [develooper.com], i think a pop3/imap server written in perl would simply be accessible for hacking and experimentation.
mandrill [trainedmonkey.com] was my start at writing something like an imap server. (not really a true server, but something meant to be run using mutt's tunnel feature.)
but the imap spec bites. you really don't want to write an imap server.