packing strings in memory

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

I'm officially lost again.

I need to
1. read a string of data from a file (I know this one okay!!)
2. pack it in memory
3. retrieve the packed string data from memory and write it to disk
4. read the next line and do it again!

I don't know how to put string data into memory, pack it and retreive it for writing. Reading and writing files I do know how to do!!


Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
I'm officially lost again.

I need to
1. read a string of data from a file (I know this one okay!!)
2. pack it in memory
3. retrieve the packed string data from memory and write it to disk
4. read the next line and do it again!

I don't know how to put string data into memory, pack it and retreive it for writing. Reading and writing files I do know how to do!!


Fangles
Ok, here we go.
This is nothing i have tested but logically i should work. I'll use some pseudo code as i don't want to program your entire app :wink:

Code: Select all

memlength.l=1024
address.l=AllocateMemory(0, memlength, 0) ; reserve som memory

if readfile(0,filename.s) ; open file for reading only
  if openfile(1,packedfilename.s) ; and open the output file for writing
    repeat
      usefile(0)            ; inputfile
      in.s=readstring()     ; read a line from file
      length.w=len(in)      ; get size of string
      if length>0
        packedlength.w=PackMemory(@in, address, length) ; Crunch the string
        usefile(1)          ; outputfile
        WriteWord(packedlength) ; Good to know when unpacking
        WriteWord(length)       ; So you know how much memory to allocate when unpacking
        WriteData(address, packedlength) ; finally write your compressed string
      endif
    until eof(0) ; check if we have reached the end of the file
    CloseFile(1) ; Close output file
  endif
  CloseFile(0) ; Close input file
endif
end

 
Upps, wasn't much pseudo code there i seem to have written your program after all, sorry;)

Note about my code:
*I assumed no strings were longer than 1024-8 bytes, if the string is longer the program may crash. solution, allocate more memory.
*It's probably good coding style to check if memory allocation succeded.
*Max length of strings in this example is 65536/2-1 bytes. Solution change the allocated memory and change lengths from 'word' to 'long' and use WriteLong() instead of WriteWord().

When decompressing your packed string file do the above code in reverse!

Happy coding, and as i said i haven't actually tested the code....
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Upps, wasn't much pseudo code there i seem to have written your program after all, sorry;)
Thanks pupil, I was doing things the wrong way around, using PEEK and poke to get at strings in the buffer and wondering why it didnt't work!! I tried your example after plugging in my variables and file names and it works great. Your example is different to the packer code Fred showed us when he first put it in a library and I was trying to 'reverse engineer' his example for me and not quite succeeding.

Thanks once again.

One job down, half a million to go :)

Fangles
Post Reply