forked from sleepybishop/nanorq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
76 lines (61 loc) · 1.65 KB
/
io.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "io.h"
struct fileioctx {
struct ioctx io;
FILE *fp;
};
static size_t fileio_read(struct ioctx *io, void *buf, int len) {
struct fileioctx *fio = (struct fileioctx *)io;
return fread(buf, 1, len, fio->fp);
}
static size_t fileio_write(struct ioctx *io, const void *buf, int len) {
struct fileioctx *fio = (struct fileioctx *)io;
return fwrite(buf, 1, len, fio->fp);
}
static int fileio_seek(struct ioctx *io, const int offset) {
struct fileioctx *fio = (struct fileioctx *)io;
return (fseek(fio->fp, offset, SEEK_SET) == 0);
}
static long fileio_tell(struct ioctx *io) {
struct fileioctx *fio = (struct fileioctx *)io;
return ftell(fio->fp);
}
static void fileio_destroy(struct ioctx *io) {
struct fileioctx *fio = (struct fileioctx *)io;
fclose(fio->fp);
free(fio);
return;
}
static size_t fileio_size(struct ioctx *io) {
struct fileioctx *fio = (struct fileioctx *)io;
long ret = 0;
long pos = ftell(fio->fp);
fseek(fio->fp, 0, SEEK_END);
ret = ftell(fio->fp);
fseek(fio->fp, pos, SEEK_SET);
return ret;
}
struct ioctx *ioctx_from_file(const char *fn, int t) {
struct fileioctx *ret = NULL;
FILE *fp;
if (t) {
fp = fopen(fn, "r");
} else {
fp = fopen(fn, "w+"); // create decoder
}
if (!fp)
return NULL;
ret = calloc(1, sizeof(struct fileioctx));
ret->fp = fp;
ret->io.read = fileio_read;
ret->io.write = fileio_write;
ret->io.seek = fileio_seek;
ret->io.size = fileio_size;
ret->io.tell = fileio_tell;
ret->io.destroy = fileio_destroy;
ret->io.seekable = true;
return (struct ioctx *)ret;
}