/
mm_loc.c
55 lines (45 loc) · 906 Bytes
/
mm_loc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>
#include "mm.h"
void mm_loc_free(struct mm_loc *p)
{
if (p && p->text) xfree(p->text);
if (p) xfree(p);
}
struct mm_loc *mm_loc_new_literal(char *s, unsigned int len)
{
struct mm_loc *p;
p = malloc(sizeof(struct mm_loc));
if (p == NULL) return p;
p->off = -1;
p->text = s;
p->fp = NULL;
p->len = len;
return p;
}
char *mm_loc_string(struct mm_loc *p)
{
fpos_t fp;
char *x;
if (!p->len) return "";
if (p->fp) {
x = (char*)xmalloc(p->len);
fgetpos(p->fp, &fp);
fseek(p->fp, p->off, SEEK_SET);
if (fread(x, p->len, 1, p->fp) != 1) return x; /* err... */
fsetpos(p->fp, &fp);
p->text = x;
p->fp = NULL;
}
if (p->text) return p->text;
}
FILE *mm_loc_reader(struct mm_loc *p)
{
if (!p->fp) return NULL;
fgetpos(p->fp, &p->pos);
fseek(p->fp, p->off, SEEK_SET);
return p->fp;
}
void mm_loc_donereading(struct mm_loc *p)
{
fsetpos(p->fp, &p->pos);
}