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