2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
20 utils_strsep(char **stringp
, const char *delim
)
25 if (*stringp
== NULL
) {
30 strptr
= strstr(*stringp
, delim
);
36 *stringp
= strptr
+strlen(delim
);
41 utils_read_file(char **dst
, const char *filename
)
48 /* Open stream for reading */
49 stream
= fopen(filename
, "rb");
54 /* Find out file size */
55 fseek(stream
, 0, SEEK_END
);
56 filesize
= ftell(stream
);
57 fseek(stream
, 0, SEEK_SET
);
59 /* Allocate one extra byte for zero */
60 buffer
= malloc(filesize
+1);
66 /* Read data in a loop to buffer */
69 int ret
= fread(buffer
+read_bytes
, 1,
70 filesize
-read_bytes
, stream
);
75 } while (read_bytes
< filesize
);
77 /* Add final null byte and close stream */
78 buffer
[read_bytes
] = '\0';
81 /* If read didn't finish, return error */
82 if (read_bytes
!= filesize
) {
93 utils_hwaddr_raop(char *str
, int strlen
, const char *hwaddr
, int hwaddrlen
)
97 /* Check that our string is long enough */
98 if (strlen
== 0 || strlen
< 2*hwaddrlen
+1)
101 /* Convert hardware address to hex string */
102 for (i
=0,j
=0; i
<hwaddrlen
; i
++) {
103 int hi
= (hwaddr
[i
]>>4) & 0x0f;
104 int lo
= hwaddr
[i
] & 0x0f;
106 if (hi
< 10) str
[j
++] = '0' + hi
;
107 else str
[j
++] = 'A' + hi
-10;
108 if (lo
< 10) str
[j
++] = '0' + lo
;
109 else str
[j
++] = 'A' + lo
-10;
112 /* Add string terminator */
118 utils_hwaddr_airplay(char *str
, int strlen
, const char *hwaddr
, int hwaddrlen
)
122 /* Check that our string is long enough */
123 if (strlen
== 0 || strlen
< 2*hwaddrlen
+hwaddrlen
)
126 /* Convert hardware address to hex string */
127 for (i
=0,j
=0; i
<hwaddrlen
; i
++) {
128 int hi
= (hwaddr
[i
]>>4) & 0x0f;
129 int lo
= hwaddr
[i
] & 0x0f;
131 if (hi
< 10) str
[j
++] = '0' + hi
;
132 else str
[j
++] = 'a' + hi
-10;
133 if (lo
< 10) str
[j
++] = '0' + lo
;
134 else str
[j
++] = 'a' + lo
-10;
139 /* Add string terminator */