Add sscanf
Not sure how scanf or fscanf would work on the CE, but it would be nice to add sscanf and vsscanf to the toolchain.
int sscanf(const char *__restrict buffer, const char *__restrict format, ...);
int vsscanf(const char *__restrict buffer, const char *__restrict format, va_list va);
scanf could work the same way printf does.
Wat
scanf implies having an stdin, which we don't have. You can always get user input some other way that's more native to the specific platform.
At least the other ones mentioned in the top post are "standalone".
scanfimplies having an stdin, which we don't have.
And printf implies having an stdout, which the CE doesn't really have either. That didn't stop printf from being implemented.
Except we have the LCD as the output and a whole section in the doc about printf, puts etc. On the contrary, something that gets close (but not really) to stdin is os_GetStringInput, which is not recommended to use anyway.
So no, it's not comparable. If someone wants to have scanf based on a non-recommended function, they can always make their own in their program.
I have written an implementation of (v)sscanf based off of https://github.com/tusharjois/bscanf. Similar to bscanf, (v)sscanf requires that all non-suppressed character sequence types must have a maximum field width:
- Valid : "%*3c %*8s %*12[^abc]"
- Valid : "%3c %8s %12[^abc]"
- Valid : "%*c %*s %*[^abc]"
- Invalid : "%c %s %[^abc]" (these are as unsafe as `gets`)
I've also implemented the [ specifier, so format strings such as "%9[^]]%1[]]%*[]]%3c%9[^^]%9[^]^]%3[]^]%*4[]^]%tn%4[^]^]%jn" will work. However, I have not implemented ranges such as %3[0-9] (These are implementation defined in the C23 standard), so one must use %3[0123456789] for now. The maximum size of the character set will be limited to 32-72 characters (since the stack frame has to be less than 127 bytes to prevent inefficient code from being generated), and the conversion will fail/return if there are too many characters.
There are some bugs relating to Ti's strspn and strcspn before it can be added https://github.com/CE-Programming/toolchain/issues/646.
Nice, this at least enforces some safety 👍