struct audio_format_handler: use proper pointer type for the init function
[paraslash.git] / file_writer.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file file_writer.c simple output plugin for testing purposes */
20
21 #include "para.h"
22 #include "write.h"
23 #include "string.h"
24 #include "error.h"
25
26 /** data specific to the file writer */
27 struct private_file_writer_data {
28 /** the file descriptor of the output file */
29 int fd;
30 };
31
32 static int file_writer_open(struct writer_node *w)
33 {
34 struct private_file_writer_data *pfwd = para_calloc(
35 sizeof(struct private_file_writer_data));
36 char *tmp = para_tmpname(), *home = para_homedir(),
37 *filename = make_message("%s/.paraslash/%s", home, tmp);
38
39 free(home);
40 free(tmp);
41 w->private_data = pfwd;
42 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
43 free(filename);
44 if (pfwd->fd >= 0)
45 return 8192;
46 free(pfwd);
47 return -E_FW_OPEN;
48 }
49
50 static int file_writer_write(char *data, size_t nbytes, struct writer_node *wn)
51 {
52 struct private_file_writer_data *pfwd = wn->private_data;
53 int ret = write(pfwd->fd, data, nbytes);
54 if (ret < 0)
55 ret = -E_FW_WRITE;
56 return ret;
57 }
58
59 static void file_writer_close(struct writer_node *wn)
60 {
61 struct private_file_writer_data *pfwd = wn->private_data;
62 close(pfwd->fd);
63 free(pfwd);
64 }
65
66 /** the init function of the file writer */
67 void file_writer_init(struct writer *w)
68 {
69 w->open = file_writer_open;
70 w->write = file_writer_write;
71 w->close = file_writer_close;
72 w->shutdown = NULL; /* nothing to do */
73 }