-
-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] Functions to read the contents of text and binary files #933
Comments
#67 (comment) discusses some possible syntaxes and issues with native arrays. If we ended up not adding them, another approach to reading binary file content would be to just read it as a string too, and just trust the user not to do anything with the string that expects valid UTF-8. This would require adding a few byte-based functions ( ...except that even they are implemented with C's own strings functions, so would behave incorrectly if the binary string has any $00 bytes. Even the ongoing PR #650 to have dynamic strings would probably still have that limitation. (I'm also not really a fan of adding this kind of implicit limitation, where the notion of "valid string" vs "binary string" is all up to the user's understanding and has no type-level enforcement in the rgbasm language. It's also less flexible than arrays, which could store any numbers, not just bytes. But I'm bringing it up because arrays have issues of their own, and would be a large feature to add just for the same of |
C++ #include <iostream>
#include <string>
int main() {
std::string s("hello");
std::cout << s.size() << " " << s << std::endl;
s[2] = '\0';
std::cout << s.size() << " " << s << std::endl;
return 0;
}
|
Currently pokegold uses a custom tool to measure the size of a 5x5, 6x6, or 7x7 PNG image and encode that as $55, $66, or $77. This could replace its
(pokecrystal can't do this because its front pics have extra animation tiles.) |
|
We could compare this proposal with WLA-DX's .fopen "data.bin" fp
.fsize fp t
.repeat t
.fread fp d
.db d+26
.endr
.undefine t, d |
I don't like exposing arbitrary file I/O because it's just complicated:
...so for the time being, I still prefer people building ad-hoc tools and solutions when such needs arise. Please understand that I'm not trying to be a grouch raining on everyone's parade, but also reining in what I fear to be scope creep. Note I am however leaving this issue open so that people can still voice any support, use cases, or design suggestions. |
Don't we have exactly this theoretical problem already, with |
In theory yes, but not really because |
INCLUDE
andINCBIN
can include a file's contents directly as assembly code or data, but there are times when it would be nice to access a file's contents arbitrarily. For example, you might want to do something different depending on the size of the file; or reuse a tilemap with some offset added to all the bytes; or use part of a file depending on an rgbasm calculation; or so on. This currently has to be handled by including files generated with custom tools according to particular Makefile rules.If we had arbitrary-length strings (#650) and arrays (#67), this could be accomplished with just two functions:
READFILE("file.txt")
would return a string of the file's charactersREADBIN("file.dat")
would return an array of the file's bytesFor both functions, if the file doesn't exist they would abort with a "No such file" error, or under
-M
dependency-generating mode they would act consistent withINCLUDE
andINCBIN
after #903 gets resolved.Note that
READFILE
would read the file's bytes as-is, which may not be valid UTF-8. It's already possibly to create a non-UTF-8 .asm file with arbitrary bytes in a string literal, so this is not a new issue. Some functions likeSTRCAT
andSTRIN
wouldn't care about invalid UTF-8, others likeSTRLEN
andSTRSUB
would print a "Invalid UTF-8 byte" error.The text was updated successfully, but these errors were encountered: