static int get_bytes(DviContext *dvi, size_t n) { /* * caller wants to read `n' bytes from dvi->buffer + dvi->pos. * Make sure there is enough data to satisfy the request */ if(NEEDBYTES(dvi, n)) { size_t required; int newlen; if(dvi->buffer.frozen || dvi->in == NULL || feof(dvi->in)) { /* this is EOF */ dviwarn(dvi, _("unexpected EOF\n")); return -1; } /* get more data */ if(dvi->buffer.data == NULL) { /* first allocation */ dvi->buffer.size = Max(DVI_BUFLEN, n); dvi->buffer.data = (Uchar *)mdvi_malloc(dvi->buffer.size); dvi->buffer.length = 0; dvi->buffer.frozen = 0; } else if(dvi->buffer.pos < dvi->buffer.length) { /* move whatever we want to keep */ dvi->buffer.length -= dvi->buffer.pos; memmove(dvi->buffer.data, dvi->buffer.data + dvi->buffer.pos, dvi->buffer.length); } else { /* we can discard all the data in this buffer */ dvi->buffer.length = 0; } required = n - dvi->buffer.length; if(required > dvi->buffer.size - dvi->buffer.length) { /* need to allocate more memory */ dvi->buffer.size = dvi->buffer.length + required + 128; dvi->buffer.data = (Uchar *)xresize(dvi->buffer.data, char, dvi->buffer.size); } /* now read into the buffer */ newlen = fread(dvi->buffer.data + dvi->buffer.length, 1, dvi->buffer.size - dvi->buffer.length, dvi->in); if(newlen == -1) { mdvi_error("%s: %s\n", dvi->filename, strerror(errno)); return -1; } dvi->buffer.length += newlen; dvi->buffer.pos = 0; }
char *read_string(FILE *in, int s, char *buffer, size_t len) { int n; char *str; n = fugetn(in, s ? s : 1); if((str = buffer) == NULL || n + 1 > len) str = mdvi_malloc(n + 1); if(fread(str, 1, n, in) != n) { if(str != buffer) mdvi_free(str); return NULL; } str[n] = 0; return str; }
BITMAP *bitmap_alloc_raw(int w, int h) { BITMAP *bm; bm = xalloc(BITMAP); bm->width = w; bm->height = h; bm->stride = BM_BYTES_PER_LINE(bm); if(h && bm->stride) bm->data = (BmUnit *)mdvi_malloc(h * bm->stride); else bm->data = NULL; return bm; }
char *buff_gets(Buffer *buf, size_t *length) { char *ptr; char *ret; size_t len; ptr = strchr(buf->data, '\n'); if(ptr == NULL) return NULL; ptr++; /* include newline */ len = ptr - buf->data; ret = mdvi_malloc(len + 1); if(len > 0) { memcpy(ret, buf->data, len); memmove(buf->data, buf->data + len, buf->length - len); buf->length -= len; } ret[len] = 0; if(length) *length = len; return ret; }
void epsf_special(DviContext *dvi, char *prefix, char *arg) { char *file; char *special; char *psfile; char *tmp; EpsfBox box = {0, 0, 0, 0}; int x, y; int w, h; double xf, vf; struct stat buf; file = parse_epsf_special(&box, &special, prefix, arg); if (file != NULL) mdvi_free (special); xf = dvi->params.dpi * dvi->params.mag / (72.0 * dvi->params.hshrink); vf = dvi->params.vdpi * dvi->params.mag / (72.0 * dvi->params.vshrink); w = FROUND(box.bw * xf); h = FROUND(box.bh * vf); x = FROUND(box.ox * xf) + dvi->pos.hh; y = FROUND(box.oy * vf) + dvi->pos.vv - h + 1; if (!file || !dvi->device.draw_ps) { dvi->device.draw_rule (dvi, x, y, w, h, 0); return; } if (file[0] == '/') { /* Absolute path */ if (stat (file, &buf) == 0) dvi->device.draw_ps (dvi, file, x, y, w, h); else dvi->device.draw_rule (dvi, x, y, w, h, 0); return; } tmp = mdvi_strrstr (dvi->filename, "/"); if (tmp) { /* Document directory */ int path_len = strlen (dvi->filename) - strlen (tmp + 1); int file_len = strlen (file); psfile = mdvi_malloc (path_len + file_len + 1); psfile[0] = '\0'; strncat (psfile, dvi->filename, path_len); strncat (psfile, file, file_len); if (stat (psfile, &buf) == 0) { dvi->device.draw_ps (dvi, psfile, x, y, w, h); mdvi_free (psfile); return; } mdvi_free (psfile); } psfile = mdvi_build_path_from_cwd (file); if (stat (psfile, &buf) == 0) { /* Current working dir */ dvi->device.draw_ps (dvi, psfile, x, y, w, h); mdvi_free (psfile); return; } mdvi_free (psfile); psfile = kpse_find_pict (file); if (psfile) { /* kpse */ dvi->device.draw_ps (dvi, psfile, x, y, w, h); } else { dvi->device.draw_rule(dvi, x, y, w, h, 0); } free (psfile); }