support for binding :commands to keys
[cmus.git] / cmus / read_wrapper.c
blob1260c04531c25768b102f7bd757038839db0a7d9
1 /*
2 * Copyright 2004 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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
17 * 02111-1307, USA.
20 #include <read_wrapper.h>
21 #include <ip.h>
22 #include <file.h>
24 #include <errno.h>
25 #include <string.h>
27 ssize_t read_wrapper(struct input_plugin_data *ip_data, void *buffer, size_t count)
29 int rc;
31 if (ip_data->metaint == 0) {
32 /* no metadata in the stream */
33 return read(ip_data->fd, buffer, count);
36 if (ip_data->counter == ip_data->metaint) {
37 /* read metadata */
38 unsigned char byte;
39 int len;
41 rc = read(ip_data->fd, &byte, 1);
42 if (rc == -1)
43 return -1;
44 if (rc == 0)
45 return 0;
46 if (byte != 0) {
47 len = ((int)byte) * 16;
48 ip_data->metadata[0] = 0;
49 rc = read_all(ip_data->fd, ip_data->metadata, len);
50 if (rc == -1)
51 return -1;
52 if (rc < len) {
53 ip_data->metadata[0] = 0;
54 return 0;
56 ip_data->metadata[len] = 0;
57 ip_data->metadata_changed = 1;
59 ip_data->counter = 0;
61 if (count + ip_data->counter > ip_data->metaint)
62 count = ip_data->metaint - ip_data->counter;
63 rc = read(ip_data->fd, buffer, count);
64 if (rc > 0)
65 ip_data->counter += rc;
66 return rc;