Parent: [85a0e1] (diff)

Child: [2d78fb] (diff)

Download this file

upmpdutils.cxx    369 lines (325 with data), 10.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* Copyright (C) 2014 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//
// This file has a number of mostly uninteresting and badly
// implemented small utility functions. This is a bit ugly, but I am
// not linking to Qt or glib just to get path-concatenating
// functions...
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <pwd.h>
#include <regex.h>
#include <errno.h>
#include <string.h>
#ifndef O_STREAMING
#define O_STREAMING 0
#endif
#include <iostream>
#include <sstream>
using namespace std;
#include "mpdcli.hxx"
#include "upmpdutils.hxx"
// Append system error string to input string
void catstrerror(string *reason, const char *what, int _errno)
{
if (!reason)
return;
if (what)
reason->append(what);
reason->append(": errno: ");
char nbuf[20];
sprintf(nbuf, "%d", _errno);
reason->append(nbuf);
reason->append(" : ");
#ifdef sun
// Note: sun strerror is noted mt-safe ??
reason->append(strerror(_errno));
#else
#define ERRBUFSZ 200
char errbuf[ERRBUFSZ];
// There are 2 versions of strerror_r.
// - The GNU one returns a pointer to the message (maybe
// static storage or supplied buffer).
// - The POSIX one always stores in supplied buffer and
// returns 0 on success. As the possibility of error and
// error code are not specified, we're basically doomed
// cause we can't use a test on the 0 value to know if we
// were returned a pointer...
// Also couldn't find an easy way to disable the gnu version without
// changing the cxxflags globally, so forget it. Recent gnu lib versions
// normally default to the posix version.
// At worse we get no message at all here.
errbuf[0] = 0;
(void)strerror_r(_errno, errbuf, ERRBUFSZ);
reason->append(errbuf);
#endif
}
bool file_to_string(const string &fn, string &data, string *reason)
{
const int RDBUFSZ = 4096;
bool ret = false;
int fd = -1;
struct stat st;
fd = open(fn.c_str(), O_RDONLY|O_STREAMING);
if (fd < 0 || fstat(fd, &st) < 0) {
catstrerror(reason, "open/stat", errno);
return false;
}
data.reserve(st.st_size+1);
char buf[RDBUFSZ];
for (;;) {
int n = read(fd, buf, RDBUFSZ);
if (n < 0) {
catstrerror(reason, "read", errno);
goto out;
}
if (n == 0)
break;
data.append(buf, n);
}
ret = true;
out:
if (fd >= 0)
close(fd);
return ret;
}
void path_catslash(string &s) {
if (s.empty() || s[s.length() - 1] != '/')
s += '/';
}
string path_cat(const string &s1, const string &s2) {
string res = s1;
path_catslash(res);
res += s2;
return res;
}
string path_home()
{
uid_t uid = getuid();
struct passwd *entry = getpwuid(uid);
if (entry == 0) {
const char *cp = getenv("HOME");
if (cp)
return cp;
else
return "/";
}
string homedir = entry->pw_dir;
path_catslash(homedir);
return homedir;
}
string path_tildexpand(const string &s)
{
if (s.empty() || s[0] != '~')
return s;
string o = s;
if (s.length() == 1) {
o.replace(0, 1, path_home());
} else if (s[1] == '/') {
o.replace(0, 2, path_home());
} else {
string::size_type pos = s.find('/');
int l = (pos == string::npos) ? s.length() - 1 : pos - 1;
struct passwd *entry = getpwnam(s.substr(1, l).c_str());
if (entry)
o.replace(0, l+1, entry->pw_dir);
}
return o;
}
void trimstring(string &s, const char *ws)
{
string::size_type pos = s.find_first_not_of(ws);
if (pos == string::npos) {
s.clear();
return;
}
s.replace(0, pos, string());
pos = s.find_last_not_of(ws);
if (pos != string::npos && pos != s.length()-1)
s.replace(pos+1, string::npos, string());
}
string xmlquote(const string& in)
{
string out;
for (unsigned int i = 0; i < in.size(); i++) {
switch(in[i]) {
case '"': out += "&quot;";break;
case '&': out += "&amp;";break;
case '<': out += "&lt;";break;
case '>': out += "&gt;";break;
case '\'': out += "&apos;";break;
default: out += in[i];
}
}
return out;
}
// Translate 0-100% MPD volume to UPnP VolumeDB: we do db upnp-encoded
// values from -10240 (0%) to 0 (100%)
int percentodbvalue(int value)
{
int dbvalue;
if (value == 0) {
dbvalue = -10240;
} else {
float ratio = float(value)*value / 10000.0;
float db = 10 * log10(ratio);
dbvalue = int(256 * db);
}
return dbvalue;
}
#ifdef __APPLE__
#define exp10 __exp10
#endif
// Translate VolumeDB to MPD 0-100
int dbvaluetopercent(int dbvalue)
{
float db = float(dbvalue) / 256.0;
float vol = exp10(db/10);
int percent = floor(sqrt(vol * 10000.0));
if (percent < 0) percent = 0;
if (percent > 100) percent = 100;
return percent;
}
// Format duration in milliseconds into UPnP duration format
string upnpduration(int ms)
{
int hours = ms / (3600 * 1000);
ms -= hours * 3600 * 1000;
int minutes = ms / (60 * 1000);
ms -= minutes * 60 * 1000;
int secs = ms / 1000;
ms -= secs * 1000;
char cbuf[100];
// This is the format from the ref doc, but it appears that the
// decimal part in the seconds field is an issue with some control
// points. So drop it...
// sprintf(cbuf, "%d:%02d:%02d.%03d", hours, minutes, secs, ms);
sprintf(cbuf, "%d:%02d:%02d", hours, minutes, secs);
return cbuf;
}
// H:M:S to seconds
int upnpdurationtos(const string& dur)
{
int hours, minutes, seconds;
if (sscanf(dur.c_str(), "%d:%d:%d", &hours, &minutes, &seconds) != 3) {
return 0;
}
return 3600 * hours + 60 * minutes + seconds;
}
// Get from ssl unordered_map, return empty string for non-existing
// key (so this only works for data where this behaviour makes sense).
const string& mapget(const unordered_map<string, string>& im, const string& k)
{
static string ns; // null string
unordered_map<string, string>::const_iterator it = im.find(k);
if (it == im.end())
return ns;
else
return it->second;
}
// Bogus didl fragment maker. We probably don't need a full-blown XML
// helper here
string didlmake(const MpdStatus& mpds, bool next)
{
const unordered_map<string, string>& songmap =
next? mpds.nextsong : mpds.currentsong;
ostringstream ss;
ss << "<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" "
"xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" "
"xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\" "
"xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\">"
<< "<item restricted=\"1\">";
{ const string& val = mapget(songmap, "dc:title");
ss << "<dc:title>" << xmlquote(val) << "</dc:title>";
}
// TBD Playlists etc?
ss << "<upnp:class>object.item.audioItem.musicTrack</upnp:class>";
{ const string& val = mapget(songmap, "upnp:artist");
if (!val.empty()) {
string a = xmlquote(val);
ss << "<dc:creator>" << a << "</dc:creator>" <<
"<upnp:artist>" << a << "</upnp:artist>";
}
}
{ const string& val = mapget(songmap, "upnp:album");
if (!val.empty()) {
ss << "<upnp:album>" << xmlquote(val) << "</upnp:album>";
}
}
{ const string& val = mapget(songmap, "upnp:genre");
if (!val.empty()) {
ss << "<upnp:genre>" << xmlquote(val) << "</upnp:genre>";
}
}
{const string& val = mapget(songmap, "upnp:originalTrackNumber");
if (!val.empty()) {
ss << "<upnp:originalTrackNumber>" << val <<
"</upnp:originalTrackNumber>";
}
}
// TBD: the res element normally has size, sampleFrequency,
// nrAudioChannels and protocolInfo attributes, which are bogus
// for the moment. partly because MPD does not supply them. And
// mostly everything is bogus if next is set...
ss << "<res " << "duration=\"" << upnpduration(mpds.songlenms) << "\" "
// Bitrate keeps changing for VBRs and forces events. Keeping
// it out for now.
// << "bitrate=\"" << mpds.kbrate << "\" "
<< "sampleFrequency=\"44100\" audioChannels=\"2\" "
<< "protocolInfo=\"http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000\""
<< ">"
<< xmlquote(mapget(songmap, "uri"))
<< "</res>"
<< "</item></DIDL-Lite>";
return ss.str();
}
// Substitute regular expression
// The c++11 regex package does not seem really ready from prime time
// (Tried on gcc + libstdc++ 4.7.2-5 on Debian, with little
// joy). So...:
string regsub1(const string& sexp, const string& input, const string& repl)
{
regex_t expr;
int err;
const int ERRSIZE = 200;
char errbuf[ERRSIZE+1];
regmatch_t pmatch[10];
if ((err = regcomp(&expr, sexp.c_str(), REG_EXTENDED))) {
regerror(err, &expr, errbuf, ERRSIZE);
cerr << "upmpd: regsub1: regcomp() failed: " << errbuf << endl;
return string();
}
if ((err = regexec(&expr, input.c_str(), 10, pmatch, 0))) {
regerror(err, &expr, errbuf, ERRSIZE);
cerr << "upmpd: regsub1: regcomp() failed: " << errbuf << endl;
regfree(&expr);
return string();
}
if (pmatch[0].rm_so == -1) {
// No match
regfree(&expr);
return input;
}
string out = input.substr(0, pmatch[0].rm_so);
out += repl;
out += input.substr(pmatch[0].rm_eo);
regfree(&expr);
return out;
}