toolchain icon indicating copy to clipboard operation
toolchain copied to clipboard

Add sscanf

Open ZERICO2005 opened this issue 9 months ago • 5 comments

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);

ZERICO2005 avatar Mar 13 '25 17:03 ZERICO2005

scanf could work the same way printf does.

BlueCannonBall avatar Apr 06 '25 00:04 BlueCannonBall

Wat

mateoconlechuga avatar Apr 06 '25 00:04 mateoconlechuga

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".

adriweb avatar Apr 06 '25 07:04 adriweb

scanf implies 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.

BlueCannonBall avatar Apr 06 '25 18:04 BlueCannonBall

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.

adriweb avatar Apr 06 '25 18:04 adriweb

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.

ZERICO2005 avatar Oct 04 '25 17:10 ZERICO2005

Nice, this at least enforces some safety 👍

adriweb avatar Oct 04 '25 17:10 adriweb