home *** CD-ROM | disk | FTP | other *** search
- /********************************************
- * Decode 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 decodes %xx encodings in
- * form args and URLs passed as strings to it.
- *
- * 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 "AEVTaevtdURL", indicating that this is
- * an AppleEvent, class aevt, code dURL.
- *********************************************/
-
- #include <AppleEvents.h>
-
- /*********************************************/
-
- unsigned char HexChar(unsigned char c)
- {
- unsigned char k;
- /*upper case the character*/
-
- if (c>='a' && c<='z') c = (c-'a')+'A';
- k=0;
- if (c>='0' && c<='9')
- k = c - '0';
- else if (c>='A' && c<='Z')
- k = (c-'A') + (unsigned char) 10;
- return k;
- }
-
- /*********************************************/
-
- pascal OSErr main( AppleEvent *theAEEvent,
- AppleEvent *theReply,
- long *theRefCon)
-
- {
-
- /* Function Prototypes */
-
- /* variables */
- OSErr theErr = noErr;
- DescType typeCode;
- Size sizeOfParam,
- actualSize;
- char *url;
- int i;
- 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;
-
- /*decode the %xx encodings*/
- while ( url[i] ) {
- while ( url[i] ) {
- if (url [i]=='%' && i+2<actualSize) {
- bite = (HexChar(url[i+1])*16 + HexChar(url[i+2])) & 0x7F;
- url [i] = bite;
- BlockMove (&url[i+3], &url[i+1], actualSize - (i+2));
- actualSize -= 2;
- }
- i++;
- }
- }
- }
- /*return the result of any processing to the caller*/
- if (theReply->descriptorType != typeNull)
- theErr = AEPutParamPtr(theReply, keyDirectObject, typeChar,
- url, actualSize );
- DisposPtr (url);
- }
- else return errAEEventNotHandled;
- }
- else // Wasn't a string so bail
- return errAEEventNotHandled;
- }
-
- return theErr;
- }
-