[En-Nut-Discussion] Fixes to nut/crt/getf.c

Pete Allinson pete at peteallinson.plus.com
Thu Feb 3 13:43:06 CET 2005


Hi,

A certain amount of exasperation with nut/crt/scanf and its friends caused 
me finally to take action ;-)
The results (notwithstanding ongoing testing and tidying-up) can be seen at:
http://www.peteallinson.plus.com/ethernut/nut/crt

I'd be interested in any comments, particularly examples of format strings 
and inputs that break _getf (but lets not go mad here! - I'm trying to keep 
it lean and mean :-)

Harald, you're free to put these into Nut/OS if you think they're 
worthy.  (But you might want to give me a bit of time for more testing and 
tidying-up.  Most of my testing has been nut/crt in isolation.)

The changes (from Nut/OS 3.9.2) are summarised as follows:

nut/crt/getf.c
==============
* Uses macros GETCH and UNGETCH to get/push-back the next/current input 
character.  Means that input characters aren't dropped between conversion 
specifiers.
* Added %n conversion specifier.
* Added %[ conversion specifier (but only works on 7-bit character input).
* Added support for h and hh modifiers.
* Fixed %f to denote float, and %lf to denote double conversion.

In my early ideas the GETCH and UNGETCH macros stored the push-back 
character locally, but I didn't like having the push-back character being 
lost when fscanf returned.  So these macros now use fgetc and ungetc.  This 
resulted in further changes to _get, as well as changes elsewhere:

* Argument 1 (_getb) is not used; Argument 2 is the file stream instead of 
the file descriptor.

nut/crt/vfscanf.c, nut/crt/vfscanf_p.c
================== ===================
* Change how these functions call _getf.

Changing the signature of _getf and using fgetc/ungetc had further 
consequences for vsscanf:

nut/crt/nut_io.h
================
* Add extra member to __iob, iob_str, to store pointer to vsscanf input string.
* New flag, _IOSTR, telling fgetc to read from iob_str instead of input device.
* New declaration for _getf.

nut/crt/fgetc.c
===============
* Test for _IOSTR to get character from iob_str.
* Fix setting _IOEOF and _IOERR flags.

nut/crt/vsscanf.c, nut/crt/vsscanf_p.c
================== ===================
* Allocate a FILE (__iob), set flags and _IOSTR, set iob_str.
* Pass this stream to _getf.

nut/crt/ungetc.c
================
* Clear _IOEOF.


Regards,

Pete Allinson







More information about the En-Nut-Discussion mailing list