--!movie
--!encoding=utf-8
--****************************************************************************
-- Software: BINARY FILE INTERFACE (BinFile Xtra version)
-- Version: 0.5
-- Date: 2015-08-07
-- Author: Valentin Schmidt
--
-- Requirements/Dependencies:
-- - Xtra "BinFile" (v2.7+)
--
-- Interface for binary file operations (PHP style)
--
-- ************************************************************************
----------------------------------------
-- Opens file with specified mode, returns file pointer (xtra instance) or 0
-- @param {string} tFile
-- @param {string} tMode - r, w, a, r+, w+, a+ (,rw, wr), with "b" appended for binary files
-- @return {instance|false}
----------------------------------------
on fopen (tFile, tMode)
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)
if ok then return fp
else return ok
end
----------------------------------------
-- Closes open file
-- @param {instance} fp
-- @return {bool} success
----------------------------------------
on fclose (fp)
ok = fp.bx_fclose()
fp = 0
return ok
end
----------------------------------------
-- Reads n bytes from open file, returns bytearray
-- @param {instance} fp
-- @param {integer} n
-- @return {bytearray}
----------------------------------------
on freadbytes(fp, n)
return fp.bx_fread(n)
end
----------------------------------------
-- Writes byteArray to open file
-- @param {instance} fp
-- @param {bytearray} ba
-- @return {integer} bytesWritten
----------------------------------------
on fwritebytes (fp, ba)
return fp.bx_fwrite(ba)
end
----------------------------------------
-- Reads n bytes from open file, returns string
-- @param {instance} fp
-- @param {integer} n
-- @return {string}
----------------------------------------
on freadstring(fp, n)
ba = fp.bx_fread(n)
return ba.readRawString(ba.length)
end
----------------------------------------
-- Writes string to open file
-- @param {instance} fp
-- @param {string} str
-- @return {integer} bytesWritten
----------------------------------------
on fwritestring (fp, str)
return fp.bx_fwrite(str)
end
----------------------------------------
-- Returns position in open file
-- @param {instance} fp
-- @return {integer}
----------------------------------------
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.
-- @param {instance} fp
-- @param {integer} pos
-- @param {integer|symbol} whence - optional, default = 0
-- @return {bool} success
----------------------------------------
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
-- @param {instance} fp
-- @return {integer}
----------------------------------------
on fsize (fp)
return fp.bx_fsize()
end
----------------------------------------
-- global functions, no file pointer passed
----------------------------------------
----------------------------------------
-- Returns file size
-- @param {string} tFile
-- @return {integer}
----------------------------------------
on file_size (tFile)
return bx_file_size(tFile)
end
----------------------------------------
-- Truncates file
-- @param {string} tFile
-- @param {integer} tSize
-- @return {bool} success
----------------------------------------
on file_truncate (tFile, tSize)
return bx_file_truncate(tFile, tSize)
end
----------------------------------------
-- Deletes file
-- @param {string} tFile
-- @return {bool} success
----------------------------------------
on file_delete (tFile)
return bx_file_delete(tFile)
end
----------------------------------------
-- Check if file exists
-- @param {string} tFile
-- @return {bool} exists
----------------------------------------
on file_exists (tFile)
return bx_file_exists(tFile)
end
----------------------------------------
-- Reads whole file, returns UTF-8 string
-- @param {string} tFile
-- @return {string|false}
----------------------------------------
on file_get_string (tFile)
ba = bx_file_get_contents (tFile)
ret = ba.readRawString(ba.length)
return ret
end
----------------------------------------
-- Saves UTF-8 string as file
-- @param {string} tFile
-- @param {string} tString
-- @param {bool} [tAddBOM]
-- @return {bool} success
----------------------------------------
on file_put_string (tFile, tString, tAddBOM)
if tAddBOM then
fp = fopen(tFile, "wb")
BOM = bytearray(3)
BOM[1] = 239
BOM[2] = 187
BOM[3] = 191
fwritebytes(fp, BOM)
ok = fwritestring(fp, tString)
fclose(fp)
return ok<>0
else
return bx_file_put_contents (tFile, tString)<>0
end if
end
----------------------------------------
-- Returns whole file, returns byteArray
-- @param {string} tFile
-- @return {bytearray|false}
----------------------------------------
on file_get_bytes(tFile)
return bx_file_get_contents (tFile)
end
----------------------------------------
-- Saves byteArray as file
-- @param {string} tFile
-- @param {byteArray} tByteArray
-- @return {bool} success
----------------------------------------
on file_put_bytes (tFile, tByteArray)
return bx_file_put_contents (tFile, tByteArray)
end
----------------------------------------
-- Display open file dialog
-- @param {string} title
-- @param {string} filterMask
-- @param {string} defaultFileName
-- @return {string} filename
----------------------------------------
on display_open (title, filterMask, defaultFileName)
fn = bx_display_open (title, filterMask, defaultFileName)
return string(fn)
end
----------------------------------------
-- Display save file dialog
-- @param {string} title
-- @param {string} filterMask
-- @param {string} defaultFileName
-- @return {string} filename
----------------------------------------
on display_save (title, filterMask, defaultFileName)
fn = bx_display_save (title, filterMask, defaultFileName)
return string(fn)
end