[comp.lang.c] STDIO

peter@ficc.ferranti.com (Peter da Silva) (09/07/90)

In article <17632@haddock.ima.isc.com> karl@kelp.ima.isc.com (Karl Heuer) writes:
> Fine; functions that have that property can be declared with a type other than
> "bool".  Functions that don't have any useful value to return on success can
> and should return TRUE.  (fgets() was another mistake.)

Fgets() has a useful value to return: the number of characters read. A
0 length read from fgets should be an EOF (think about it).

For that matter, the whole stdio suite is fubar. But, it's a standard.
Perhaps a new library could be designed in alt.lang.cfutures

	FILE *openf(name, mode);
	char *name;
	int mode;	/* From fcntl.h */

	int readf(fp, buf, len);
	FILE *fp;
	char *buf;
	int len;

	int readlnf(fp, buf, len);
	FILE *fp;
	char *buf;
	int len;

	int parsef(fp, fmt, args...)
	FILE *fp;
	char *fmt;

	int readln(buf, len);
	char *buf;
	int len;

	int formatf(fp, fmt, args...);
	FILE *fp;
	char *fmt;

	int formats(buf, len, fmt, args...);
	char *buf;
	int len;
	char *fmt;

Fp, when present, always comes first. Buffers are always associated with
a length. Consistent rules for the syntax would make things a lot easier
to remember.
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com

stephen@estragon.uchicago.edu (Stephen P Spackman) (09/08/90)

In article <38Q5QG2@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:
   For that matter, the whole stdio suite is fubar. But, it's a standard.
   Perhaps a new library could be designed in alt.lang.cfutures
   [...]

Another thing that is needed is a function
   FILE *pseudof
     ( int (*producer)(void *handle, char *buf, size_t len)
     , int (*consumer)(void *handle, char const *buf, size_t len)
     , void *handle
     , char const *mode
     );

that takes USER-DEFINED buffer readers and writers and makes a "FILE"
object. That way, you gat all the sprintf analogues for free, and you
can fake up portable I/O for nonstandard filesystems and all sorts of
groovy stuff.

That signature needs further thought - maybe it needs a flush/close
function as well, e.g.; but it's not so far away.

stephen p spackman  stephen@estragon.uchicago.edu  312.702.3982

chris@mimsy.umd.edu (Chris Torek) (09/08/90)

In article <STEPHEN.90Sep7125034@estragon.uchicago.edu>
stephen@estragon.uchicago.edu (Stephen P Spackman) writes:
>Another thing that is needed is a function
>   FILE *pseudof
>     ( int (*producer)(void *handle, char *buf, size_t len)
>     , int (*consumer)(void *handle, char const *buf, size_t len)
>     , void *handle
>     , char const *mode
>     );
>
>that takes USER-DEFINED buffer readers and writers and makes a "FILE"
>object.

Gee, you mean like this one? :-)

	FILE *funopen(const void *_cookie,
		int (*readfn)(void *_cookie, char *_buf, int _n),
		int (*writefn)(void *_cookie, const char *_buf, int _n),
		fpos_t (*seekfn)(void *_cookie, fpos_t _off, int _whence),
		int (*closefn)(void *_cookie));

>That signature needs further thought - maybe it needs a flush/close
>function as well, e.g.; but it's not so far away.

No flush function is required, but seek and close functions are.

No `mode' is required either; this is implicit in the arguments.
(At least one of readfn and writefn must be non nil; if both are, the
function is a read-and-write.  All operations are `binary'; any `text'
conversion must be done in the read/write operations.)

This is running in the stdio on my 4.3BSD-reno-ish machine.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

peter@sugar.hackercorp.com (Peter da Silva) (09/08/90)

In article <STEPHEN.90Sep7125034@estragon.uchicago.edu> stephen@estragon.uchicago.edu (Stephen P Spackman) writes:
> Another thing that is needed is a function
>    FILE *pseudof
>      ( int (*producer)(void *handle, char *buf, size_t len)
>      , int (*consumer)(void *handle, char const *buf, size_t len)
>      , void *handle
>      , char const *mode
>      );

That's pretty cool, man. I like it. Except that in my ideal stdio the
mode is back to being a binary mask, but that was easy to miss. The
mas would have values like:

	O_RONLY	O_RDWR	O_WRONLY
	O_BINARY
	O_APPEND
	O_UNBUFFERED
	etc...

> That signature needs further thought - maybe it needs a flush/close
> function as well, e.g.; but it's not so far away.

Sounds good.
-- 
Peter da Silva.   `-_-'
<peter@sugar.hackercorp.com>.

roland@ai.mit.edu (Roland McGrath) (09/09/90)

The GNU C Library (when I release it--don't ask) allows user-specified read,
write, seek, and close functions that operate on magic cookies (void *).

There is also another level of customization that can be made: user-specified
input_room and output_room functions.  These must understand stdio internals,
but are very useful for things like a self-expanding buffer (this is how I
implement asprintf, which is sprintf but gives you back a malloc'd string of
just the right length).
--
	Roland McGrath
	Free Software Foundation, Inc.
roland@ai.mit.edu, uunet!ai.mit.edu!roland

scott@nbc1.ge.com (Scott Barman) (09/10/90)

In article <38Q5QG2@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:
>Fp, when present, always comes first. Buffers are always associated with
>a length. Consistent rules for the syntax would make things a lot easier
>to remember.
>-- 
>Peter da Silva.   `-_-'

Aww Peter, now why did you do that?  It made sense!!!	:-)

-- 
scott barman				NBC Systems Development
scott@nbc1.ge.com			30 Rockerfeller Plaza, Room 1615W
{philabs,crdgw1}!nbc1!scott		New York, NY  10112	+1 212/664-2787
  (This does not represent any [un]official opinions of NBC or its affiliates)