Mail Archives: djgpp-workers/2001/06/13/04:45:03
hello fellow programmer(s),
there is a small improvement i wish to make to the libc
code. it does not infer or require any
interface change, but it will just add a new function.
specifically, i wish to provide _doprnt users with a
function that will allow text formatting on arbitrary
devices rather than files, using appropriate fputc compatible
functions. the idea has already been implemented for
_doscan, using the _doscan_low function, now prototyped
in stdio.h.
all the modification is making a function responsible
for the real text formatting, from the part of _doprnt
that comes right after the FILE flag
operations. i have named this new function _doprnt_low,
and it is able to generate formatted text using any data
structure that the provided
print_putc routine might manipulate. _doprnt_low should
be called by _doprnt at the same point it's code has
been cut off _doprnt.
here is why didn't i choose the virtual file system services:
the use i make of this enhancement is for a data structure
that simulates stdio operations on a memory string. the
appropriate fputc function will
trigger automatic string reallocation, or will conversely
fail output upon reaching the buffer capacity limit (think
of snprintf).
all this is done with the help of 5 long integers and
a pointer in a strbuf struct. should i use the virtual
file system, the strbuf would make the
pseudo file descriptor layer, and will be buffered again
through a bigger FILE structure, in case i make use of
any *printf functionality. here,
buffering would be very expensive in memory (FILE struct
and buffers) and useless operations.
moreover, string operations are usually very abundant,
and are not taking into consideration such system limits
like maximum file handles, that will be overrun very
fast, in deficit of the real file/port/socket usage.
the overhead of calling _doprnt_low by _doprnt is small,
and the achievement in functionality is much higher than
should be if vfs would be used.
i include diff -c results made for doprnt.c and stdio.h
downloaded these days by cvs (thanks, eli).
also please allow me distribute modified doprnt.c to
be used by users of my programs (that i will soon release)
with older libc.
-----------------------doprnt.dif----------------------
*** doprnt.c~ Wed Jun 13 07:46:14 2001
--- doprnt.c Wed Jun 13 07:46:08 2001
***************
*** 1,3 ****
--- 1,4 ----
+ /* slightly modified in 2000 by alex bodnaru, to be
inserted in original */
/* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for
details */
/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for
details */
/* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for
details */
***************
*** 29,35 ****
#define BUF (MAXEXPLD+MAXFRACT+1) /* + decimal point
*/
! #define PUTC(ch) (void) putc(ch, fp)
#define ARG(basetype) _ulonglong = \
flags&LONGDBL ? va_arg(argp, long long basetype)
: \
--- 30,36 ----
#define BUF (MAXEXPLD+MAXFRACT+1) /* + decimal point
*/
! #define PUTC(ch) (void) print_putc(ch, fp)
#define ARG(basetype) _ulonglong = \
flags&LONGDBL ? va_arg(argp, long long basetype)
: \
***************
*** 77,82 ****
--- 78,99 ----
int
_doprnt(const char *fmt0, va_list argp, FILE *fp)
{
+ if (fp == NULL)
+ return (EOF);
+ if (fp->_flag & _IORW)
+ {
+ fp->_flag |= _IOWRT;
+ fp->_flag &= ~(_IOEOF|_IOREAD);
+ }
+ if ((fp->_flag & _IOWRT) == 0)
+ return (EOF);
+
+ return _doprnt_low(fmt0, argp, fp, putc);
+ }
+
+ int
+ _doprnt_low(const char *fmt0, va_list argp, FILE *fp,
int (*print_putc)(int, FILE *))
+ {
const char *fmt; /* format string */
int ch; /* character from fmt */
int cnt; /* return value accumulator */
***************
*** 102,114 ****
decimal = localeconv()->decimal_point[0];
- if (fp->_flag & _IORW)
- {
- fp->_flag |= _IOWRT;
- fp->_flag &= ~(_IOEOF|_IOREAD);
- }
- if ((fp->_flag & _IOWRT) == 0)
- return (EOF);
fmt = fmt0;
digs = "0123456789abcdef";
--- 119,124 ----
-----------------------stdio.dif-----------------------
*** stdio.h~ Wed Jun 13 08:00:35 2001
--- stdio.h Wed Jun 13 07:49:54 2001
***************
*** 130,135 ****
--- 130,136 ----
void _djstat_describe_lossage(FILE *_to_where);
int _doprnt(const char *_fmt, va_list _args, FILE
*_f);
+ int _doprnt_low(const char *fmt0, va_list argp,
FILE *fp, int (*print_putc)(int, FILE *));
int _doscan(FILE *_f, const char *_fmt, void **_argp);
int _doscan_low(FILE *, int (*)(FILE *_get), int
(*_unget)(int, FILE *), const char *_fmt, void **_argp);
int fpurge(FILE *_f);
_________________________________________
Free web based e-mail with Pop access at http://www.newmail.net
- Raw text -