Squares

General FreeBASIC programming questions.
Locked
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

How do you call a compression routine from inside of FB ??

I want to use the zip library

I started working on compression , to compress the digits produced by "Big Calc" , they might be a megabyte long.
You can do onetime , 50% compression , by turning every 2 digits to chr( num )

I figure i just , might as well , use a normal compression on the numbers..
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

In win 10 powershell does the job.

Code: Select all

 

#Include "file.bi"


sub compress(filein as string,fileout as string) 
shell "powershell Compress-Archive  " +curdir +"\"+ filein+ " " + fileout
end sub

sub uncompress(filein as string,newfolder as string) 
shell "powershell Expand-Archive  "  +curdir +"\"+ filein+ " " + newfolder
end sub



compress("pool.exe","cpool.zip")
print iif(fileexists("cpool.zip"),"Done","Error")

uncompress("cpool.zip","newpool")
print iif(fileexists("newpool\pool.exe"),"expanded","Error")
sleep






 
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I'm using Linux 64 bit...

FB has a zip library , I was wondering how to utilize it. How do you call it up , and pass strings to it.

I can't make heads or tails , out of the zip.bi and zlib.bi
How to call them up , and pass strings to them.. To deflate or inflate ?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

zlib1.dll comes with the 32 bit windows version 1.06

at the top write in the lib you want to use.
The .bi file is not needed, I have called the three required functions myself.
everything is in a namespace packer.

Code: Select all

 





#inclib "zlib1"
Extern "C"
Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Ulong Ptr, Byval source As Const Ubyte Ptr, Byval sourceLen As Ulong) As Long
Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Ulong Ptr, Byval source As Const Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

namespace packer
Dim shared As String file
Dim Shared As Integer f
Dim Shared As Integer passed_length
dim shared as ubyte ptr source,destination
Sub string_split(Byval s As String,chars As String,result() As String)
    Redim result(0)
    Dim As String var1,var2
    Dim As Long pst,LC=Len(chars)
    #macro split(stri)
    pst=Instr(stri,chars)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+LC)
    Else
        var1=stri
    End If
    If Len(var1) Then 
        Redim Preserve result(1 To Ubound(result)+1)
        result(Ubound(result))=var1
    End If
    #endmacro
    Do
        split(s):s=var2
    Loop Until var2=""
End Sub


Function getfileuc(file As String) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    f=Freefile
    Open file For Binary Access Read As #f
    'Dim As Integer count
    Dim As String text
    If Lof(f) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function

Function getfile(file As String) As String
    f=Freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    Return text
End Function

'=================   UNPACK ===============
sub unpack
Var text=getfileuc(file)

Dim As Integer stringlength,destinationlength
stringlength=Len(text)
destinationlength =passed_length

 source = Allocate(stringlength)
 destination =Allocate(destinationlength)

source=@text[0]

Var mistake=uncompress(destination,@destinationlength, source, stringlength)
If mistake<>0 Then Print "There was an error":Sleep:End
Dim As String uncompressed

'Build the uncompressed string
uncompressed=String(destinationlength,0)
Print "Please wait"
For i As Integer = 0 To destinationlength- 1
    uncompressed[i]=(destination[i])
Next

Redim As String temp()

