Rev 4680 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4680 | right-hear | 1 | #include "fitz.h" |
2 | |||
3 | fz_stream * |
||
4 | fz_new_stream(void *state, |
||
5 | int(*read)(fz_stream *stm, unsigned char *buf, int len), |
||
6 | void(*close)(fz_stream *stm)) |
||
7 | { |
||
8 | fz_stream *stm; |
||
9 | |||
10 | stm = fz_malloc(sizeof(fz_stream)); |
||
11 | |||
12 | stm->refs = 1; |
||
13 | stm->error = 0; |
||
14 | stm->eof = 0; |
||
15 | stm->pos = 0; |
||
16 | |||
17 | stm->bits = 0; |
||
18 | stm->avail = 0; |
||
19 | |||
20 | stm->bp = stm->buf; |
||
21 | stm->rp = stm->bp; |
||
22 | stm->wp = stm->bp; |
||
23 | stm->ep = stm->buf + sizeof stm->buf; |
||
24 | |||
25 | stm->state = state; |
||
26 | stm->read = read; |
||
27 | stm->close = close; |
||
28 | stm->seek = NULL; |
||
29 | |||
30 | return stm; |
||
31 | } |
||
32 | |||
33 | fz_stream * |
||
34 | fz_keep_stream(fz_stream *stm) |
||
35 | { |
||
36 | stm->refs ++; |
||
37 | return stm; |
||
38 | } |
||
39 | |||
40 | void |
||
41 | fz_close(fz_stream *stm) |
||
42 | { |
||
43 | stm->refs --; |
||
44 | if (stm->refs == 0) |
||
45 | { |
||
46 | if (stm->close) |
||
47 | stm->close(stm); |
||
48 | fz_free(stm); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /* File stream */ |
||
53 | |||
54 | static int read_file(fz_stream *stm, unsigned char *buf, int len) |
||
55 | { |
||
56 | int n = read(*(int*)stm->state, buf, len); |
||
57 | if (n < 0) |
||
58 | return fz_throw("read error: %s", strerror(errno)); |
||
59 | return n; |
||
60 | } |
||
61 | |||
62 | static void seek_file(fz_stream *stm, int offset, int whence) |
||
63 | { |
||
64 | int n = lseek(*(int*)stm->state, offset, whence); |
||
65 | if (n < 0) |
||
66 | fz_warn("cannot lseek: %s", strerror(errno)); |
||
67 | stm->pos = n; |
||
68 | stm->rp = stm->bp; |
||
69 | stm->wp = stm->bp; |
||
70 | } |
||
71 | |||
72 | static void close_file(fz_stream *stm) |
||
73 | { |
||
74 | int n = close(*(int*)stm->state); |
||
75 | if (n < 0) |
||
76 | fz_warn("close error: %s", strerror(errno)); |
||
77 | fz_free(stm->state); |
||
78 | } |
||
79 | |||
80 | fz_stream * |
||
81 | fz_open_fd(int fd) |
||
82 | { |
||
83 | fz_stream *stm; |
||
84 | int *state; |
||
85 | |||
86 | state = fz_malloc(sizeof(int)); |
||
87 | *state = fd; |
||
88 | |||
89 | stm = fz_new_stream(state, read_file, close_file); |
||
90 | stm->seek = seek_file; |
||
91 | |||
92 | return stm; |
||
93 | } |
||
94 | |||
95 | fz_stream * |
||
96 | fz_open_file(const char *name) |
||
97 | { |
||
98 | int fd = open(name, O_BINARY | O_RDONLY, 0); |
||
99 | if (fd == -1) |
||
100 | return NULL; |
||
101 | return fz_open_fd(fd); |
||
102 | } |
||
103 | |||
104 | #ifdef _WIN32 |
||
105 | fz_stream * |
||
106 | fz_open_file_w(const wchar_t *name) |
||
107 | { |
||
108 | int fd = _wopen(name, O_BINARY | O_RDONLY, 0); |
||
109 | if (fd == -1) |
||
110 | return NULL; |
||
111 | return fz_open_fd(fd); |
||
112 | } |
||
113 | #endif |
||
114 | |||
115 | /* Memory stream */ |
||
116 | |||
117 | static int read_buffer(fz_stream *stm, unsigned char *buf, int len) |
||
118 | { |
||
119 | return 0; |
||
120 | } |
||
121 | |||
122 | static void seek_buffer(fz_stream *stm, int offset, int whence) |
||
123 | { |
||
124 | if (whence == 0) |
||
125 | stm->rp = stm->bp + offset; |
||
126 | if (whence == 1) |
||
127 | stm->rp += offset; |
||
128 | if (whence == 2) |
||
129 | stm->rp = stm->ep - offset; |
||
130 | stm->rp = CLAMP(stm->rp, stm->bp, stm->ep); |
||
131 | stm->wp = stm->ep; |
||
132 | } |
||
133 | |||
134 | static void close_buffer(fz_stream *stm) |
||
135 | { |
||
136 | if (stm->state) |
||
137 | fz_drop_buffer(stm->state); |
||
138 | } |
||
139 | |||
140 | fz_stream * |
||
141 | fz_open_buffer(fz_buffer *buf) |
||
142 | { |
||
143 | fz_stream *stm; |
||
144 | |||
145 | stm = fz_new_stream(fz_keep_buffer(buf), read_buffer, close_buffer); |
||
146 | stm->seek = seek_buffer; |
||
147 | |||
148 | stm->bp = buf->data; |
||
149 | stm->rp = buf->data; |
||
150 | stm->wp = buf->data + buf->len; |
||
151 | stm->ep = buf->data + buf->len; |
||
152 | |||
153 | stm->pos = buf->len; |
||
154 | |||
155 | return stm; |
||
156 | } |
||
157 | |||
158 | fz_stream * |
||
159 | fz_open_memory(unsigned char *data, int len) |
||
160 | { |
||
161 | fz_stream *stm; |
||
162 | |||
163 | stm = fz_new_stream(NULL, read_buffer, close_buffer); |
||
164 | stm->seek = seek_buffer; |
||
165 | |||
166 | stm->bp = data; |
||
167 | stm->rp = data; |
||
168 | stm->wp = data + len; |
||
169 | stm->ep = data + len; |
||
170 | |||
171 | stm->pos = len; |
||
172 | |||
173 | return stm; |
||
174 | }>>> |