fix serious typo
[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 "write.h"
24 #include "error.h"
25
26 const char *writer_names[] ={WRITER_NAMES};
27 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
28
29 int wng_write(struct writer_node_group *g, char *buf, size_t *loaded)
30 {
31 int ret, i, need_more_writes = 1;
32 size_t min_written = 0;
33
34 while (need_more_writes) {
35 need_more_writes = 0;
36 FOR_EACH_WRITER_NODE(i, g) {
37 size_t w = g->written[i];
38 int bytes_to_write;
39 struct writer_node *wn = &g->writer_nodes[i];
40 if (!i)
41 min_written = w;
42 else
43 min_written = PARA_MIN(min_written, w);
44 if (w == *loaded)
45 continue;
46 if (!g->eof && (*loaded < wn->chunk_bytes + w))
47 continue;
48 bytes_to_write = PARA_MIN(wn->chunk_bytes,
49 *loaded - w);
50 ret = wn->writer->write(buf + w, bytes_to_write, wn);
51 if (ret < 0)
52 goto out;
53 if (ret != bytes_to_write)
54 PARA_WARNING_LOG("short write: %d/%d\n", ret,
55 bytes_to_write);
56 g->written[i] += ret;
57 need_more_writes = 1;
58 }
59 }
60 *loaded -= min_written;
61 ret = 0;
62 if (g->eof)
63 goto out;
64 if (*loaded)
65 memmove(buf, buf + min_written, *loaded);
66 FOR_EACH_WRITER_NODE(i, g)
67 g->written[i] -= min_written;
68 ret = 1;
69 out:
70 return ret;
71 }
72
73 int wng_open(struct writer_node_group *g)
74 {
75 int i, ret = 1;
76
77 FOR_EACH_WRITER_NODE(i, g) {
78 struct writer_node *wn = &g->writer_nodes[i];
79 ret = wn->writer->open(wn);
80 if (ret < 0)
81 goto out;
82 wn->chunk_bytes = ret;
83 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
84 }
85 out:
86 return ret;
87 }
88
89 void wng_close(struct writer_node_group *g)
90 {
91 int i;
92
93 FOR_EACH_WRITER_NODE(i, g) {
94 struct writer_node *wn = &g->writer_nodes[i];
95 wn->writer->close(wn);
96 }
97 }
98
99 struct writer_node_group *wng_new(unsigned num_writers)
100 {
101 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
102 g->num_writers = num_writers;
103 g->writer_nodes = para_calloc(num_writers
104 * sizeof(struct writer_node));
105 g->written = para_calloc(num_writers * sizeof(size_t));
106 return g;
107 }
108
109 void wng_destroy(struct writer_node_group *g)
110 {
111 if (!g)
112 return;
113 free(g->written);
114 free(g->writer_nodes);
115 free(g);
116 }
117
118 void init_supported_writers(void)
119 {
120 int i;
121
122 FOR_EACH_WRITER(i)
123 writers[i].init(&writers[i]);
124 }
125
126 int check_writer_arg(const char *arg)
127 {
128 int i, ret = -E_WRITE_COMMON_SYNTAX;
129 char *a = para_strdup(arg), *p = strchr(a, ':');
130 if (p)
131 *p = '\0';
132 p++;
133 FOR_EACH_WRITER(i) {
134 if (strcmp(writer_names[i], a))
135 continue;
136 ret = i;
137 goto out;
138 }
139 out:
140 free(a);
141 return ret;
142 }
143
144 struct writer_node_group *setup_default_wng(void)
145 {
146 struct writer_node_group *wng = wng_new(1);
147 enum writer_enum default_writer;
148
149 if (NUM_SUPPORTED_WRITERS == 1)
150 default_writer = FILE_WRITE;
151 else
152 default_writer = 1;
153 wng->writer_nodes[0].writer = &writers[default_writer];
154 PARA_INFO_LOG("using default writer: %s\n",
155 writer_names[default_writer]);
156 return wng;
157 }