home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Unique_Too20380012162006.psc / PlanetSourceCodeSearch_vb / clsCoder.cls < prev    next >
Text File  |  2005-04-27  |  7KB  |  207 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsCoder"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. ' ***********************************************************************
  15. '
  16. ' CLASS : clsCoder.cls
  17. '
  18. ' PURPOSE : Provide access to the URL Coding / Decoding routines
  19. '
  20. ' WRITTEN BY : Alon Hirsch
  21. '
  22. ' COMPANY : Debtpack (Pty) Ltd. - Development
  23. '
  24. ' DATE : 11 February 2002
  25. '
  26. ' ***********************************************************************
  27. Option Explicit
  28. DefInt A-Z
  29.  
  30. ' characters allowed in a URL without needing to be encoded
  31. Private Const URLValid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  32.  
  33. Public Function sURLEncode(ByVal sWork As String) As String
  34.     ' This function will URLEncode sWork and return it as the value of the function
  35.     Dim iLoop As Integer
  36.     Dim iLen As Integer
  37.     Dim sRet As String
  38.     Dim sTemp As String
  39.     
  40.     ' prepare the result string
  41.     sRet = ""
  42.  
  43.     ' check if we have a string to work with
  44.     If Len(sWork) > 0 Then
  45.         ' we do - determine the length of the string
  46.         iLen = Len(sWork)
  47.         ' check all the characters (one by one)
  48.         For iLoop = 1 To iLen
  49.             ' check each character in turn
  50.             ' get the next character
  51.             sTemp = Mid$(sWork, iLoop, 1)
  52.             ' is the character a valid one or not
  53.             If InStr(1, URLValid, sTemp, vbBinaryCompare) = 0 Then
  54.                 If sTemp = Chr$(32) Then
  55.                     ' convert space to +
  56.                     sTemp = "+"
  57.                 Else
  58.                     ' not valid - use HEX representation of it
  59.                     sTemp = "%" & Right$("0" & Hex(Asc(sTemp)), 2)
  60.                 End If
  61.             End If
  62.             ' add this to the returned string
  63.             sRet = sRet & sTemp
  64.         Next iLoop
  65.         ' return the final result
  66.         sURLEncode = sRet
  67.     End If
  68. End Function
  69. Public Function sURLDecodeB(ByVal sWork As String) As String
  70.     ' This function will scan through the entire sWork and replace all valid
  71.     ' URL Encoded character with their ASCII character value and then return the
  72.     Dim sTemp As String
  73.     Dim sChar As String
  74.     Dim sNewString As String
  75.     Dim lPos1 As Long
  76.     Dim lLen As Long
  77.     Dim lChar As Long
  78.     
  79.     ' prepare the result string
  80.     sNewString = ""
  81.     
  82.     ' determine the lengh of the data to process
  83.     lLen = Len(sWork)
  84.     
  85.     ' loop through each character (NOT BYTE)
  86.     For lChar = 1 To lLen
  87.         ' retrieve the character
  88.         sChar = Mid$(sWork, lChar, 1)
  89.         ' now examine the character
  90.         If sChar = "%" Then
  91.             ' encoded character - decode the next 2 characters
  92.             sTemp = Mid$(sWork, lChar + 1, 2)
  93.             sNewString = sNewString & ChrB$("&H" & sTemp)
  94.             ' increment counter to skip the encoded value
  95.             lChar = lChar + 2
  96.         ElseIf sChar = "+" Then
  97.             ' is a space - decode it
  98.             sNewString = sNewString & ChrB$(32)
  99.         Else
  100.             ' not decoded - use it as is
  101.             sNewString = sNewString & ChrB$(AscB(sChar))
  102.         End If
  103.     Next lChar
  104.     
  105.     ' return the new string to the calling process
  106.     sURLDecodeB = sNewString
  107. End Function
  108. Public Function sURLDecode(ByVal sWork As String) As String
  109.     ' This function will scan through the entire sWork and replace all valid
  110.     ' URL Encoded character with their ASCII character value
  111.     Dim sTemp As String
  112.     Dim sChar As String
  113.     Dim lPos1 As Long
  114.     Dim lPos2 As Long
  115.     Dim lChar As Long
  116.     Dim bFirst As Boolean
  117.     
  118.     ' start with an empty string
  119.     sTemp = ""
  120.     lPos2 = 1
  121.     bFirst = True
  122.     
  123.     ' start by replacing all + with spaces
  124.     sWork = Replace(sWork, "+", Chr$(32))
  125.     
  126.     ' *** now handle the actuall encoded stuff
  127.     ' find the first occurrence
  128.     lPos1 = InStr(1, sWork, "%", vbTextCompare)
  129.     If lPos1 = 0 Then
  130.         ' none found - return the entire string
  131.         sTemp = sWork
  132.     Else
  133.         ' check as long as there are still encoeded characters.
  134.         Do While lPos1 <> 0
  135.             ' find the first %
  136.             ' check if we found one or not
  137.             If lPos1 <> 0 Then
  138.                 ' we found 1 - decode it and add it to the result
  139.                 If bFirst Then
  140.                     ' this is the first time in - stemp is all data up to the first %
  141.                     sTemp = Left$(sWork, lPos1 - 1)
  142.                     bFirst = False
  143.                 Else
  144.                     ' add all the data from the last position to the current position
  145.                     sTemp = sTemp & Mid$(sWork, lPos2 + 2, (lPos1 - lPos2 - 2))
  146.                 End If
  147.                 sChar = Mid$(sWork, lPos1 + 1, 2)
  148.                 lChar = CLng("&H" & sChar)
  149.                 sTemp = sTemp & Chr$(lChar)
  150.                 ' start at the next position
  151.                 lPos2 = lPos1 + 1
  152.             End If
  153.             
  154.             ' check for the next one
  155.             lPos1 = InStr(lPos2, sWork, "%", vbTextCompare)
  156.             If lPos1 = 0 Then
  157.                 ' no more - add the rest of the string to be checked
  158.                 sTemp = sTemp & Mid$(sWork, lPos2 + 2)
  159.             End If
  160.         Loop
  161.     End If
  162.     ' return the string we have decoded
  163.     sURLDecode = sTemp
  164. End Function
  165. Public Function sURLEncodeB(ByVal sWork As String) As String
  166.     ' This function will URLEncodeB sWork and return it as the value of the function
  167.     ' This performs a BYTE-WISE encoding
  168.     Dim iLoop As Integer
  169.     Dim iLen As Integer
  170.     Dim iTemp As Integer
  171.     Dim sRet As String
  172.     Dim sTemp As String
  173.     Dim bTemp As Byte
  174.     
  175.     ' prepare the result string
  176.     sRet = ""
  177.  
  178.     ' check if we have a string to work with
  179.     If LenB(sWork) > 0 Then
  180.         ' we do - determine the length of the string
  181.         iLen = LenB(sWork)
  182.         ' check all the characters (one by one)
  183.         For iLoop = 1 To iLen
  184.             ' check each character in turn
  185.             ' get the next character
  186.             iTemp = AscB(MidB$(sWork, iLoop, 1))
  187.             ' is the character a valid one or not
  188.             If (iTemp < 65 Or iTemp > 90) And (iTemp < 97 Or iTemp > 122) Then
  189.                 'If sTemp = Chr$(32) Then
  190.                 If iTemp = 32 Then
  191.                     ' convert space to +
  192.                     sTemp = "+"
  193.                 Else
  194.                     ' not valid - use HEX representation of it
  195.                     sTemp = "%" & Right$("0" & Hex(iTemp), 2)
  196.                 End If
  197.             Else
  198.                 sTemp = Chr$(iTemp)
  199.             End If
  200.             ' add this to the returned string
  201.             sRet = sRet & sTemp
  202.         Next iLoop
  203.         ' return the final result
  204.         sURLEncodeB = sRet
  205.     End If
  206. End Function
  207.