PowerBASIC Forums
  Source Code
  CGI - UrlEncode

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   CGI - UrlEncode
Florent Heyworth
Member
posted May 13, 2000 07:50 PM     Click Here to See the Profile for Florent Heyworth     Edit/Delete Message   Reply w/Quote
This function is useful when passing URL query strings which might need to be encoded to preserve them. Corresponds to the
'application/x-www-form-urlencoded' media type


FUNCTION UrlEncode( BYVAL sToEscape AS STRING ) AS STRING
'formats a string to comply with CGI application/x-www-form-urlencoded
'media type. Useful in CGI to be used as a query part or to pass
'variables around
REGISTER lIterIn AS LONG
REGISTER lIterOut AS LONG
LOCAL lLength AS LONG
LOCAL pbByteIn AS BYTE PTR
LOCAL pbByteOut AS BYTE PTR
LOCAL pszReturnStr AS ASCIIZ PTR
LOCAL sOut AS STRING
LOCAL sHex AS STRING * 2


lLength = LEN(sToEscape)
'Allocate to len(sToEscape) * 3 + 1 (worst case scenario)
sOut = SPACE$( (lLength * 3)+1 )
pbByteIn = STRPTR( sToEscape )
pbByteOut = STRPTR( sOut )

DO UNTIL lIterIn = lLength
SELECT CASE @pbByteIn[lIterIn]
CASE 48 TO 57, 65 TO 90, 97 TO 122
@pbByteOut[lIterOut] = @pbByteIn[lIterIn]
CASE 32
@pbByteOut[lIterOut] = 43 '+
CASE ELSE
IF @pbByteIn[lIterIn] < 16 THEN
@pbByteOut[lIterOut] = 37 '%
@pbByteOut[lIterOut+1] = 48 '0
@pbByteOut[lIterOut+2] = ASC(HEX$(@pbByteIn[lIterIn]))
ELSE
@pbByteOut[lIterOut] = 37 '%
sHex = HEX$(@pbByteIn[lIterIn])
@pbByteOut[lIterOut+1] = ASC(sHex,1)
@pbByteOut[lIterOut+2] = ASC(sHex,2)
END IF
lIterOut = lIterOut + 2
END SELECT
INCR lIterIn
INCR lIterOut
LOOP

@pbByteOut[lIterOut] = 0 'terminate the string

pszReturnStr = STRPTR( sOut )

FUNCTION = @pszReturnStr

END FUNCTION

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c