home *** CD-ROM | disk | FTP | other *** search
RISC OS BBC BASIC V Source | 1994-09-16 | 4.2 KB | 92 lines |
- -- Example program to use messages object from BASIC
- -- By George Taylor, george@tardis.ed.ac.uk
- $;" at line ";
- "Adhesive"
- "BasicC"
- "UsefulBits"
- "Squash"
- -- This assembles Adhesive stuff and sets up an exit handler
- -- to deregister with Adhesive when your program exits.
- -- Because of the way BASIC works this exit handler is not called
- -- if you generate an error with error number 0.
- _AdhesivePreInit
- -- Register with Adhesive with the name "test Squash object".
- -- The name is not important, it is only used for informational
- -- purposes by Adhesive.
- _AdhesiveInit("test Squash object")
- -- Request the basicC object from Adhesive.
- _AdhesiveRequest(
- _AssembleRequests_basicC)
- -- Request the message object from Adhesive.
- -- Note we do not need to know that the message object
- -- requires the string+ object, Adhesive takes care of that
- -- for us.
- _AdhesiveRequest(
- _AssembleRequests_squash)
- -- We need to change HIMEM to give the C system some private workspace
- -- after our program.
- -- Do not make this smaller than 16K otherwise an error will occur.
- -- If you wish to allocate more space to the C system without the
- -- C system increasing your wimpslot then make the 16K larger.
- -- With 16K there is about 2K available for malloc() and C stack,
- -- the wimpslot is increased if possible when this runs out.
- -- NOTE: HIMEM must not be changed inside a procedure.
- -1024*16
- -- Initialise C system. Make this call once and only once during the
- -- execution of a program.
- -- NOTE: DO NOT change HIMEM after intialising with the C system or
- -- your program will crash.
- _BasicCInit
- -- Create C_printf%,C_malloc%,C_free% variables.
- _UsefulBitsInit
- -- END OF INTIALISATION
- -- When doing C calls which require a pointer to a buffer
- -- for a 32bit result we shall pass a pointer into this buffer.
- -- Don't try and put more than 256 bytes in this buffer!
- Q CPtrBuffer% =
- _Zmalloc(256)
- -- The calls squash_GetSize and squash_Load will work for any
- -- file type. If the file is actually a squash file then this
- -- is transparent to us. To verify this we test the files 'test1'
- -- (a text file) and 'test2' (a squashed text file).
- -- Find out size and filetype of file. We need the size so we
- -- can allocate a buffer to load the file into. You would typically
- -- check the filetype is correct for the files purpose (e.g. you
- -- would check that a sprite resources file has the correct Sprite
- -- file type).
- -- NB. Squash is very effective on sprite files both in terms of
- -- speed and compressed size.
- _squash_GetSize("test1",size%,filetype%)
- '"File 'test1', size=";size%;", filetype="~filetype%
- -- Allocate buffer for uncompressed file.
- buffer%=
- _Zmalloc(size%)
- -- Load file.
- _squash_Load("test1",buffer%)
- -- Do something with the file. We shall output the first line as
- -- a string.
- $(buffer%)
- -- Free off the buffer space now we have finished with the loaded file.
- _Free(buffer%)
- -- We now repeat the same operations with the file "test2".
- _squash_GetSize("test2",size%,filetype%)
- '"File 'test2', size=";size%;", filetype="~filetype%
- buffer%=
- _Zmalloc(size%)
- _squash_Load("test2",buffer%)
- $(buffer%)
- _Free(buffer%)
- -- The procedures/functions below are used to provide an
- -- even simpler interface to the squash code.
- _squash_GetSize(filename$,
- size%,
- filetype%)
- basicC_CCall,squash_GetSize%,size%,filename$,CPtrBuffer%
- filetype%=!CPtrBuffer%
- size%=-1
- _Error("failed to read squash file '"+filename$+"'")
- _squash_Load(filename$, buf%)
- basicC_CCall,squash_Load%,ok%,filename$,buf%
- ok%=0
- _Error("failed to load squash file '"+filename$+"'")
-