|
3
|
Programación / Programación Visual Basic / Re: [Reto] UrlEncode y UrlDecode
|
en: 21 Diciembre 2012, 13:42 pm
|
Bueno ahi esta con APIs, lo unico que vi que no es igual a lo que pedis es que las barras en los parametros no las codifica... no se, en teoria codifica lo necesario segun M$. '--------------------------------------------------------------------------------------- ' Module : mUrlEncode ' DateTime : 21/12/2012 - Fin del Mundo! ' Author : Cobein ' Mail : cobein27@hotmail.com ' Purpose : Encode and Decode url parameters ' Requirements: None ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '--------------------------------------------------------------------------------------- Option Explicit Private Const ICU_ESCAPE As Long = &H80000000 Private Const ICU_DECODE As Long = &H10000000 Private Const CP_UTF8 As Long = 65001 Private Const ICU_BROWSER_MODE As Long = &H2000000 Private Type URL_COMPONENTS StructSize As Long Scheme As String SchemeLength As Long nScheme As Long HostName As String HostNameLength As Long nPort As Long UserName As String UserNameLength As Long Password As String PasswordLength As Long URLPath As String UrlPathLength As Long ExtraInfo As String ExtraInfoLength As Long End Type Private Declare Function InternetCrackUrl Lib "wininet.dll" Alias "InternetCrackUrlA" (ByVal lpszUrl As String, ByVal dwUrlLength As Long, ByVal dwFlags As Long, lpUrlComponents As URL_COMPONENTS) As Long Private Declare Function InternetCanonicalizeUrl Lib "wininet.dll" Alias "InternetCanonicalizeUrlA" (ByVal lpszUrl As String, ByVal lpszBuffer As String, lpdwBufferLength As Long, ByVal dwFlags As Long) As Long Private Declare Function InternetCreateUrl Lib "wininet.dll" Alias "InternetCreateUrlA" (lpUrlComponents As URL_COMPONENTS, ByVal dwFlags As Long, ByVal lpszUrl As String, lpdwUrlLength As Long) As Long Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long Public Function URLDecode(ByVal sURL As String, _ Optional ByVal bEncodeSpace As Boolean = False, _ Optional ByVal bUTF8 As Boolean = True) As String Dim tURL_COMPONENTS As URL_COMPONENTS Call CrackUrl(sURL, tURL_COMPONENTS) If bEncodeSpace Then tURL_COMPONENTS.ExtraInfo = Replace(tURL_COMPONENTS.ExtraInfo, "+", " ") End If URLDecode = CreateUrl(tURL_COMPONENTS, False) If bUTF8 Then URLDecode = UTF82Unicode(URLDecode) End If End Function Public Function URLEncode(ByVal sURL As String, _ Optional ByVal bEncodeSpace As Boolean = False, _ Optional ByVal bUTF8 As Boolean = True) As String Dim tURL_COMPONENTS As URL_COMPONENTS If bUTF8 Then sURL = Unicode2UTF8(sURL) End If Call CrackUrl(sURL, tURL_COMPONENTS) URLEncode = CreateUrl(tURL_COMPONENTS, True) If bEncodeSpace Then URLEncode = Replace(URLEncode, "%20", "+") End If End Function Private Function CreateUrl(ByRef tURL_COMPONENTS As URL_COMPONENTS, ByVal bEscape As Boolean) As String Dim sBuffer As String sBuffer = String$(2048, 0) tURL_COMPONENTS.StructSize = Len(tURL_COMPONENTS) If InternetCreateUrl(tURL_COMPONENTS, IIf(bEscape, ICU_ESCAPE, 0), sBuffer, 2048) Then CreateUrl = Left$(sBuffer, lstrlen(sBuffer)) End If End Function Private Sub CrackUrl(ByVal sURL As String, ByRef tURL_COMPONENTS As URL_COMPONENTS) Dim sBuffer As String Dim lSize As Long lSize = 2048 sBuffer = Space$(lSize) If InternetCanonicalizeUrl(sURL, sBuffer, lSize, ICU_BROWSER_MODE) Then sURL = Left$(sBuffer, lstrlen(sBuffer)) With tURL_COMPONENTS .StructSize = Len(tURL_COMPONENTS) .Scheme = Space$(lSize) .SchemeLength = lSize .HostName = Space$(lSize) .HostNameLength = lSize .UserName = Space$(lSize) .UserNameLength = lSize .Password = Space$(lSize) .PasswordLength = lSize .URLPath = Space$(lSize) .UrlPathLength = lSize .ExtraInfo = Space$(lSize) .ExtraInfoLength = lSize End With Call InternetCrackUrl(sURL, Len(sURL), ICU_DECODE, tURL_COMPONENTS) End If End Sub Private Function UTF82Unicode(ByVal sData As String) As String Dim lRet As Long Dim sBuffer As String sBuffer = Space(Len(sData)) lRet = MultiByteToWideChar(CP_UTF8, 0, _ StrPtr(StrConv(sData, vbFromUnicode)), Len(sData), _ StrPtr(sBuffer), Len(sData)) If lRet Then UTF82Unicode = Left$(sBuffer, lRet) End If End Function Private Function Unicode2UTF8(ByVal sData As String) As String Dim lRet As Long Dim sBuffer As String sBuffer = Space(LenB(sData)) lRet = WideCharToMultiByte(CP_UTF8, 0, _ StrPtr(sData), Len(sData), _ StrPtr(sBuffer), Len(sBuffer), _ vbNullString, 0) If lRet Then sBuffer = StrConv(sBuffer, vbUnicode) Unicode2UTF8 = Left$(sBuffer, lRet) End If End Function
|
|
|
4
|
Programación / Programación Visual Basic / Re: [Reto] UrlEncode y UrlDecode
|
en: 19 Diciembre 2012, 14:27 pm
|
Leandro aca arme un funcion que replica lo que hace la web que posteaste. Las funciones para UTF8 las saque de tu code. Hay que limpiar ese code porque quedo medio desastre pero bueno es simplemente a modo de ejemplo. Nota: los caracteres reservados los saque de http://en.wikipedia.org/wiki/Percent-encodingOption Explicit
Private Declare Function WideCharToMultiByte Lib "KERNEL32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long Private Declare Function MultiByteToWideChar Lib "KERNEL32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long Private Const CP_UTF8 As Long = 65001
Private Sub Form_Load() Debug.Print (EncodeURL("http://www.taringa.net/buscar/?q=día 12/12/12&interval=")) End Sub
Private Function EncodeURL(ByVal sURL As String) As String Dim bvData() As Byte Dim i As Long Dim sChar As String * 1 bvData = Unicode2UTF8(sURL) For i = 0 To UBound(bvData) Step 2 sChar = Chr$(bvData(i)) Select Case sChar Case "a" To "z", "A" To "Z", "0" To "9", "-", "_", ".", "~" EncodeURL = EncodeURL & sChar Case Else EncodeURL = EncodeURL & "%" & Right$("0" & Hex(Asc(sChar)), 2) End Select Next End Function
Private Function DecodeURL(ByVal sURL As String) As String Dim bvData() As Byte Dim i As Long Dim sChar As String * 1 bvData = sURL For i = 0 To UBound(bvData) Step 2 sChar = Chr$(bvData(i)) If sChar = "%" Then DecodeURL = DecodeURL & Chr$(Val("&h" & Chr$(bvData(i + 2)) & Chr$(bvData(i + 4)))) i = i + 4 Else DecodeURL = DecodeURL & sChar End If Next DecodeURL = UTF82Unicode(DecodeURL) End Function
Private Function UTF82Unicode(ByVal sUTF8 As String) As String
Dim UTF8Size As Long Dim BufferSize As Long Dim BufferUNI As String Dim LenUNI As Long Dim bUTF8() As Byte If LenB(sUTF8) = 0 Then Exit Function bUTF8 = StrConv(sUTF8, vbFromUnicode) UTF8Size = UBound(bUTF8) + 1 BufferSize = UTF8Size * 2 BufferUNI = String$(BufferSize, vbNullChar) LenUNI = MultiByteToWideChar(CP_UTF8, 0, bUTF8(0), UTF8Size, StrPtr(BufferUNI), BufferSize) If LenUNI Then UTF82Unicode = Left$(BufferUNI, LenUNI) End If
End Function
Private Function Unicode2UTF8(ByVal strUnicode As String) As String
Dim LenUNI As Long Dim BufferSize As Long Dim LenUTF8 As Long Dim bUTF8() As Byte LenUNI = Len(strUnicode) If LenUNI = 0 Then Exit Function BufferSize = LenUNI * 3 + 1 ReDim bUTF8(BufferSize - 1) LenUTF8 = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), LenUNI, bUTF8(0), BufferSize, vbNullString, 0) If LenUTF8 Then ReDim Preserve bUTF8(LenUTF8 - 1) Unicode2UTF8 = StrConv(bUTF8, vbUnicode) End If
End Function
|
|
|
8
|
Programación / Programación Visual Basic / Re: Tamaño maximo de archivo.
|
en: 30 Diciembre 2011, 03:00 am
|
En realidad tenes 2 problemas primero el de2gb que se presenta porque el long tiene signo y pasado cierto size el valor se vuelve negativo, el otro problema viene cuando el size supera el long y ahi se trabaja con con un complemento el segundo parametro de GetFileSize, ambos problemas entan resueltos en diferentes clases como dijo seba el post anterior.
|
|
|
10
|
Programación / Programación Visual Basic / Re: How to remove types
|
en: 20 Noviembre 2011, 23:26 pm
|
The types are just memory spaces, you can replace them with byte/long arrays or just allocate your own memory, then when you need to address any of the parameters you calculate the offset in the type structure. Eg:
type Test longdata1 as long longdata2 as long end type
there you have an eight byte size structure, 4 bytes each long, so when you need to address any of them you simply calculate the offset, so longdata2 will be placed at 4 bytes from the bigging of the memory.
|
|
|
|
|
|
|