How do you guys see the binary I/O working?

Personally I'd like to see an extension to KiXtart which includes binary data types which would be used for other things like information returned by COM objects and the like.

Another (far simpler) approach would be to provide some new file functions that use a buffer. The buffer is an array in which each element is the ASCII value of the character which has been read/will be written.

The functions would take the form:
code:
$iBytesRead=ReadArray($fdFileDescriptor,$aiDataRead,$iBytesToRead)

$iBytesRead is the number of bytes successfully read, "0" if there is no more data (end of file) and "-1" if there is an error.
$fdFileDescriptor is the file number used in the file Open().
$aiDataRead is the array which will contain the values of the data read.
$iBytesToRead is the number of bytes that you want to read - could be optional in which case it is the size of the array.

Similarly the write would be:
code:
$iBytesWritten=WriteArray($fdFileDescriptor,$aiWriteData,$iBytesToWrite

The wide awake among you will have spotted a problem with ReadArray(). The array is passed as a parameter. To be able to populate the array with the data, we would need pass-by-reference support in KiXtart, and we don't. Yet.

So, ReadArray can be simplified to:
code:
$aiDataRead=ReadArray($fdFileDescriptor,$iBytesToRead)

@ERROR can be checked for end-of-file and error conditions. The number of bytes read is the size of the array.
I prefer the first method as there is a small problem with this way of doing reads in that it is tricky to identify when some data was read before an error occurred.

The other change required is that the Open() function will probably need a flag to ensure that the file is opened in binary or "raw" mode. This is because it will probably be required by the underlying file open API.

Mixing binary and non-binary reads and writes may be possible, but the results will be hard to predict and should be avoided.

Performing binary reads and writes on a file not opened in binary mode (and vice versa) will also have unexpected results.

Some more thoughts (shouldn't think and type at the same time [Wink] )

If binary file I/O is implemented it should be able to support random file I/O, i.e.:
  • A file can be opened for both read and write.
  • Seek() function to move the file cursor to an arbitrary location in the file (1=start, -1=end)
  • Truncate() function to reduce the size of the file (when you've deleted data from the end) or to initialize a file which has data in it already that you want to discard
We would also need some way of dealing with non-string data, WriteInteger(), ReadFloat() perhaps? Or maybe a way of converting the data types to a character array for reading/writing.
Other useful support functions would allow filling an array with a character, padding a string to a length with an arbitrary character and split()/join() to work with a null delimiter.

The more I think about it the more complex it gets.

Enough of my ramblings, for now.