ID:1371185
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
This suggestion is relatively simple. Allow file objects (either from file() or cache) to be accessed as lists, via a property such as "FileObject.data".

This would allow BYOND code to access file data in binary mode, subject to the same security limitations as access to files in text mode. Accessing a file would allow you to retrieve a temporary list representing the file as a stream of bytes.

For Example, if we create a file "Hello.txt" in the .dmb folder and write "HELLO" in it:

var/file/F = file("Hello.txt")
var/list/Bytes = F.read()


Bytes would contain the values

{0x48, 0x45, 0x4C, 0x4C, 0x4F}


This way we can read binary data from files, without any major changes to the DM language.

Writing a file would be similar:

FileObject.write(Bytes)
FileObject.append(Bytes)

write() would delete the file first, whereas append() would merely write data to the end of the file.

These changes would allow us to read and write raw binary data to files, both in overwrite and append modes.

Data validation for write() and append() would go as follows:

Before trying to write data, check every value in Bytes[]. If any value is negative, or greater than 255, automatically CRASH() noting that non-byte values cannot be written. In addition, if a value in the array is a lone character which can be represented in ASCII, then automatically convert it to the numeric character code and use that.

Additional alterations to the write() and append() functions would be the following:

* If write() or append() are passed a longer string, then automatically convert the string to bytes and write those, but not the null-terminator.
* Multiple parameters, e.g. write("Hello", 10, 13, "World!") will be written independently. For example, that function would result in the following file contents:
Hello
World!


Edit: Apparently I managed to accidentally check the 'this could affect security' checkbox. My apologies.
There is a library that allows reading a writing binary of files by the use of a DLL. Don't think it'll work with the cache directly though.

http://www.byond.com/developer/Hobnob/file_io

Hobnob also gives the source of his dll to compile into a .so or modify.