string_split(file,"\",temp())

Var filename= Rtrim(temp(Ubound(temp)),".fbz")


f=Freefile
Open "_"+filename For Output As #f
Print #f,uncompressed
Close #f
Print "The UNcompressed file is "&"_"+filename

Print"Press any key"

Sleep
end sub

'===================  PACK ============

sub pack
   Var text=getfile(file)
Dim As Integer stringlength,destinationlength
stringlength=Len(text)

destinationlength = compressBound(stringlength)

Dim As Ubyte Ptr source = Allocate(stringlength)
Dim As Ubyte Ptr destination = Allocate(destinationlength)
source=@text[0]

Var mistake=compress(destination, @destinationlength, source, stringlength)
Redim As String temp()

string_split(file,"\",temp())

Var filename=temp(Ubound(temp))

Print filename & " Compressed"
Print
Print filename & "            Length = ";Len(text)
Print
Print filename & " Compressed length = ";destinationlength
Print
Print "Compression = ";destinationlength/Len(text)
If mistake <>0 Then Print "There was an error"

Dim As String compressed=String(destinationlength,0)

For n As Integer=0 To destinationlength-1
    compressed[n]=destination[n]
Next n

f=Freefile
Open filename+".fbz" For Output As #f
Print #f,stringlength &"|";
Print #f,compressed
Close #f
Print "The compressed file is "&filename+".fbz"

Print"Press any key"
sleep
end sub

sub finish destructor
    print "ending"
    deallocate source
    deallocate destination
    source=0
    destination=0
    end
    end sub
end namespace

'==============  use =========

packer.file="bird.exe"  '<<<---- YOUR FILE HERE
packer.pack
print "now unpacking"
'always the full filename + .fbz for a packed (compressed) file.

packer.file="bird.exe.fbz"
packer.unpack

 
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I want to use the zlib in my "Big Calc" program..
I need to pass a string number to the zlib , to compress it or expand it..

I don't want to compress or decompress files. just strings..
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Create a temp file and pass your string to it, then when you are finished with it, delete the temp file.

Code: Select all





#inclib "zlib1"
Extern "C"
Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Ulong Ptr, Byval source As Const Ubyte Ptr, Byval sourceLen As Ulong) As Long
Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Ulong Ptr, Byval source As Const Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

namespace packer
Dim shared As String file
Dim Shared As Integer f
Dim Shared As Integer passed_length
dim shared as ubyte ptr source,destination
Sub string_split(Byval s As String,chars As String,result() As String)
    Redim result(0)
    Dim As String var1,var2
    Dim As Long pst,LC=Len(chars)
    #macro split(stri)
    pst=Instr(stri,chars)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+LC)
    Else
        var1=stri
    End If
    If Len(var1) Then 
        Redim Preserve result(1 To Ubound(result)+1)
        result(Ubound(result))=var1
    End If
    #endmacro
    Do
        split(s):s=var2
    Loop Until var2=""
End Sub


Function getfileuc(file As String) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    f=Freefile
    Open file For Binary Access Read As #f
    'Dim As Integer count
    Dim As String text
    If Lof(f) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function

Function getfile(file As String) As String
    f=Freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    Return text
End Function

'=================   UNPACK ===============
sub unpack
Var text=getfileuc(file)

Dim As Integer stringlength,destinationlength
stringlength=Len(text)
destinationlength =passed_length

 source = Allocate(stringlength)
 destination =Allocate(destinationlength)

source=@text[0]

Var mistake=uncompress(destination,@destinationlength, source, stringlength)
If mistake<>0 Then Print "There was an error":Sleep:End
Dim As String uncompressed

'Build the uncompressed string
uncompressed=String(destinationlength,0)
Print "Please wait"
For i As Integer = 0 To destinationlength- 1
    uncompressed[i]=(destination[i])
Next

Redim As String temp()

