Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: Miseryk en 1 Julio 2014, 20:08 pm



Título: [APORTE] ReadIni Memoria
Publicado por: Miseryk en 1 Julio 2014, 20:08 pm
Bueno, estaba trabajando con archivos en memoria, y no quería guardarlos en directorios temporales o cosas así, así que hice una función que simula a GetPrivateProfileString.

PD: solamente hice en modo lectura, ya que des-en-crip-to un archivo y ni me interesa modifcarlo desde ahí.

Código
  1. Public Function MiseryReadKey(Cadena As String, Section As String, Key As String) As String
  2. 'BreakLine = Enter o ;
  3.  
  4. Dim FirstPos As Long, LastPos As Long, FitPos As Long
  5. Dim FinalStr As String
  6.  
  7. Section = UCase(Section)
  8. Key = UCase(Key)
  9.  
  10. '[Section]
  11. FirstPos = InStr(1, UCase(Cadena), "[" & Section & "]")
  12.  
  13. If FirstPos < 1 Then
  14.    MiseryReadKey = ""
  15.    Exit Function
  16. End If
  17.  
  18. FirstPos = FirstPos + Len("[" & Section & "]")
  19.  
  20. LastPos = InStr(FirstPos, UCase(Cadena), "[") - 1
  21.  
  22. 'Patch, si está al final no ván a haber más "["
  23. If LastPos < 1 Then
  24.    LastPos = Len(Cadena) + 1
  25. End If
  26.  
  27. FinalStr = Mid(Cadena, FirstPos, LastPos - FirstPos)
  28.  
  29. 'Key
  30. FirstPos = InStr(1, UCase(FinalStr), Key)
  31.  
  32. If FirstPos < 1 Then
  33.    MiseryReadKey = ""
  34.    Exit Function
  35. End If
  36.  
  37. LastPos = InStr(FirstPos, FinalStr, Chr(13)) - 1 'Patch 07/07/2014
  38.  
  39. 'Patch, lo mismo acá, no ván a haber más enters si lée el último
  40. If LastPos < 1 Then
  41.    LastPos = Len(FinalStr) + 1
  42. End If
  43.  
  44. 'Hay un comentario
  45. FitPos = InStr(FirstPos, FinalStr, ";")
  46. If FitPos > 0 Then
  47.    If FitPos < LastPos Then
  48.        LastPos = FitPos - 1
  49.    End If
  50. End If
  51.  
  52. '=
  53. FirstPos = InStr(FirstPos, FinalStr, "=")
  54.  
  55. If FirstPos < 1 Then
  56.    MiseryReadKey = ""
  57.    Exit Function
  58. End If
  59.  
  60. MiseryReadKey = Trim(Mid(FinalStr, FirstPos + 1, LastPos - FirstPos))
  61. End Function
  62.  

Código
  1. MsgBox MiseryReadKey(txtFile.Text, "NUMERO1", "Val1")
  2.  

El FitPos es para el enter o ;

Archivo:
Código
  1. [NUMERO1]
  2. Val1=333 ;asd
  3. Val2=666;asd
  4.  

Saludos!


Título: Re: [APORTE] ReadIni Memoria
Publicado por: 79137913 en 2 Julio 2014, 17:40 pm
HOLA!!!

Soporta Unicode?

P.D: muy buen aporte!

GRACIAS POR LEER!!!


Título: Re: [APORTE] ReadIni Memoria
Publicado por: engel lex en 2 Julio 2014, 19:57 pm
des-en-crip-to

descifro*

:P el foro está forzado para que la gente use la palabra correcta XD


Título: Re: [APORTE] ReadIni Memoria
Publicado por: Miseryk en 3 Julio 2014, 20:55 pm
HOLA!!!

Soporta Unicode?

P.D: muy buen aporte!

GRACIAS POR LEER!!!

Gracias (Y)

No, solamente soporta texto en string, textbox y cosas así, los cuales usan ANSI, estoy en lo correcto o estoy hablando boludeces?

descifro*

:P el foro está forzado para que la gente use la palabra correcta XD

Ah, no sabía, pensé que era para que google u otras páginas no tomen a foro.elhacker como spam o con virus.

Edit:
Aunque busca hasta enters o ; y lo guarda a string, desconozco si realmente guarda bien los bytes ahí, pero debería probarlo.


Título: Re: [APORTE] ReadIni Memoria
Publicado por: 79137913 en 4 Julio 2014, 18:39 pm
HOLA!!!

Me imagino, supongo que no debe soportar, intenta meter un archivo binario en memoria y volver a volcarlo, si funciona soporta unicode sino no.

GRACIAS POR LEER!!!


Título: Re: [APORTE] ReadIni Memoria
Publicado por: Miseryk en 8 Julio 2014, 22:21 pm
Patch:

Código
  1. Public Function MiseryReadKey(Cadena As String, Section As String, key As String) As String
  2. 'BreakLine = Enter o ;
  3.  
  4. Dim FirstPos As Long, LastPos As Long, FitPos As Long
  5. Dim FinalStr As String
  6.  
  7. Section = UCase(Section)
  8. key = UCase(key & "=") 'Patch 08/07/2014
  9.  
  10. '[Section]
  11. FirstPos = InStr(1, UCase(Cadena), "[" & Section & "]")
  12.  
  13. If FirstPos < 1 Then
  14.    MiseryReadKey = ""
  15.    Exit Function
  16. End If
  17.  
  18. FirstPos = FirstPos + Len("[" & Section & "]")
  19.  
  20. LastPos = InStr(FirstPos, UCase(Cadena), "[") - 1
  21.  
  22. 'Patch, si está al final no ván a haber más "["
  23. If LastPos < 1 Then
  24.    LastPos = Len(Cadena) + 1
  25. End If
  26.  
  27. FinalStr = mid(Cadena, FirstPos, LastPos - FirstPos)
  28.  
  29. 'Key
  30. FirstPos = InStr(1, UCase(FinalStr), key)
  31.  
  32. If FirstPos < 1 Then
  33.    MiseryReadKey = ""
  34.    Exit Function
  35. End If
  36.  
  37. LastPos = InStr(FirstPos, FinalStr, Chr(13)) - 1 'Patch 07/07/2014
  38.  
  39. 'Patch, lo mismo acá, no ván a haber más enters si lée el último
  40. If LastPos < 1 Then
  41.    LastPos = Len(FinalStr) + 1
  42. End If
  43.  
  44. 'Hay un comentario
  45. FitPos = InStr(FirstPos, FinalStr, ";")
  46. If FitPos > 0 Then
  47.    If FitPos < LastPos Then
  48.        LastPos = FitPos - 1
  49.    End If
  50. End If
  51.  
  52. '=
  53. FirstPos = InStr(FirstPos, FinalStr, "=")
  54.  
  55. If FirstPos < 1 Then
  56.    MiseryReadKey = ""
  57.    Exit Function
  58. End If
  59.  
  60. MiseryReadKey = Trim(mid(FinalStr, FirstPos + 1, LastPos - FirstPos))
  61. End Function
  62.