Debian Codegraph @

src/cfg_intake.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2017 Moritz Grimm <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include "compat.h"
22
23 #include <sys/queue.h>
24
25 #include <string.h>
26
27 #include "cfg_private.h"
28 #include "cfg_intake.h"
29 #include "xalloc.h"
30
31 struct cfg_intake {
32 TAILQ_ENTRY(cfg_intake) entry;
33 char *name;
34 enum cfg_intake_type type;
35 char filename[PATH_MAX];
36 int shuffle;
37 int stream_once;
38 };
39
40 TAILQ_HEAD(cfg_intake_list, cfg_intake);
41
42 struct cfg_intake_list *
43 cfg_intake_list_create(void)
44 {
45 struct cfg_intake_list *il;
46
47 il = xcalloc(1UL, sizeof(*il));
48 TAILQ_INIT(il);
49
50 return (il);
51 }
52
53 void
54 cfg_intake_list_destroy(cfg_intake_list_t *il_p)
55 {
56 struct cfg_intake_list *il = *il_p;
57 struct cfg_intake *i;
58
59 if (!il)
60 return;
61
62 while (NULL != (i = TAILQ_FIRST(il))) {
63 TAILQ_REMOVE(il, i, entry);
64 cfg_intake_destroy(&i);
65 }
66
67 xfree(il);
68 *il_p = NULL;
69 }
70
71 unsigned int
72 cfg_intake_list_nentries(struct cfg_intake_list *il)
73 {
74 struct cfg_intake *i;
75 unsigned int n = 0;
76
77 TAILQ_FOREACH(i, il, entry) {
78 n++;
79 }
80
81 return (n);
82 }
83
84 struct cfg_intake *
85 cfg_intake_list_find(struct cfg_intake_list *il, const char *name)
86 {
87 struct cfg_intake *i;
88
89 TAILQ_FOREACH(i, il, entry) {
90 if (0 == strcasecmp(i->name, name))
91 return (i);
92 }
93
94 return (NULL);
95 }
96
97 struct cfg_intake *
98 cfg_intake_list_get(struct cfg_intake_list *il, const char *name)
99 {
100 struct cfg_intake *i;
101
102 i = cfg_intake_list_find(il, name);
103 if (i)
104 return (i);
105 i = cfg_intake_create(name);
106 if (!i)
107 return (NULL);
108
109 TAILQ_INSERT_TAIL(il, i, entry);
110
111 return (i);
112 }
113
114 void
115 cfg_intake_list_foreach(struct cfg_intake_list *il,
116 void (*cb)(cfg_intake_t, void *), void *cb_arg)
117 {
118 struct cfg_intake *i;
119
120 TAILQ_FOREACH(i, il, entry) {
121 cb(i, cb_arg);
122 }
123 }
124
125 struct cfg_intake *
126 cfg_intake_create(const char *name)
127 {
128 struct cfg_intake *i;
129
130 if (!name || !name[0])
131 return (NULL);
132
133 i = xcalloc(1UL, sizeof(*i));
134 i->name = xstrdup(name);
135
136 return (i);
137 }
138
139 void
140 cfg_intake_destroy(struct cfg_intake **i_p)
141 {
142 struct cfg_intake *i = *i_p;
143
144 xfree(i->name);
145 xfree(i);
146 *i_p = NULL;
147 }
148
149 int
150 cfg_intake_set_name(struct cfg_intake *i, struct cfg_intake_list *il,
151 const char *name, const char **errstrp)
152 {
153 struct cfg_intake *i2;
154
155 if (!name || !name[0]) {
156 if (errstrp)
157 *errstrp = "empty";
158 return (-1);
159 }
160
161 i2 = cfg_intake_list_find(il, name);
162 if (i2 && i2 != i) {
163 if (errstrp)
164 *errstrp = "already exists";
165 return (-1);
166 }
167
168 SET_XSTRDUP(i->name, name, errstrp);
169
170 return (0);
171 }
172
173 int
174 cfg_intake_set_type(struct cfg_intake *i, struct cfg_intake_list *not_used,
175 const char *type, const char **errstrp)
176 {
177 (void)not_used;
178
179 if (!type || !type[0]) {
180 if (errstrp)
181 *errstrp = "empty";
182 return (-1);
183 }
184
185 if (0 == strcasecmp("autodetect", type))
186 i->type = CFG_INTAKE_AUTODETECT;
187 else if (0 == strcasecmp("file", type))
188 i->type = CFG_INTAKE_FILE;
189 else if (0 == strcasecmp("playlist", type))
190 i->type = CFG_INTAKE_PLAYLIST;
191 else if (0 == strcasecmp("program", type))
192 i->type = CFG_INTAKE_PROGRAM;
193 else if (0 == strcasecmp("stdin", type))
194 i->type = CFG_INTAKE_STDIN;
195 else {
196 if (errstrp)
197 *errstrp = "unsupported";
198 return (-1);
199 }
200 return (0);
201 }
202
203 int
204 cfg_intake_set_filename(struct cfg_intake *i, struct cfg_intake_list *not_used,
205 const char *filename, const char **errstrp)
206 {
207 (void)not_used;
208 SET_STRLCPY(i->filename, filename, errstrp);
209 return (0);
210 }
211
212 int
213 cfg_intake_set_shuffle(struct cfg_intake *i, struct cfg_intake_list *not_used,
214 const char *shuffle, const char **errstrp)
215 {
216 (void)not_used;
217 SET_BOOLEAN(i->shuffle, shuffle, errstrp);
218 return (0);
219 }
220
221 int
222 cfg_intake_set_stream_once(struct cfg_intake *i, struct cfg_intake_list *not_used,
223 const char *stream_once, const char **errstrp)
224 {
225 (void)not_used;
226 SET_BOOLEAN(i->stream_once, stream_once, errstrp);
227 return (0);
228 }
229
230 int
231 cfg_intake_validate(struct cfg_intake *i, const char **errstrp)
232 {
233 if (!cfg_intake_get_filename(i) &&
234 CFG_INTAKE_STDIN != cfg_intake_get_type(i)) {
235 if (NULL != errstrp)
236 *errstrp = "intake filename missing";
237 return (-1);
238 }
239
240 return (0);
241 }
242
243 const char *
244 cfg_intake_get_name(struct cfg_intake *i)
245 {
246 return (i->name);
247 }
248
249 enum cfg_intake_type
250 cfg_intake_get_type(struct cfg_intake *i)
251 {
252 return (i->type);
253 }
254
255 const char *
256 cfg_intake_get_type_str(struct cfg_intake *i)
257 {
258 switch (i->type) {
259 case CFG_INTAKE_FILE:
260 return ("file");
261 case CFG_INTAKE_PLAYLIST:
262 return ("playlist");
263 case CFG_INTAKE_PROGRAM:
264 return ("program");
265 case CFG_INTAKE_STDIN:
266 return ("stdin");
267 case CFG_INTAKE_AUTODETECT:
268 default:
269 return ("autodetect");
270 }
271 }
272
273 const char *
274 cfg_intake_get_filename(struct cfg_intake *i)
275 {
276 return (i->filename[0] ? i->filename : NULL);
277 }
278
279 int
280 cfg_intake_get_shuffle(struct cfg_intake *i)
281 {
282 return (i->shuffle);
283 }
284
285 int
286 cfg_intake_get_stream_once(struct cfg_intake *i)
287 {
288 return (i->stream_once);
289 }