--****************************************************************************
-- Software: BINARY FILE INTERFACE (BinFile Xtra version)
-- Version: 0.3
-- Date: 2007-06-06
-- Author: Valentin Schmidt
--
-- Requirements/Dependencies:
-- - Xtra "BinFile"
--
-- Interface for binary file operations (PHP style)
--
-- ************************************************************************
----------------------------------------
-- opens file with specified mode, returns file pointer (xtra instance)
-- tMode: r, rb, w, wb, a, ab, rw, rwb, wr, wrb
-- tType and tCreator optional, only used in mac version when new file is created
----------------------------------------
on fopen (tFile, tMode, tType, tCreator)
fp = xtra("BinFile").new()
if tMode = "rw" or tMode = "wr" then tMode="r+"
if tMode = "rwb" or tMode = "wrb" then tMode="r+b"
ok = fp.bx_fopen(tFile, tMode, tType, tCreator)
if ok then return fp
else return ok
end
----------------------------------------
-- closes open file
----------------------------------------
on fclose (fp)
ok = fp.bx_fclose()
fp = 0
return ok
end
----------------------------------------
-- reads n bytes from open file, returns binary string
----------------------------------------
on fread(fp, n)
return fp.bx_fread(n)
end
----------------------------------------
-- writes binary string to open file
----------------------------------------
on fwrite (fp, str)
return fp.bx_fwrite(str)
end
----------------------------------------
-- returns position in open file
----------------------------------------
on ftell (fp)
return fp.bx_ftell()
end
----------------------------------------
-- sets position in open file
-- whence: either integer or symbol
-- 0 or #SEEK_SET: set position to offset bytes. (default)
-- 1 or #SEEK_CUR: set position to current position plus offset.
-- 2 or #SEEK_END: set position to end of file plus offset.
----------------------------------------
on fseek (fp, pos, whence)
if voidP(whence) then whence = 0
else if ilk(whence)=#symbol then
whence = [#SEEK_SET, #SEEK_CUR, #SEEK_END].getPos(whence)
if whence>0 then whence=whence-1
end if
return fp.bx_fseek(pos, whence)
end
----------------------------------------
-- returns size of open file
----------------------------------------
on fsize (fp)
return fp.bx_fsize()
end
----------------------------------------
-- global functions, no file pointer passed
----------------------------------------
----------------------------------------
-- returns file size
----------------------------------------
on file_size (tFile)
return bx_file_size(tFile)
end
----------------------------------------
-- truncates file
----------------------------------------
on file_truncate (tFile, tSize)
return bx_file_truncate(tFile, tSize)
end
----------------------------------------
-- deletes file
----------------------------------------
on file_delete (tFile)
return bx_file_delete(tFile)
end
----------------------------------------
--
----------------------------------------
on file_exists (tFile)
return bx_file_exists(tFile)
end
----------------------------------------
-- reads whole file, returns binary string
----------------------------------------
on file_get_contents (tFile)
return bx_file_get_contents (tFile)
end
----------------------------------------
-- saves (binary) string as file (tType and tCreator optional, only used in mac version)
----------------------------------------
on file_put_contents (tFile, tString, tType, tCreator)
return bx_file_put_contents (tFile, tString, tType, tCreator)
end
----------------------------------------
-- display open file dialog
----------------------------------------
on display_open (title, filterMask, defaultFileName)
fn = bx_display_open (title, filterMask, defaultFileName)
return string(fn)
end
----------------------------------------
-- display save file dialog
----------------------------------------
on display_save (title, filterMask, defaultFileName)
fn = bx_display_save (title, filterMask, defaultFileName)
return string(fn)
end