CocoaDev

Edit AllPages

I thought it would be interesting to write a CGI application in Objective C. However, I was unable to find an Objective C class for handling CGI parameters so I wrote one myself.

You just create a Foundation tool and then work your way from the main function using the class. For example, the following code will print out the name/value pairs passed to the CGI program:

#import <Foundation/Foundation.h> #import “ObjectiveCGI.h”

int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// Create the CGI object
ObjectiveCGI *cgi = [[ObjectiveCGI alloc] init];

NSDictionary *params = [cgi getParams];
NSEnumerator *enumerator = [params keyEnumerator];
id key;

printf("Content-type: text/plain\n\n");

// Print out each key value pair as plain text
while ((key = [enumerator nextObject])) 
{
    printf("%s=%s",[key cString], 
                   params objectForKey: key] cString]);  
}
    
// Release the CGI object
[cgi release];

[pool release];
return 0; }

I’ve written some CGI programs, such as Vox Machina CGI (http://voxmachina.sytes.net) and [[VirtualSafari (http://virtualsafari.sytes.net) using this class. And here’s the code to the CGI class itself.

#import “ObjectiveCGI.h”

@implementation ObjectiveCGI

@end