1. -- ************************************************************************
  2. -- Software: BINARY FILE INTERFACE (fileIO Xtra version)
  3. -- Version:  0.6 - D11.5+ version based on bytearray
  4. -- Date:     2014-11-10
  5. -- Author:   Valentin Schmidt
  6. --
  7. -- Requirements/Dependencies:
  8. -- - Xtra "fileIO"
  9. --
  10. -- Interface for binary file operations
  11. --
  12. -- ************************************************************************
  13.  
  14. ----------------------------------------
  15. -- Opens file with specified mode, returns file pointer (xtra instance) or 0
  16. -- @param {string} tFile
  17. -- @param {string} tMode - r, w, a, r+, w+, a+ (,rw, wr)
  18. -- @return {instance|false}
  19. ----------------------------------------
  20. on fopen (tFile, tMode)
  21.   fp = xtra("fileIO").new()
  22.  
  23.   --  'r' Open for reading only; place the file pointer at the beginning of the file.  
  24.   --  'r+' Open for reading and writing; place the file pointer at the beginning of the file.  
  25.   --  'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.  
  26.   --  'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.  
  27.   --  'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.  
  28.   --  'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.  
  29.  
  30.   pos = offset("b", tMode)
  31.   is_binary = false -- currently not used
  32.   if pos then
  33.     is_binary = true
  34.     delete char pos of tMode
  35.   end if
  36.  
  37.   case (tMode) of
  38.     "r":
  39.       fp.openFile(tFile, 1)
  40.     "r+","rw","wr":
  41.       fp.openFile(tFile, 0)
  42.     "w":
  43.       fp.createFile(tFile)
  44.       fp.openFile(tFile, 2)
  45.     "w+":
  46.       fp.createFile(tFile)
  47.       fp.openFile(tFile, 0)
  48.     "a":
  49.       fp.openFile(tFile, 2)
  50.       fp.setPosition(fp.getLength())
  51.     "a+":
  52.       fp.openFile(tFile, 0)
  53.       fp.setPosition(fp.getLength())
  54.      
  55.     otherwise:
  56.       return false
  57.   end case
  58.  
  59.   if (fp.status()<>0) then fp = 0
  60.   return fp
  61. end
  62.  
  63. ----------------------------------------
  64. -- Closes open file
  65. -- @param {instance} fp
  66. ----------------------------------------
  67. on fclose (fp)
  68.   fp.closeFile()
  69.   fp = 0
  70. end
  71.  
  72. ----------------------------------------
  73. -- Reads n bytes from open file, returns bytearray
  74. -- @param {instance} fp
  75. -- @param {integer} n
  76. -- @return {bytearray}
  77. ----------------------------------------
  78. on freadbytes(fp, n)
  79.   ba = fp.readByteArray(n)
  80.   ba.position = 1
  81.   return ba
  82. end
  83.  
  84. ----------------------------------------
  85. -- Writes byteArray to open file
  86. -- @param {instance} fp
  87. -- @param {bytearray} ba
  88. ----------------------------------------
  89. on fwritebytes (fp, ba)
  90.   return fp.writeByteArray(ba)
  91. end
  92.  
  93. ----------------------------------------
  94. -- Reads n bytes from open file, returns string
  95. -- @param {instance} fp
  96. -- @param {integer} n
  97. -- @return {string}
  98. ----------------------------------------
  99. on freadstring(fp, n)
  100.   ba = freadbytes(fp, n)
  101.   ba.position = 1
  102.   return ba.readRawString(ba.length)
  103. end
  104.  
  105. ----------------------------------------
  106. -- Writes string to open file
  107. -- @param {instance} fp
  108. -- @param {string} str
  109. ----------------------------------------
  110. on fwritestring (fp, str)
  111.   fp.writeString(str)
  112. end
  113.  
  114. ----------------------------------------
  115. -- Returns position in open file
  116. -- @param {instance} fp
  117. -- @return {integer}
  118. ----------------------------------------
  119. on ftell (fp)
  120.   return fp.getPosition()
  121. end
  122.  
  123. ----------------------------------------
  124. -- Sets position in open file
  125. -- whence: either integer or symbol
  126. -- 0 or #SEEK_SET: set position to offset bytes. (default)
  127. -- 1 or #SEEK_CUR: set position to current position plus offset.
  128. -- 2 or #SEEK_END: set position to end of file plus offset.
  129. -- @param {instance} fp
  130. -- @param {integer} pos
  131. -- @param {integer|symbol} whence - optional, default = 0
  132. -- @return {bool} success
  133. ----------------------------------------
  134. on fseek (fp, pos, whence)
  135.   if voidP(whence) then whence = 0
  136.   else if ilk(whence)=#symbol then
  137.     whence = [#SEEK_SET, #SEEK_CUR, #SEEK_END].getPos(whence)
  138.     if whence>0 then whence=whence-1
  139.   end if
  140.  
  141.   case (whence) of
  142.     0: fp.setPosition(pos)  
  143.     1: fp.setPosition(fp.getPosition() + pos)
  144.     2: fp.setPosition(fp.getLength() + pos)
  145.   end case
  146.  
  147.   return (fp.status()=0)
  148. end
  149.  
  150. ----------------------------------------
  151. -- Returns size of open file
  152. -- @param {instance} fp
  153. -- @return {integer}
  154. ----------------------------------------
  155. on fsize (fp)
  156.   return fp.getLength()
  157. end
  158.  
  159. --**************************************
  160. -- global functions, no file pointer passed
  161. --**************************************
  162.  
  163. ----------------------------------------
  164. -- Returns file size
  165. -- @param {string} tFile
  166. -- @return {integer}
  167. ----------------------------------------
  168. on file_size (tFile)
  169.   fp = xtra("fileIO").new()
  170.   fp.openFile(tFile, 1)
  171.   err = fp.status()
  172.   if err<>0 then return false
  173.   len = fp.getLength()
  174.   fp.closeFile()
  175.   fp = 0
  176.   return len
  177. end
  178.  
  179. ----------------------------------------
  180. -- Truncates file
  181. -- WARNING: only suited for smaller files, reads complete file into RAM!
  182. -- @param {string} tFile
  183. -- @param {integer} tSize
  184. -- @return {bool} success
  185. ----------------------------------------
  186. on file_truncate (tFile, tSize)
  187.   fp = xtra("fileIO").new()
  188.   fp.openFile(tFile, 0)
  189.   data = fp.readByteArray(tSize)
  190.   fp.delete()
  191.   err = fp.status()
  192.   if err<>0 then return false
  193.   fp.createFile(tFile)
  194.   fp.openFile(tFile, 2)
  195.   fp.writeByteArray(data)
  196.   err = fp.status()
  197.   fp.closeFile()
  198.   fp=0
  199.   return (err=0)
  200. end
  201.  
  202. ----------------------------------------
  203. -- Deletes file
  204. -- @param {string} tFile
  205. -- @return {bool} success
  206. ----------------------------------------
  207. on file_delete (tFile)
  208.   fp = xtra("fileIO").new()
  209.   fp.openFile(tFile, 0)
  210.   err = fp.status()
  211.   if (err=0) then err = fp.delete()
  212.   fp = 0
  213.   return (err=0)
  214. end
  215.  
  216. ----------------------------------------
  217. -- Check if file exists
  218. -- @param {string} tFile
  219. -- @return {bool} exists
  220. ----------------------------------------
  221. on file_exists (tFile)
  222.   fp = xtra("fileIO").new()
  223.   fp.openFile(tFile, 1)
  224.   stat = fp.status()
  225.   fp.closeFile()
  226.   fp = 0
  227.   return (stat<>-37)
  228. end
  229.  
  230. ----------------------------------------
  231. -- Reads whole file, returns UTF-8 string
  232. -- @param {string} tFile
  233. -- @return {string|false}
  234. ----------------------------------------
  235. on file_get_string (tFile)
  236.   fp = xtra("fileIO").new()
  237.   fp.openFile(tFile,1)
  238.   err = fp.status()
  239.   if (err) then return false
  240.   ret = fp.readFile()
  241.   fp.closeFile()
  242.   fp = 0
  243.   if chartonum(ret.char[1]) = 65279 then
  244.     delete char 1 of ret -- remove UTF-8 BOM
  245.   end if
  246.   return ret
  247. end
  248.  
  249. ----------------------------------------
  250. -- Saves UTF-8 string as file
  251. -- @param {string} tFile
  252. -- @param {string} tString
  253. -- @param {bool} [tAddBOM]
  254. -- @return {bool} success
  255. ----------------------------------------
  256. on file_put_string (tFile, tString, tAddBOM)
  257.   fp = xtra("fileIO").new()  
  258.   fp.openFile(tFile, 1)
  259.   err = fp.status()
  260.   if (err=0) then fp.delete()
  261.   else if (err<> -37) then return false
  262.  
  263.   fp.createFile(tFile)
  264.   err = fp.status()
  265.   if (err<>0) then return false
  266.  
  267.   fp.openFile(tFile, 2)
  268.   err = fp.status()
  269.   if (err<>0) then return false
  270.  
  271.   if tAddBOM then
  272.     fp.writeString(numtochar(65279)) -- add UTF-8 BOM
  273.   end if
  274.  
  275.   err = fp.writeString(tString)
  276.   fp.closeFile()
  277.   fp=0
  278.   return (err=0)
  279. end
  280.  
  281. ----------------------------------------
  282. -- Returns whole file, returns byteArray
  283. -- @param {string} tFile
  284. -- @return {bytearray|false}
  285. ----------------------------------------
  286. on file_get_bytes (tFile)
  287.  
  288.   fp = xtra("fileIO").new()  
  289.   fp.openFile(tFile, 1)
  290.   err = fp.status()
  291.   if (err<>0) then return false
  292.  
  293.   data = fp.readByteArray(fp.getLength())
  294.   fp.closeFile()
  295.   fp = 0
  296.  
  297.   return data
  298. end
  299.  
  300. ----------------------------------------
  301. -- Saves byteArray as file
  302. -- @param {string} tFile
  303. -- @param {byteArray} tByteArray
  304. -- @return {bool} success
  305. ----------------------------------------
  306. on file_put_bytes (tFile, tByteArray)
  307.  
  308.   fp = xtra("fileIO").new()  
  309.   fp.openFile(tFile, 1)
  310.   err = fp.status()
  311.   if (err=0) then fp.delete()
  312.   else if (err <> -37) then return false
  313.  
  314.   fp.createFile(tFile)
  315.   err = fp.status()
  316.   if (err<>0) then return false
  317.  
  318.   fp.openFile(tFile, 2)
  319.   err = fp.status()
  320.   if (err<>0) then return false
  321.  
  322.   ok = fp.writeByteArray(tByteArray)
  323.   fp.closeFile()
  324.   fp=0
  325.  
  326.   return (err=0)
  327. end
  328.  
  329. ----------------------------------------
  330. -- Display open file dialog
  331. -- @param {string} title - optional, ignored in fileIO version
  332. -- @param {string} filterMask - optional
  333. -- @param {string} defaultFileName - optional, ignored in fileIO version
  334. -- @return {string} filename
  335. ----------------------------------------
  336. on display_open (title, filterMask, defaultFileName)  
  337.   fio=xtra("fileIO").new()  
  338.   if stringP(filterMask) then fio.setFilterMask(filterMask)
  339.   fn=fio.displayOpen()
  340.   fio=0
  341.   return string(fn)
  342. end
  343.  
  344. ----------------------------------------
  345. -- Display save file dialog
  346. -- @param {string} title
  347. -- @param {string} filterMask - optional
  348. -- @param {string} defaultFileName
  349. -- @return {string} filename
  350. ----------------------------------------
  351. on display_save (title, filterMask, defaultFileName)  
  352.   fio=xtra("fileIO").new()  
  353.   if stringP(filterMask) then fio.setFilterMask(filterMask)
  354.   fn=fio.displaySave(title, defaultFileName)
  355.   fio=0
  356.   return string(fn)
  357. end
  358.  
[raw code]