1 | #include "libtrace.h" |
---|
2 | #include "libtrace_int.h" |
---|
3 | #include "libtraceio.h" |
---|
4 | #include <sys/types.h> /* for ssize_t/off_t */ |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | |
---|
8 | #include <errno.h> /* for debugging */ |
---|
9 | |
---|
10 | struct libtrace_io_t { |
---|
11 | FILE *file; |
---|
12 | }; |
---|
13 | |
---|
14 | ssize_t libtrace_io_read(libtrace_io_t *io, void *buf, size_t len) |
---|
15 | { |
---|
16 | int ret=fread(buf,1,len,io->file); |
---|
17 | |
---|
18 | if (ret==(int)len) { |
---|
19 | printf("read %i bytes\n",ret); |
---|
20 | return len; |
---|
21 | } |
---|
22 | |
---|
23 | /* EOF or an Error occurred */ |
---|
24 | if (ferror(io->file)) { |
---|
25 | int err=errno; |
---|
26 | perror("fread"); |
---|
27 | errno=err; |
---|
28 | /* errno will be set */ |
---|
29 | return -1; |
---|
30 | } |
---|
31 | |
---|
32 | printf("eof\n"); |
---|
33 | |
---|
34 | return 0; /* EOF */ |
---|
35 | } |
---|
36 | |
---|
37 | libtrace_io_t *libtrace_io_fdopen(int fd, const char *mode) |
---|
38 | { |
---|
39 | libtrace_io_t *io = malloc(sizeof(libtrace_io_t)); |
---|
40 | io->file = fdopen(fd,mode); |
---|
41 | return io; |
---|
42 | } |
---|
43 | |
---|
44 | libtrace_io_t *libtrace_io_open(const char *path, const char *mode) |
---|
45 | { |
---|
46 | libtrace_io_t *io = malloc(sizeof(libtrace_io_t)); |
---|
47 | io->file = fopen(path,mode); |
---|
48 | return io; |
---|
49 | } |
---|
50 | |
---|
51 | /* Technically close returns -1 on failure, but if the close fails, really |
---|
52 | * what are you going to do about it? |
---|
53 | */ |
---|
54 | void libtrace_io_close(libtrace_io_t *io) |
---|
55 | { |
---|
56 | fclose(io->file); |
---|
57 | io->file=NULL; |
---|
58 | free(io); |
---|
59 | } |
---|
60 | |
---|
61 | ssize_t libtrace_io_write(libtrace_io_t *io, const void *buf, size_t len) |
---|
62 | { |
---|
63 | return fwrite(buf,len,1,io->file); |
---|
64 | } |
---|
65 | |
---|
66 | off_t libtrace_io_seek(libtrace_io_t *io, off_t offset, int whence) |
---|
67 | { |
---|
68 | return fseek(io->file,offset,whence); |
---|
69 | } |
---|
70 | |
---|
71 | ssize_t libtrace_io_tell(libtrace_io_t *io) |
---|
72 | { |
---|
73 | return ftell(io->file); |
---|
74 | } |
---|