How do I append bytes in memory?

Just starting out? Need help? Post your questions and find answers here.
DARKGirl
User
User
Posts: 61
Joined: Wed Nov 10, 2004 10:32 pm

How do I append bytes in memory?

Post by DARKGirl »

Hello and thank you for reading. I begin with this code:

Code: Select all

      *Buffer = AllocateMemory(1024)
      bytes_read.l
      Repeat 
        Delay(10)
        InternetReadFile_(HOR_Handle, *Buffer, 1024, @bytes_read) 
      Until bytes_read=0 
That code will read all the data from the website and put it in the *buffer until bytes_read = 0 (there is no more data to be read from the catch).

What I need to do is find a way to put the byte fragments of *buffer together into a single *pointer so i can use CatchImage(#Image, @Appended_Buffer).

I have tried everything I know from this:

Code: Select all

    *Buffer = AllocateMemory(1024)
    bytes_read.l
    *RealBuffer = 0
    old_bytes_read.l = 0
    Repeat 
      Delay(10)
      InternetReadFile_(HOR_Handle, *Buffer, 1024, @bytes_read) 
      
      If old_bytes_read = 0
        *Temp_Buffer = 0
      Else
        *Temp_Buffer = AllocateMemory(old_bytes_read)
      EndIf
      
      If *RealBuffer <> 0
          CopyMemory(*RealBuffer, *Temp_Buffer, old_bytes_read)
          FreeMemory(*RealBuffer)
      EndIf
        
      *RealBuffer = AllocateMemory(bytes_read + old_bytes_read)
      old_bytes_read = bytes_read
      CopyMemory(*Buffer, @RealBuffer, bytes_read)
      FreeMemory(*Temp_Buffer)
      FreeMemory(*Buffer)
      *Buffer = AllocateMemory(1024)
    Until bytes_read=0 
To this:

Code: Select all

      *Buffer = AllocateMemory(1024)
      bytes_read.l
      total_bytes = 0
      Repeat 
        Delay(10)
        InternetReadFile_(HOR_Handle, *Buffer, 1024, @bytes_read) 
        *total = AllocateMemory( total_bytes+bytes_read )
        CopyMemory( @Buffer, (@*total)+bytes_read, bytes_read ) 
        total_bytes + bytes_read
        FreeMemory(*total)
      Until bytes_read=0 
This too: :x

Code: Select all

      *Buffer = AllocateMemory(1024)
      bytes_read.l
      *RealBuffer = 0
      total_bytes.l = 0
      Repeat 
        Delay(10)
        InternetReadFile_(HOR_Handle, *Buffer, 1024, @bytes_read) 
        strinf.s = PeekS(*Buffer)
        Debug stringf.s
        Stringl.s + stringf.s
              Debug Stringl.s     
      Until bytes_read=0 
      *Buffer = Val(Stringl.s)
And finally this:

Code: Select all

    *Buffer = AllocateMemory(1024)
    bytes_read.l
    *RealBuffer = 0
    total_bytes.l = 0
    Repeat 
      Delay(10)
      InternetReadFile_(HOR_Handle, *Buffer, 1024, @bytes_read) 
      *RealBuffer = ReAllocateMemory(*RealBuffer, total_bytes + bytes_read)
      CopyMemory(*Buffer, @RealBuffer, bytes_read)
      total_bytes + bytes_read
    Until bytes_read=0 
Mind you, NONE of the above code snipplets work. I have been at this for 2 days with no results! I am starting to think there is no way to put bytes of data together in memory. If you are already thinking about suggesting to write the data to a file, that is out of the question, I want to work directly in memory. PLEASE, anyone..If you know how to accomplish what I am doing, tell me what I am doing wrong.

Thanks in Advance,
DARKGirl
Andras
User
User
Posts: 34
Joined: Wed Oct 27, 2004 6:58 am
Location: Germany

Post by Andras »

You can simply "shift" the pointer (*Buffer+total_bytes) and directly read to that memoryposition...

This should work (haven't tested it):

Code: Select all

    *Buffer = AllocateMemory(1024) 
    bytes_read.l 
    total_bytes.l = 0 
    Repeat 
      Delay(10) 
      InternetReadFile_(HOR_Handle, *Buffer+total_bytes, 1024, @bytes_read) 
      total_bytes + bytes_read 
      *Buffer = ReAllocateMemory(*Buffer, total_bytes + 1024) 
    Until bytes_read=0 

    *Buffer=ReAllocateMemory(*Buffer, total_bytes)
Undank ist der Welten Lohn
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

This works for me (as I told you ;)

Code: Select all

#INTERNET_FLAG_RELOAD = $80000000
#INTERNET_DEFAULT_HTTP_PORT = 80
#INTERNET_SERVICE_HTTP = 3
#HTTP_QUERY_CONTENT_LENGTH = 5
#HTTP_QUERY_STATUS_CODE = 19
#HTTP_QUERY_FLAG_NUMBER = $20000000
#HTTP_STATUS_OK = 200
#INTERNET_OPEN_TYPE_DIRECT = 1
domain$ = "www.purebasic.com"
hInet = InternetOpen_("Test", #INTERNET_OPEN_TYPE_DIRECT, #NULL, #NULL, 0)
If hInet=0:MessageRequester("Error", "Internet connection not available."):End:EndIf
hURL = InternetOpenUrl_(hInet, "http://"+domain$+"/update/GadgetExtension", #NULL, 0, #INTERNET_FLAG_RELOAD, 0)
If hURL=0:MessageRequester("Error", "URL not available"):End:EndIf
hInetCon = InternetConnect_(hInet, domain$, #INTERNET_DEFAULT_HTTP_PORT, #NULL, #NULL, #INTERNET_SERVICE_HTTP, 0, 0)
If hInetCon=0:MessageRequester("Error", "Unable To connect To "+domain$):EndIf
hHttpOpenRequest = HttpOpenRequest_(hInetCon, "HEAD", "update/GadgetExtension", "http/1.0", #NULL, 0, #INTERNET_FLAG_RELOAD, 0)
If hHttpOpenRequest=0:MessageRequester("Error", "Http open request to "+domain$+" failed"):End:EndIf
If HttpSendRequest_(hHttpOpenRequest, #NULL, 0, 0, 0)=0:MessageRequester("Error", "Http send request to "+domain$+" failed."):End:EndIf
datalength = 12:ReturnValue$ = Space(datalength)
If HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_STATUS_CODE, @ReturnValue$, @datalength, 0)=0:MessageRequester("Error", "Status code query failed."):End:EndIf
If Val(ReturnValue$)<>#HTTP_STATUS_OK:MessageRequester("Error", "Bad status code."):End:EndIf
datalength = 12:ReturnValue$ = Space(datalength)
If HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_CONTENT_LENGTH, @ReturnValue$, @datalength, 0)=0:MessageRequester("Error", "Content length query failed."):End:EndIf
ContentLength = Val(ReturnValue$)
Debug domain$+"/update/GadgetExtension"
Debug "Total: "+Str(ContentLength)+" bytes"
*Buffer = AllocateMemory(ContentLength)
*BufferStart = *Buffer
Repeat
  If InternetReadFile_(hURL, *Buffer, 4096, @Bytes)=0:MessageRequester("Error", "Download failed."):End:EndIf
  *Buffer+Bytes
  Debug "Total bytes read: "+Str(*Buffer-*BufferStart)
Until Bytes=0
InternetCloseHandle_(hURL)
InternetCloseHandle_(hInetCon)
InternetCloseHandle_(hInet)
El_Choni
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

El_Choni wrote:This works for me (as I told you ;)

Code: Select all

#INTERNET_FLAG_RELOAD = $80000000
#INTERNET_DEFAULT_HTTP_PORT = 80
#INTERNET_SERVICE_HTTP = 3
#HTTP_QUERY_CONTENT_LENGTH = 5
#HTTP_QUERY_STATUS_CODE = 19
#HTTP_QUERY_FLAG_NUMBER = $20000000
#HTTP_STATUS_OK = 200
#INTERNET_OPEN_TYPE_DIRECT = 1
domain$ = "www.purebasic.com"
hInet = InternetOpen_("Test", #INTERNET_OPEN_TYPE_DIRECT, #NULL, #NULL, 0)
If hInet=0:MessageRequester("Error", "Internet connection not available."):End:EndIf
hURL = InternetOpenUrl_(hInet, "http://"+domain$+"/update/GadgetExtension", #NULL, 0, #INTERNET_FLAG_RELOAD, 0)
If hURL=0:MessageRequester("Error", "URL not available"):End:EndIf
hInetCon = InternetConnect_(hInet, domain$, #INTERNET_DEFAULT_HTTP_PORT, #NULL, #NULL, #INTERNET_SERVICE_HTTP, 0, 0)
If hInetCon=0:MessageRequester("Error", "Unable To connect To "+domain$):EndIf
hHttpOpenRequest = HttpOpenRequest_(hInetCon, "HEAD", "update/GadgetExtension", "http/1.0", #NULL, 0, #INTERNET_FLAG_RELOAD, 0)
If hHttpOpenRequest=0:MessageRequester("Error", "Http open request to "+domain$+" failed"):End:EndIf
If HttpSendRequest_(hHttpOpenRequest, #NULL, 0, 0, 0)=0:MessageRequester("Error", "Http send request to "+domain$+" failed."):End:EndIf
datalength = 12:ReturnValue$ = Space(datalength)
If HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_STATUS_CODE, @ReturnValue$, @datalength, 0)=0:MessageRequester("Error", "Status code query failed."):End:EndIf
If Val(ReturnValue$)<>#HTTP_STATUS_OK:MessageRequester("Error", "Bad status code."):End:EndIf
datalength = 12:ReturnValue$ = Space(datalength)
If HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_CONTENT_LENGTH, @ReturnValue$, @datalength, 0)=0:MessageRequester("Error", "Content length query failed."):End:EndIf
ContentLength = Val(ReturnValue$)
Debug domain$+"/update/GadgetExtension"
Debug "Total: "+Str(ContentLength)+" bytes"
*Buffer = AllocateMemory(ContentLength)
*BufferStart = *Buffer
Repeat
  If InternetReadFile_(hURL, *Buffer, 4096, @Bytes)=0:MessageRequester("Error", "Download failed."):End:EndIf
  *Buffer+Bytes
  Debug "Total bytes read: "+Str(*Buffer-*BufferStart)
Until Bytes=0
InternetCloseHandle_(hURL)
InternetCloseHandle_(hInetCon)
InternetCloseHandle_(hInet)
Ufffff! That is what I call an absolutly unreadable source. How do you keep an overview on it? ;o)
Tranquil
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

You know what they say about unreadable code... ;)

Anyway, there are previous examples in the forum using the same approach, you don't have to use this one.
El_Choni
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

El_Choni wrote:You know what they say about unreadable code... ;)
Hm, not realy. Maybe: Coders of unreadable sources have got a long penis? Or something similar?

Mike
Tranquil
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Tranquil wrote:
El_Choni wrote:You know what they say about unreadable code... ;)
Hm, not realy. Maybe: Coders of unreadable sources have got a long penis? Or something similar?

Mike
So THAT was the reason! :mrgreen:
El_Choni
Post Reply