string_split(file,"\",temp())

Var filename= Rtrim(temp(Ubound(temp)),".fbz")


f=Freefile
Open "_"+filename For Output As #f
Print #f,uncompressed
Close #f
Print "The UNcompressed file is "&"_"+filename

Print"Press any key"

Sleep
end sub

'===================  PACK ============

sub pack
   Var text=getfile(file)
Dim As Integer stringlength,destinationlength
stringlength=Len(text)

destinationlength = compressBound(stringlength)

Dim As Ubyte Ptr source = Allocate(stringlength)
Dim As Ubyte Ptr destination = Allocate(destinationlength)
source=@text[0]

Var mistake=compress(destination, @destinationlength, source, stringlength)
Redim As String temp()

string_split(file,"\",temp())

Var filename=temp(Ubound(temp))

Print filename & " Compressed"
Print
Print filename & "            Length = ";Len(text)
Print
Print filename & " Compressed length = ";destinationlength
Print
Print "Compression = ";destinationlength/Len(text)
If mistake <>0 Then Print "There was an error"

Dim As String compressed=String(destinationlength,0)

For n As Integer=0 To destinationlength-1
    compressed[n]=destination[n]
Next n

f=Freefile
Open filename+".fbz" For Output As #f
Print #f,stringlength &"|";
Print #f,compressed
Close #f
Print "The compressed file is "&filename+".fbz"

Print"Press any key"
sleep
end sub

sub finish destructor
    print "ending"
    deallocate source
    deallocate destination
    source=0
    destination=0
    end
    end sub
end namespace

'==============  use =========

 #Include "file.bi"
Sub savefile(filename As String,p As String)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub
Function loadfile(file as string) as String
	If FileExists(file)=0 Then Print file;" not found":Sleep:end
   var  f=freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
end Function

dim as string s="abcdefghijklmnopqrstuvwxyz"+chr(13,10)
s+=s
s+=s
s+="HELLO"
s+=s
print "original string"
print s
print
savefile("Temp.txt",s)  'pass your string to a temp file


packer.file="Temp.txt"  '<<<---- YOUR FILE HERE
packer.pack
print
print "packed string "
print loadfile("Temp.txt.fbz")
print "now unpacking"

'always the full filename + .fbz for a packed (compressed) file.


packer.file="Temp.txt.fbz"
packer.unpack

dim as string result= loadfile("_Temp.txt")'get your temp file back to a string.
kill "_Temp.txt"  'delete the temp file.
print "Retrieve"
print result


  
did you get your Linux zlib to work??
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

Is there a way to just compress a string?

To pretend , the string is comming form a file?

Is there a way to just call compress() , decompress() with a string or string pointer?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Albert.
Using the same setup but only with strings.

Code: Select all

see next post down
  
Last edited by dodicat on Mar 18, 2019 12:47, edited 1 time in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Albert.
I have re-done this for strings.
I use callocate now instead of allocate to try and deallocate better.

Code: Select all




Namespace Zlibrary

#inclib "zlib1"
Extern "C"
Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern


Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source 
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace

'==============  use =========


Randomize
Dim Shared As String s:s=String(1000000,0)


For z As Long=1 To 5
    'make a million string
    For n As Long=0 To Len(s)-1
        s[n]=48+Int(Rnd*10)
    Next
    
    Print "original string"
    Print Len(s)
    Print
    
    Dim As String compressed=Zlibrary.pack(s)
    
    Print "packed string "
    Print Len(compressed)
    Print
    
    Dim As String uncompressed=Zlibrary.unpack(compressed)
    
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    Print "compression ratio  ";Len(compressed)/Len(uncompressed)
    Print Iif(uncompressed=s,"OK","ERROR")
    Print "-------------------------------"
Next z
Print "Done"
Sleep


 
tested 32 and 64 bit windows.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

Compilation failed!!! ( cannot find #inclib "zlib1" )

Compiler output:
Z:\home\albert-redditt\Desktop\FreeBASIC\bin\win64\ld.exe: cannot find -lzlib1

Where is the zip lib ? Did you use the FB lib ?

Where can i download the lib you used?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

zlib1 comes with fb1.06 (in the bin win32 folder)
I got the 64 bit one here
https://www.dll4free.com/zlib.dll.html


for 64 bits
#inclib "zlib"
instead of zlib1
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Squares

Post by marpon »

for people who want the zip static libs 32 or 64 mingw

here https://github.com/marpon/Compression_t ... master/zip
with the example adapted to work with.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I'm downloading FB 1.06

I've got FB 1.05 and in the examples dir , they got some examples , but none of them compile...they all got errors.

I'll try again with FB 1.06 , when i get it installed
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I downloaded FB 1.06 from source forge , it error on compile , cannot find "lzlib1"

Are you using the European FB ?? Can you give me a link to it.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi albert.
If you use 64 bit fb then the .dll can be downloaded from the link I gave.
you would then
#inclib "zlib"
because the lib is called zlib.dll.
Of course zlib.dll must be in the same folder as any code calling it.
Locked