1. --****************************************************************************
  2. -- Software: BINARY FILE INTERFACE (BinFile Xtra version)
  3. -- Version:  0.3
  4. -- Date:     2007-06-06
  5. -- Author:   Valentin Schmidt
  6. --
  7. -- Requirements/Dependencies:
  8. -- - Xtra "BinFile"
  9. --
  10. -- Interface for binary file operations (PHP style)
  11. --
  12. -- ************************************************************************
  13.  
  14. ----------------------------------------
  15. -- opens file with specified mode, returns file pointer (xtra instance)
  16. -- tMode: r, rb, w, wb, a, ab, rw, rwb, wr, wrb
  17. -- tType and tCreator optional, only used in mac version when new file is created
  18. ----------------------------------------
  19. on fopen (tFile, tMode, tType, tCreator)
  20.   fp = xtra("BinFile").new()
  21.  
  22.   if tMode = "rw" or tMode = "wr" then tMode="r+"
  23.   if tMode = "rwb" or tMode = "wrb" then tMode="r+b"
  24.  
  25.   ok = fp.bx_fopen(tFile, tMode, tType, tCreator)
  26.   if ok then return fp
  27.   else return ok
  28. end
  29.  
  30. ----------------------------------------
  31. -- closes open file
  32. ----------------------------------------
  33. on fclose (fp)
  34.   ok = fp.bx_fclose()
  35.   fp = 0
  36.   return ok
  37. end
  38.  
  39. ----------------------------------------
  40. -- reads n bytes from open file, returns binary string
  41. ----------------------------------------
  42. on fread(fp, n)  
  43.   return fp.bx_fread(n)
  44. end
  45.  
  46. ----------------------------------------
  47. -- writes binary string to open file
  48. ----------------------------------------
  49. on fwrite (fp, str)
  50.   return fp.bx_fwrite(str)
  51. end
  52.  
  53. ----------------------------------------
  54. -- returns position in open file
  55. ----------------------------------------
  56. on ftell (fp)
  57.   return fp.bx_ftell()
  58. end
  59.  
  60. ----------------------------------------
  61. -- sets position in open file
  62. -- whence: either integer or symbol
  63. -- 0 or #SEEK_SET: set position to offset bytes. (default)
  64. -- 1 or #SEEK_CUR: set position to current position plus offset.
  65. -- 2 or #SEEK_END: set position to end of file plus offset.
  66. ----------------------------------------
  67. on fseek (fp, pos, whence)
  68.     if voidP(whence) then whence = 0
  69.   else if ilk(whence)=#symbol then
  70.     whence = [#SEEK_SET, #SEEK_CUR, #SEEK_END].getPos(whence)
  71.     if whence>0 then whence=whence-1
  72.   end if
  73.  
  74.   return fp.bx_fseek(pos, whence)
  75. end
  76.  
  77. ----------------------------------------
  78. -- returns size of open file
  79. ----------------------------------------
  80. on fsize (fp)
  81.   return fp.bx_fsize()
  82. end
  83.  
  84. ----------------------------------------
  85. -- global functions, no file pointer passed
  86. ----------------------------------------
  87.  
  88. ----------------------------------------
  89. -- returns file size
  90. ----------------------------------------
  91. on file_size (tFile)
  92.   return bx_file_size(tFile)
  93. end
  94.  
  95. ----------------------------------------
  96. -- truncates file
  97. ----------------------------------------
  98. on file_truncate (tFile, tSize)
  99.   return bx_file_truncate(tFile, tSize)
  100. end
  101.  
  102. ----------------------------------------
  103. -- deletes file
  104. ----------------------------------------
  105. on file_delete (tFile)
  106.   return bx_file_delete(tFile)
  107. end
  108.  
  109. ----------------------------------------
  110. --
  111. ----------------------------------------
  112. on file_exists (tFile)
  113.   return bx_file_exists(tFile)
  114. end
  115.  
  116. ----------------------------------------
  117. -- reads whole file, returns binary string
  118. ----------------------------------------
  119. on file_get_contents (tFile)
  120.   return bx_file_get_contents (tFile)
  121. end
  122.  
  123. ----------------------------------------
  124. -- saves (binary) string as file (tType and tCreator optional, only used in mac version)
  125. ----------------------------------------
  126. on file_put_contents (tFile, tString, tType, tCreator)
  127.   return bx_file_put_contents (tFile, tString, tType, tCreator)
  128. end
  129.  
  130. ----------------------------------------
  131. -- display open file dialog
  132. ----------------------------------------
  133. on display_open (title, filterMask, defaultFileName)
  134.   fn = bx_display_open (title, filterMask, defaultFileName)
  135.   return string(fn)
  136. end
  137.  
  138. ----------------------------------------
  139. -- display save file dialog
  140. ----------------------------------------
  141. on display_save (title, filterMask, defaultFileName)
  142.   fn = bx_display_save (title, filterMask, defaultFileName)
  143.   return string(fn)
  144. end
  145.  
[raw code]