a413d47e2dd0d3c75d4ef26f7a630325685c5d14
[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 g->eof = 1;
42 return;
43 }
44 if (!i)
45 min_written = t->ret;
46 else
47 min_written = PARA_MIN(min_written, t->ret);
48 }
49 *g->loaded -= min_written;
50 if (!*g->loaded && *g->input_eof) {
51 g->eof = 1;
52 t->ret = -E_WNG_EOF;
53 } else
54 t->ret = 1;
55 if (*g->loaded && min_written)
56 memmove(g->buf, g->buf + min_written, *g->loaded);
57 }
58
59 int wng_open(struct writer_node_group *g)
60 {
61 int i, ret = 1;
62
63 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
64 FOR_EACH_WRITER_NODE(i, g) {
65 struct writer_node *wn = &g->writer_nodes[i];
66 wn->wng = g;
67 ret = wn->writer->open(wn);
68 if (ret < 0)
69 goto err_out;
70 wn->chunk_bytes = ret;
71 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
72 wn->task.pre_select = wn->writer->pre_select;
73 wn->task.post_select = wn->writer->post_select;
74 wn->task.private_data = wn;
75 register_task(&wn->task);
76 }
77 sprintf(g->task.status, "%s", "writer node group");
78 register_task(&g->task);
79 g->eof = 0;
80 return 1;
81 err_out:
82 while (i > 0) {
83 struct writer_node *wn = &g->writer_nodes[--i];
84 unregister_task(&wn->task);
85 wn->writer->close(wn);
86 }
87 g->num_writers = 0;
88 g->eof = 1;
89 return ret;
90 }
91
92 void wng_unregister(struct writer_node_group *g)
93 {
94 int i;
95
96 FOR_EACH_WRITER_NODE(i, g) {
97 struct writer_node *wn = &g->writer_nodes[i];
98 unregister_task(&wn->task);
99 }
100 unregister_task(&g->task);
101 g->eof = 1;
102 }
103
104 void wng_close(struct writer_node_group *g)
105 {
106 int i;
107
108 if (!g)
109 return;
110 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
111 FOR_EACH_WRITER_NODE(i, g) {
112 struct writer_node *wn = &g->writer_nodes[i];
113 wn->writer->close(wn);
114 }
115 free(g->written);
116 free(g->writer_nodes);
117 free(g);
118 }
119
120 struct writer_node_group *wng_new(unsigned num_writers)
121 {
122 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
123 g->num_writers = num_writers;
124 g->writer_nodes = para_calloc(num_writers
125 * sizeof(struct writer_node));
126 g->written = para_calloc(num_writers * sizeof(size_t));
127 g->task.private_data = g;
128 g->task.post_select = wng_post_select;
129 g->task.flags = POST_ADD_TAIL;
130 return g;
131 }
132
133 void init_supported_writers(void)
134 {
135 int i;
136
137 FOR_EACH_WRITER(i)
138 writers[i].init(&writers[i]);
139 }
140
141 void *check_writer_arg(char *wa, int *writer_num)
142 {
143 int i;
144
145 *writer_num = -E_WRITE_COMMON_SYNTAX;
146 PARA_INFO_LOG("checking %s\n", wa);
147 FOR_EACH_WRITER(i) {
148 const char *name = writer_names[i];
149 size_t len = strlen(name);
150 char c;
151 if (strlen(wa) < len)
152 continue;
153 if (strncmp(name, wa, len))
154 continue;
155 c = wa[len];
156 if (c && c != ' ')
157 continue;
158 if (c && !writers[i].parse_config)
159 return NULL;
160 *writer_num = i;
161 return writers[i].parse_config(c? wa + len + 1 : "");
162 }
163 PARA_ERROR_LOG("%s", "writer not found\n");
164 return NULL;
165 }
166
167 struct writer_node_group *setup_default_wng(void)
168 {
169 struct writer_node_group *wng = wng_new(1);
170 enum writer_enum default_writer;
171
172 if (NUM_SUPPORTED_WRITERS == 1)
173 default_writer = FILE_WRITE;
174 else
175 default_writer = 1;
176 wng->writer_nodes[0].writer = &writers[default_writer];
177 sprintf(wng->writer_nodes[0].task.status, "%s",
178 writer_names[default_writer]);
179 PARA_INFO_LOG("using default writer: %s %p\n",
180 writer_names[default_writer], writers[default_writer].parse_config);
181 wng->writer_nodes[0].conf = writers[default_writer].parse_config("");
182 return wng;
183 }