Objectmix
Tags Register Mark Forums Read

cgi objective-c code : C

This is a discussion on cgi objective-c code within the C forums in Programming Languages category; enclosed is some objective-c code that is intended to end up as a word search puzzle generator but it currently has no workings other than the web cgi stuff. enclose your own workings in the appropriate places. create a foundation command-line tool and then use this as the main.m file. add the gnu getopt.c and getopt.h files to your project. let me know what you think of it... ----- clip ----- #include <stdio.h> #include <stdlib.h> #include <getopt.h> #import <Foundation/Foundation.h> #import <CoreServices/CoreServices.h> #include <AppKit/AppKit.h> // Globals NSMutableString *ip; NSMutableString *url; NSMutableString *url2; NSMutableString *counts; NSMutableArray *parameters; // cgi paramete char *line ...


Object Mix > Programming Languages > C > cgi objective-c code

C comp.lang.c usenet archive

Reply

 

LinkBack Thread Tools
  #1  
Old 08-13-2006, 05:36 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default cgi objective-c code

enclosed is some objective-c code that is intended to end up as a
word search puzzle generator but it currently has no workings other
than the web cgi stuff.

enclose your own workings in the appropriate places.
create a foundation command-line tool and then use this as the main.m file.
add the gnu getopt.c and getopt.h files to your project.

let me know what you think of it...

----- clip -----

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#import <Foundation/Foundation.h>
#import <CoreServices/CoreServices.h>
#include <AppKit/AppKit.h>

// Globals

NSMutableString *ip;
NSMutableString *url;
NSMutableString *url2;
NSMutableString *counts;
NSMutableArray *parameters; // cgi paramete
char *line = NULL;

// global arrays to hold the word search puzzle and solution
char puzzle[99][99];
char solution[99][99];

void printstring(NSMutableString *thestring)
{
line = [thestring cStringUsingEncoding: [NSString defaultCStringEncoding]];
printf(line);
};

void printArray()
{
long index = 0;
if ([parameters count] > 0)
{
for (index = 0; index < [parameters count]; index++)
{
printstring([[parameters objectAtIndex: index] objectForKey: @"parameter"]);
printf("=");
printstring([[parameters objectAtIndex: index] objectForKey: @"value"]);
printf("<br />");
}
}
}

char *cgiDecodeString (char *text)
{
char *cp, *xp;

for (cp=text,xp=text; *cp; cp++)
{
if (*cp == '%')
{
if (strchr("0123456789ABCDEFabcdef", *(cp+1))
&& strchr("0123456789ABCDEFabcdef", *(cp+2)))
{
if (islower(*(cp+1)))
*(cp+1) = toupper(*(cp+1));
if (islower(*(cp+2)))
*(cp+2) = toupper(*(cp+2));
*(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
+ (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
xp++;
cp+=2;
}
}
else
{
*(xp++) = *cp;
}
}
memset(xp, 0, cp-xp);
return text;
};

char *locateString(char *locateWhat)
{
return strstr (line, locateWhat);
};

void cgiReadVariables ()
{
long length;
char *cp, *ip, *esp;
long objectNumber;

cp = getenv("REQUEST_METHOD");
ip = getenv("CONTENT_LENGTH");
printf("REQUEST_METHOD = %s<br />\nCONTENT_LENGTH = %s<br />\n",cp,ip);

if (cp && !strcmp(cp, "POST"))
{
//printf("Request Method = POST\n");
if (ip)
{
length = atoi(ip);
if ((length > 1) && (length < 10485760))
{
if ((line = (char *)malloc (length+2)) == NULL)
{
printf("didnt malloc"),
line = NULL;
return;
}
fgets(line, length+1, stdin);
}
else
{
line = NULL;
return;
}
}
else
{
line = NULL;
return;
}
}
else if (cp && !strcmp(cp, "GET"))
{
//printf("Request Method = GET\n");
esp = getenv("QUERY_STRING");
if (esp && strlen(esp))
{
if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
{
line = NULL;
return;
}
sprintf (line, "%s", esp);
}
else
{
line = NULL;
return;
}
};
if (line)
// if (YES)
{
line = cgiDecodeString(line);

/*
* From now on all cgi variables are stored in the variable line
* and look like foo=bar&foobar=barfoo&foofoo=barbar
*/
for (cp=line; *cp; cp++)
{
if (*cp == ',')
*cp = ' ';
if (*cp == '<')
*cp = ' ';
if (*cp == '>')
*cp = ' ';
if (*cp == '+')
*cp = ' ';
if (*cp == '\r')
*cp = ' ';
*cp = tolower(*cp);
};
NSMutableString *theline = [NSMutableString stringWithCString: line];
NSMutableString *parameter = [[[NSMutableString alloc] init] autorelease];
NSMutableString *value = [[[NSMutableString alloc] init] autorelease];
long position = 0;
while (([theline length] > 0) && (position < [theline length]))
{
long loc = [theline rangeOfString: @"&" options: NSCaseInsensitiveSearch range: NSMakeRange(position,([theline length] - position))].location;
if (loc != NSNotFound)
{
[parameter setString: [NSMutableString stringWithString: [theline substringWithRange: NSMakeRange(position,loc-position)]]];
[value setString: [NSMutableString stringWithString: [theline substringWithRange: NSMakeRange(position,loc-position)]]];
position = loc + 1;
}
else
{
[parameter setString: [NSMutableString stringWithString: [theline substringWithRange: NSMakeRange(position,[theline length]-position)]]];
[value setString: [NSMutableString stringWithString: [theline substringWithRange: NSMakeRange(position,[theline length]-position)]]];
[theline setString: @""];
};
loc = [parameter rangeOfString: @"=" options: NSCaseInsensitiveSearch].location;
if (loc == NSNotFound)
{
[value setString: @""];
}
else
{
[parameter deleteCharactersInRange: NSMakeRange(loc,([parameter length]-loc))];
[value deleteCharactersInRange: NSMakeRange(0,(loc+1))];
};
[parameters addObject: [[[NSMutableDictionary alloc] init] autorelease]];
objectNumber = [parameters count]-1;
[[parameters objectAtIndex: objectNumber] setObject: [NSMutableString stringWithString: parameter] forKey: @"parameter"];
[[parameters objectAtIndex: objectNumber] setObject: [NSMutableString stringWithString: value] forKey: @"value"];
}
}
};

int main(int argc, char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
parameters = [[NSMutableArray alloc] init];


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

// insert code here...

printArray();


[parameters release];

[pool release];

return 0;
};
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Objective-C FAQ usenet C 0 10-15-2006 12:51 PM
Objective-C FAQ usenet C 0 09-14-2006 12:52 PM
Objective-C FAQ usenet C 0 11-24-2004 01:54 PM
Objective-C FAQ usenet C 0 10-25-2004 12:55 PM
Objective-C FAQ usenet C 0 01-14-2004 01:52 PM


All times are GMT -5. The time now is 08:37 AM.

Managed by Infnx Pvt Ltd.