Re: GCC 3.2 replacement for streammarker?
On 27-Oct-02, 10:20 (CST), "Steve M. Robbins" <steven.robbins@videotron.ca> wrote:
> [Bear in mind, however, that this code dates from 10 years ago,
> and was developed when stdio was a lot more standard, apparently.
> They took a lot of liberties with poking about in the FILE structure]
10 years ago was 3 years after the ANSI/ISO C standard was released, And
FILE has always[1] been an opaque type. No excuse. As I'm sure you know.
> Sadly, geomview is too general: it accepts input from many different
> sources including over the net from a remote machine, the tty, a
> pipe from another process, a memory buffer, or a bona-fide disk file.
>
> In a perfect world, it needs to occasionally seek backwards on
> any of those sources.
Then it needs to do its own buffering.
With no errorchecking, or even a compile test:
#include <stdio.h>
#include <stdlib.h>
static int dobuf = 0;
static char *buf=NULL;
static char *bp = NULL;
static size_t l = 0;
void add_to_buf(int c) {
l++;
buf = realloc(buf, l));
buf[l-1] = (char) c;
}
int my_fgetc(FILE *foo) {
int c;
if (bp) {
if (*bp != '\0') {
c = (int) *bp++;
} else {
bp == NULL;
}
} else {
c = fgetc(foo);
if (dobuf) {
add_to_buf(c);
}
}
return c;
}
my_setmark() {
dobuf = 1;
}
my_freemark() {
free(buf);
bp = NULL;
l = 0;
}
my_rewind() {
bp = buf;
}
(No, I wouldn't code it this way for real. But it's sure as hell better
than mucking about in FILE *).
Steve
[1] Or at least since I started C programming in 1987, which is just
like "always" in Internet time. I think even K&R 1 said not to muck
around in FILE.
--
Steve Greenland
The irony is that Bill Gates claims to be making a stable operating
system and Linus Torvalds claims to be trying to take over the
world. -- seen on the net
Reply to: