blob: 4b165552b5a21be91d19db45375a61ab828a0aa8 [file] [log] [blame]
Sylvain Munautd9fb0e32010-11-11 20:41:04 +01001/* Process Queue: File handling tasks */
2
3/*
4 * This file is part of gapk (GSM Audio Pocket Knife).
5 *
6 * gapk is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * gapk is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with gapk. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <errno.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <gapk/codecs.h>
26#include <gapk/formats.h>
27#include <gapk/procqueue.h>
28
29
30struct pq_state_file {
31 FILE *fh;
32 unsigned int blk_len;
33};
34
35
36static int
37pq_cb_file_input(void *_state, uint8_t *out, const uint8_t *in)
38{
39 struct pq_state_file *state = _state;
40 int rv;
41 rv = fread(out, state->blk_len, 1, state->fh);
42 return rv == 1 ? 0 : -1;
43}
44
45static int
46pq_cb_file_output(void *_state, uint8_t *out, const uint8_t *in)
47{
48 struct pq_state_file *state = _state;
49 int rv;
50 rv = fwrite(in, state->blk_len, 1, state->fh);
51 return rv == 1 ? 0 : -1;
52}
53
54static void
55pq_cb_file_exit(void *_state)
56{
57 free(_state);
58}
59
60static int
61pq_queue_file_op(struct pq *pq, FILE *fh, unsigned int blk_len, int in_out_n)
62{
63 struct pq_item *item;
64 struct pq_state_file *state;
65
66 state = calloc(1, sizeof(struct pq_state_file));
67 if (!state)
68 return -ENOMEM;
69
70 state->fh = fh;
71 state->blk_len = blk_len;
72
73 item = pq_add_item(pq);
74 if (!item) {
75 free(state);
76 return -ENOMEM;
77 }
78
79 item->len_in = in_out_n ? 0 : blk_len;
80 item->len_out = in_out_n ? blk_len : 0;
81 item->state = state;
82 item->proc = in_out_n ? pq_cb_file_input : pq_cb_file_output;
83 item->exit = pq_cb_file_exit;
84
85 return 0;
86}
87
88
89int
90pq_queue_file_input(struct pq *pq, FILE *src, unsigned int blk_len)
91{
92 return pq_queue_file_op(pq, src, blk_len, 1);
93}
94
95int
96pq_queue_file_output(struct pq *pq, FILE *dst, unsigned int blk_len)
97{
98 return pq_queue_file_op(pq, dst, blk_len, 0);
99}