de0502f52c5ef3dd2cbb614857eb03c8c87e3470
[paraslash.git] / write_common.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 write_common.c common functions of para_audiod and para_write */
20
21 #include "para.h"
22 #include "string.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "write.h"
26 #include "error.h"
27
28 const char *writer_names[] ={WRITER_NAMES};
29 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
30
31 static void wng_post_select(__a_unused struct sched *s, struct task *t)
32 {
33 struct writer_node_group *g = t->private_data;
34 int i;
35 size_t min_written = 0;
36
37 FOR_EACH_WRITER_NODE(i, g) {
38 struct writer_node *wn = &g->writer_nodes[i];
39 t->ret = wn->task.ret;
40 if (t->ret < 0)
41 return;
42 if (!i)
43 min_written = t->ret;
44 else
45 min_written = PARA_MIN(min_written, t->ret);
46 }
47 *g->loaded -= min_written;
48 if (!*g->loaded && *g->input_eof)
49 t->ret = -E_WNG_EOF;
50 else
51 t->ret = 1;
52 if (*g->loaded && min_written)
53 memmove(g->buf, g->buf + min_written, *g->loaded);
54 }
55
56 int wng_open(struct writer_node_group *g)
57 {
58 int i, ret = 1;
59
60 FOR_EACH_WRITER_NODE(i, g) {
61 struct writer_node *wn = &g->writer_nodes[i];
62 ret = wn->writer->open(wn);
63 if (ret < 0)
64 goto out;
65 wn->chunk_bytes = ret;
66 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
67 wn->wng = g;
68 PARA_DEBUG_LOG("pre_select: %p\n", &wn->writer->pre_select);
69 PARA_DEBUG_LOG("post_select: %p\n", &wn->writer->post_select);
70 wn->task.pre_select = wn->writer->pre_select;
71 wn->task.post_select = wn->writer->post_select;
72 wn->task.private_data = wn;
73 register_task(&wn->task);
74 }
75 sprintf(g->task.status, "%s", "writer node group");
76 g->eof = 0;
77 register_task(&g->task);
78 out:
79 return ret;
80 }
81
82 void wng_destroy(struct writer_node_group *g)
83 {
84 if (!g)
85 return;
86 free(g->written);
87 free(g->writer_nodes);
88 free(g);
89 }
90
91 void wng_close(struct writer_node_group *g)
92 {
93 int i;
94
95 FOR_EACH_WRITER_NODE(i, g) {
96 struct writer_node *wn = &g->writer_nodes[i];
97 unregister_task(&wn->task);
98 wn->writer->close(wn);
99 }
100 }
101
102 static void wng_event_handler(struct task *t)
103 {
104 struct writer_node_group *g = t->private_data;
105
106 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
107 unregister_task(t);
108 wng_close(g);
109 wng_destroy(g);
110 }
111
112 struct writer_node_group *wng_new(unsigned num_writers)
113 {
114 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
115 g->num_writers = num_writers;
116 g->writer_nodes = para_calloc(num_writers
117 * sizeof(struct writer_node));
118 g->written = para_calloc(num_writers * sizeof(size_t));
119 g->task.private_data = g;
120 g->task.post_select = wng_post_select;
121 g->task.event_handler = wng_event_handler;
122 g->task.flags = POST_ADD_TAIL;
123 return g;
124 }
125
126 void init_supported_writers(void)
127 {
128 int i;
129
130 FOR_EACH_WRITER(i)
131 writers[i].init(&writers[i]);
132 }
133
134 void *check_writer_arg(char *wa, int *writer_num)
135 {
136 int i;
137
138 PARA_INFO_LOG("checking %s\n", wa);
139 FOR_EACH_WRITER(i) {
140 const char *name = writer_names[i];
141 size_t len = strlen(name);
142 char c;
143 if (strlen(wa) < len)
144 continue;
145 if (strncmp(name, wa, len))
146 continue;
147 c = wa[len];
148 if (c && c != ' ')
149 continue;
150 if (c && !writers[i].parse_config)
151 return NULL;
152 *writer_num = i;
153 return writers[i].parse_config(c? wa + len + 1 : "");
154 }
155 PARA_ERROR_LOG("%s", "writer not found\n");
156 return NULL;
157 }
158
159 struct writer_node_group *setup_default_wng(void)
160 {
161 struct writer_node_group *wng = wng_new(1);
162 enum writer_enum default_writer;
163
164 if (NUM_SUPPORTED_WRITERS == 1)
165 default_writer = FILE_WRITE;
166 else
167 default_writer = 1;
168 wng->writer_nodes[0].writer = &writers[default_writer];
169 sprintf(wng->writer_nodes[0].task.status, "%s",
170 writer_names[default_writer]);
171 PARA_INFO_LOG("using default writer: %s %p\n",
172 writer_names[default_writer], writers[default_writer].parse_config);
173 wng->writer_nodes[0].conf = writers[default_writer].parse_config("");
174 return wng;
175 }