1/*
2 * page.c
3 *
4 * map page numbers to file position
5 */
6
7#ifdef HAVE_CONFIG_H
8# include "config.h"
9#endif
10
11#include <X11/IntrinsicP.h>
12#include <X11/StringDefs.h>
13#include <X11/Xos.h>
14#include <stdio.h>
15#include <ctype.h>
16#include "DviP.h"
17
18static DviFileMap *
19MapPageNumberToFileMap(DviWidget dw, int number)
20{
21    DviFileMap *m;
22
23    for (m = dw->dvi.file_map; m; m = m->next) {
24        if (m->page_number == number)
25            break;
26    }
27    return m;
28}
29
30void
31DestroyFileMap(DviFileMap *m)
32{
33    DviFileMap *next;
34
35    for (; m; m = next) {
36        next = m->next;
37        XtFree((char *) m);
38    }
39}
40
41void
42ForgetPagePositions(DviWidget dw)
43{
44    DestroyFileMap(dw->dvi.file_map);
45    dw->dvi.file_map = NULL;
46}
47
48void
49RememberPagePosition(DviWidget dw, int number)
50{
51    DviFileMap *m = MapPageNumberToFileMap(dw, number);
52
53    if (!m) {
54        m = (DviFileMap *) XtMalloc(sizeof *m);
55        m->page_number = number;
56        m->next = dw->dvi.file_map;
57        dw->dvi.file_map = m;
58    }
59    if (dw->dvi.tmpFile)
60        m->position = ftell(dw->dvi.tmpFile);
61    else
62        m->position = ftell(dw->dvi.file);
63}
64
65long
66SearchPagePosition(DviWidget dw, int number)
67{
68    DviFileMap *m = MapPageNumberToFileMap(dw, number);
69
70    if (!m)
71        return -1;
72    return m->position;
73}
74
75void
76FileSeek(DviWidget dw, long position)
77{
78    if (dw->dvi.tmpFile) {
79        dw->dvi.readingTmp = 1;
80        fseek(dw->dvi.tmpFile, position, SEEK_SET);
81    }
82    else
83        fseek(dw->dvi.file, position, SEEK_SET);
84}
85