- * Type to encapsulate the local buffer
- * management skipping over in a file
- */
-
-typedef struct {
- io_glue *ig;
- int len;
- int cp;
- char buf[BSIZ];
-} mbuf;
-
-
-static
-void init_buf(mbuf *mb, io_glue *ig) {
- mb->len = 0;
- mb->cp = 0;
- mb->ig = ig;
-}
-
-
-
-/*
-=item gnext(mbuf *mb)
-
-Fetches a character and advances in stream by one character.
-Returns a pointer to the byte or NULL on failure (internal).
-
- mb - buffer object
-
-=cut
-*/
-
-#define gnext(mb) (((mb)->cp == (mb)->len) ? gnextf(mb) : (mb)->buf + (mb)->cp++)
-
-static
-char *
-gnextf(mbuf *mb) {
- io_glue *ig = mb->ig;
- if (mb->cp == mb->len) {
- mb->cp = 0;
- mb->len = ig->readcb(ig, mb->buf, BSIZ);
- if (mb->len == -1) {
- i_push_error(errno, "file read error");
- mm_log((1, "i_readpnm: read error\n"));
- return NULL;
- }
- if (mb->len == 0) {
- mm_log((1, "i_readpnm: end of file\n"));
- return NULL;
- }
- }
- return &mb->buf[mb->cp++];
-}
-
-
-/*
-=item gpeek(mbuf *mb)
-
-Fetches a character but does NOT advance. Returns a pointer to
-the byte or NULL on failure (internal).
-
- mb - buffer object
-
-=cut
-*/
-
-#define gpeek(mb) ((mb)->cp == (mb)->len ? gpeekf(mb) : (mb)->buf + (mb)->cp)
-
-static
-char *
-gpeekf(mbuf *mb) {
- io_glue *ig = mb->ig;
- if (mb->cp == mb->len) {
- mb->cp = 0;
- mb->len = ig->readcb(ig, mb->buf, BSIZ);
- if (mb->len == -1) {
- i_push_error(errno, "read error");
- mm_log((1, "i_readpnm: read error\n"));
- return NULL;
- }
- if (mb->len == 0) {
- mm_log((1, "i_readpnm: end of file\n"));
- return NULL;
- }
- }
- return &mb->buf[mb->cp];
-}
-
-int
-gread(mbuf *mb, unsigned char *buf, size_t read_size) {
- int total_read = 0;
- if (mb->cp != mb->len) {
- int avail_size = mb->len - mb->cp;
- int use_size = read_size > avail_size ? avail_size : read_size;
- memcpy(buf, mb->buf+mb->cp, use_size);
- mb->cp += use_size;
- total_read += use_size;
- read_size -= use_size;
- buf += use_size;
- }
- if (read_size) {
- io_glue *ig = mb->ig;
- int read_res = i_io_read(ig, buf, read_size);
- if (read_res >= 0) {
- total_read += read_res;
- }
- }
- return total_read;
-}
-
-
-/*
-=item skip_spaces(mb)