home *** CD-ROM | disk | FTP | other *** search
- /********************************************
- * Encode URL OSAX - by Chuck Shotton, 10/17/94
- * This code, the OSAX, and all of its constituent parts are hereby
- * placed in the public domain.
- *
- * This code demonstrates how to build an OSAX for use with MacHTTP
- * and AppleScript. This particular example encodes special chars
- * in form args and URLs passed as strings to it using %xx encodings.
- *
- * This code was written using Think C, but should build with no
- * problems under CodeWarrior as well. It should be build as a 68k
- * code resource to avoid problems. The code resource type is 'osax'
- * and its resource ID MUST match the event class and code.
- * For this event the name is "AEVTaevteURL", indicating that this is
- * an AppleEvent, class aevt, code eURL.
- *********************************************/
-
- #include <AppleEvents.h>
-
- /*********************************************/
-
- void CharToHex (unsigned char c, char *s)
- {
- unsigned char x;
- s[0] = '%';
- x = (c & 0xF0) >> 4;
- s[1] = x>9 ? (x-10)+'A' : x+'0';
- x = c & 0x0F;
- s[2] = x>9 ? (x-10)+'A' : x+'0';
- }
-
- /*********************************************/
-
- void EncodeChar (unsigned char c, char *s, int *j)
- {
- char *bad = " $&=+\"\\';/#?:";
- int i;
- if (c<33 || c>126) {
- CharToHex (c, &s[*j]);
- *j+=3;
- }
- else {
- for (i=0;i<14;i++)
- if (c == bad[i]) {
- CharToHex (c, &s[*j]);
- *j+=3;
- break;
- }
- if (i==14)
- s[(*j)++] = c;
- }
-
-
- }
-
- /*********************************************/
-
- pascal OSErr main( AppleEvent *theAEEvent,
- AppleEvent *theReply,
- long *theRefCon)
-
- {
-
- /* variables */
- OSErr theErr = noErr;
- DescType typeCode;
- Size sizeOfParam,
- actualSize;
- char *url, *encoded;
- int i, j;
- unsigned char bite;
-
- theErr = AESizeOfParam( theAEEvent,
- keyDirectObject,
- &typeCode,
- &sizeOfParam);
-
- if (theErr != noErr){
- return theErr;
- }
- else{
- /*try converting lots of types of strings into something we can use*/
- if ((typeCode == typeChar) || (typeCode == typeStyledText) ||
- (typeCode == typeIntlText)) {
-
- /*Make a place to store the parameter, then get it.*/
- if (url = (char *) NewPtr (sizeOfParam+2)) {
- theErr = AEGetParamPtr(theAEEvent, keyDirectObject, typeChar,
- &typeCode, (Ptr) url,
- sizeOfParam+1, &actualSize);
-
- if(theErr == noErr) {
- /* 'C' string has a null as last char */
- url [actualSize] = '\0';
- i=0;
- j = 0;
-
- /*make a buffer to hold the encoded string*/
- if (actualSize*3 > 32760)
- sizeOfParam = 32766;
- else
- sizeOfParam = actualSize*3+3;
-
- if (encoded = (char *) NewPtr (sizeOfParam)) {
- /*create the %xx encodings*/
- while ( url[i] && j<sizeOfParam-2) {
- EncodeChar (url[i++], encoded, &j);
- }
- encoded [j] = '\0';
- }
- else {
- DisposPtr (url);
- return errAEEventNotHandled;
- }
- }
- /*return the result of any processing to the caller*/
- if (theReply->descriptorType != typeNull)
- theErr = AEPutParamPtr(theReply, keyDirectObject, typeChar,
- encoded, j );
- DisposPtr (url);
- DisposPtr (encoded);
- }
- else return errAEEventNotHandled;
- }
- else // Wasn't a string so bail
- return errAEEventNotHandled;
- }
-
- return theErr;
- }
-