2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
24 const char *writer_names[] ={WRITER_NAMES};
25 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
27 int wng_write(struct writer_node_group *g, char *buf, size_t *loaded)
29 int ret, i, need_more_writes = 1;
30 size_t min_written = 0;
32 while (need_more_writes) {
34 FOR_EACH_WRITER_NODE(i, g) {
35 size_t w = g->written[i];
36 unsigned char *p = buf + w;
38 struct writer_node *wn = &g->writer_nodes[i];
42 min_written = PARA_MIN(min_written, w);
45 if (!g->eof && (*loaded < wn->chunk_bytes + w))
47 bytes_to_write = PARA_MIN(wn->chunk_bytes,
49 ret = wn->writer->write(p, bytes_to_write, wn);
52 if (ret != bytes_to_write)
53 PARA_WARNING_LOG("short write: %d/%d\n", ret,
59 *loaded -= min_written;
64 memmove(buf, buf + min_written, *loaded);
65 FOR_EACH_WRITER_NODE(i, g)
66 g->written[i] -= min_written;
72 int wng_open(struct writer_node_group *g)
76 FOR_EACH_WRITER_NODE(i, g) {
77 struct writer_node *wn = &g->writer_nodes[i];
78 ret = wn->writer->open(wn);
81 wn->chunk_bytes = ret;
82 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
88 void wng_close(struct writer_node_group *g)
92 FOR_EACH_WRITER_NODE(i, g) {
93 struct writer_node *wn = &g->writer_nodes[i];
94 wn->writer->close(wn);
98 struct writer_node_group *wng_new(unsigned num_writers)
100 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
101 g->num_writers = num_writers;
102 g->writer_nodes = para_calloc(num_writers
103 * sizeof(struct writer_node));
104 g->written = para_calloc(num_writers * sizeof(size_t));
108 void wng_destroy(struct writer_node_group *g)
113 free(g->writer_nodes);
117 void init_supported_writers(void)
122 writers[i].init(&writers[i]);
125 int check_writer_arg(const char *arg)
127 int i, ret = -E_WRITE_COMMON_SYNTAX;
128 char *a = para_strdup(arg), *p = strchr(a, ':');
133 if (strcmp(writer_names[i], a))
143 struct writer_node_group *setup_default_wng(void)
145 struct writer_node_group *wng = wng_new(1);
146 enum writer_enum default_writer;
148 if (NUM_SUPPORTED_WRITERS == 1)
149 default_writer = FILE_WRITE;
152 wng->writer_nodes[0].writer = &writers[default_writer];
153 PARA_INFO_LOG("using default writer: %s\n",
154 writer_names[default_writer]);