reduce usage of pointers / replace std::pair in SearchEngine
[ncmpcpp.git] / src / helpers.cpp
blobb854f90a1d962aa377b83e96a0f6a1600120a77d
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <cassert>
22 #include <cstring>
23 #include <algorithm>
24 #include <iostream>
25 #include <stdexcept>
27 #include "clock.h"
28 #include "charset.h"
29 #include "global.h"
30 #include "helpers.h"
31 #include "playlist.h"
32 #include "status.h"
33 #include "tag_editor.h"
34 #include "help.h"
35 #include "playlist_editor.h"
36 #include "browser.h"
37 #include "media_library.h"
38 #include "search_engine.h"
39 #include "outputs.h"
40 #include "visualizer.h"
42 bool ConnectToMPD()
44 if (!Mpd.Connect())
46 std::cout << "Couldn't connect to MPD (host = " << Mpd.GetHostname() << ", port = " << Mpd.GetPort() << "): " << Mpd.GetErrorMessage() << std::endl;
47 return false;
49 return true;
52 void ParseArgv(int argc, char **argv)
54 bool quit = 0;
55 std::string now_playing_format = "{{{(%l) }{{%a - }%t}}|{%f}}";
57 for (int i = 1; i < argc; ++i)
59 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host"))
61 if (++i >= argc)
62 exit(0);
63 Mpd.SetHostname(argv[i]);
64 continue;
66 if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port"))
68 if (++i >= argc)
69 exit(0);
70 Mpd.SetPort(atoi(argv[i]));
71 continue;
73 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
75 std::cout << "ncmpcpp version: " << VERSION << "\n\n"
76 << "optional screens compiled-in:\n"
77 # ifdef HAVE_TAGLIB_H
78 << " - tag editor\n"
79 << " - tiny tag editor\n"
80 # endif
81 # ifdef HAVE_CURL_CURL_H
82 << " - artist info\n"
83 # endif
84 # ifdef ENABLE_OUTPUTS
85 << " - outputs\n"
86 # endif
87 # ifdef ENABLE_VISUALIZER
88 << " - visualizer\n"
89 # endif
90 # ifdef ENABLE_CLOCK
91 << " - clock\n"
92 # endif
93 << "\nencoding detection: "
94 # ifdef HAVE_LANGINFO_H
95 << "enabled"
96 # else
97 << "disabled"
98 # endif // HAVE_LANGINFO_H
99 << "\nbuilt with support for:"
100 # ifdef HAVE_CURL_CURL_H
101 << " curl"
102 # endif
103 # ifdef HAVE_ICONV_H
104 << " iconv"
105 # endif
106 # ifdef HAVE_FFTW3_H
107 << " fftw"
108 # endif
109 # ifdef USE_PDCURSES
110 << " pdcurses"
111 # else
112 << " ncurses"
113 # endif
114 # ifdef HAVE_TAGLIB_H
115 << " taglib"
116 # endif
117 # ifdef _UTF8
118 << " unicode"
119 # endif
120 << std::endl;
121 exit(0);
123 else if (!strcmp(argv[i], "-?") || !strcmp(argv[i], "--help"))
125 std::cout
126 << "Usage: ncmpcpp [OPTION]...\n"
127 << " -h, --host connect to server at host [localhost]\n"
128 << " -p, --port connect to server at port [6600]\n"
129 << " -c, --config use alternative configuration file\n"
130 << " -s, --screen <name> specify the startup screen\n"
131 << " -?, --help show this help message\n"
132 << " -v, --version display version information\n"
133 << " --now-playing display now playing song [" << now_playing_format << "]\n"
134 << "\n"
135 << " play start playing\n"
136 << " pause pause the currently playing song\n"
137 << " toggle toggle play/pause mode\n"
138 << " stop stop playing\n"
139 << " next play the next song\n"
140 << " prev play the previous song\n"
141 << " volume [+-]<num> adjusts volume by [+-]<num>\n"
143 exit(0);
146 if (!ConnectToMPD())
147 exit(1);
149 if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--screen"))
151 if (++i == argc) {
152 std::cout << "ncmpcpp: no screen specified" << std::endl;
153 exit(0);
155 if (!strcmp(argv[i], "help"))
156 Config.startup_screen = myHelp;
157 else if (!strcmp(argv[i], "playlist"))
158 Config.startup_screen = myPlaylist;
159 else if (!strcmp(argv[i], "browser"))
160 Config.startup_screen = myBrowser;
161 else if (!strcmp(argv[i], "search-engine"))
162 Config.startup_screen = mySearcher;
163 else if (!strcmp(argv[i], "media-library"))
164 Config.startup_screen = myLibrary;
165 else if (!strcmp(argv[i], "playlist-editor"))
166 Config.startup_screen = myPlaylistEditor;
167 # ifdef HAVE_TAGLIB_H
168 else if (!strcmp(argv[i], "tag-editor"))
169 Config.startup_screen = myTagEditor;
170 # endif // HAVE_TAGLIB_H
171 # ifdef ENABLE_OUTPUTS
172 else if (!strcmp(argv[i], "outputs"))
173 Config.startup_screen = myOutputs;
174 # endif // ENABLE_OUTPUTS
175 # ifdef ENABLE_VISUALIZER
176 else if (!strcmp(argv[i], "visualizer"))
177 Config.startup_screen = myVisualizer;
178 # endif // ENABLE_VISUALIZER
179 # ifdef ENABLE_CLOCK
180 else if (!strcmp(argv[i], "clock"))
181 Config.startup_screen = myClock;
182 # endif // ENABLE_CLOCK
183 else {
184 std::cout << "ncmpcpp: invalid screen: " << argv[i] << std::endl;
185 exit(0);
188 else if (!strcmp(argv[i], "--now-playing"))
190 Mpd.UpdateStatus();
191 if (!Mpd.GetErrorMessage().empty())
193 std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
194 exit(1);
196 if (Mpd.isPlaying())
198 if (argc > ++i)
200 if (MPD::Song::isFormatOk("now-playing format", argv[i]))
202 // apply additional pair of braces
203 now_playing_format = "{";
204 now_playing_format += argv[i];
205 now_playing_format += "}";
206 Replace(now_playing_format, "\\n", "\n");
207 Replace(now_playing_format, "\\t", "\t");
210 std::cout << utf_to_locale_cpy(Mpd.GetCurrentlyPlayingSong().toString(now_playing_format)) << "\n";
212 exit(0);
214 else if (!strcmp(argv[i], "play"))
216 Mpd.Play();
217 quit = 1;
219 else if (!strcmp(argv[i], "pause"))
221 Mpd.Pause(1);
222 quit = 1;
224 else if (!strcmp(argv[i], "toggle"))
226 Mpd.UpdateStatus();
227 if (!Mpd.GetErrorMessage().empty())
229 std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
230 exit(1);
232 Mpd.Toggle();
233 quit = 1;
235 else if (!strcmp(argv[i], "stop"))
237 Mpd.Stop();
238 quit = 1;
240 else if (!strcmp(argv[i], "next"))
242 Mpd.Next();
243 quit = 1;
245 else if (!strcmp(argv[i], "prev"))
247 Mpd.Prev();
248 quit = 1;
250 else if (!strcmp(argv[i], "volume"))
252 i++;
253 Mpd.UpdateStatus();
254 if (!Mpd.GetErrorMessage().empty())
256 std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
257 exit(1);
259 if (i != argc)
260 Mpd.SetVolume(Mpd.GetVolume()+atoi(argv[i]));
261 quit = 1;
263 else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config"))
265 // this is used in Configuration::CheckForCommandLineConfigFilePath, ignoring here.
266 ++i;
268 else
270 std::cout << "ncmpcpp: invalid option: " << argv[i] << std::endl;
271 exit(0);
273 if (!Mpd.GetErrorMessage().empty())
275 std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
276 exit(0);
279 if (quit)
280 exit(0);
283 int CaseInsensitiveStringComparison::operator()(const std::string &a, const std::string &b)
285 const char *i = a.c_str();
286 const char *j = b.c_str();
287 if (Config.ignore_leading_the)
289 if (hasTheWord(a))
290 i += 4;
291 if (hasTheWord(b))
292 j += 4;
294 int dist;
295 while (!(dist = tolower(*i)-tolower(*j)) && *j)
296 ++i, ++j;
297 return dist;
300 bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
302 bool result = false;
303 if (a.type == b.type)
305 switch (a.type)
307 case MPD::itDirectory:
308 result = cmp(ExtractTopName(a.name), ExtractTopName(b.name)) < 0;
309 break;
310 case MPD::itPlaylist:
311 result = cmp(a.name, b.name) < 0;
312 break;
313 case MPD::itSong:
314 switch (Config.browser_sort_mode)
316 case smName:
317 result = operator()(a.song, b.song);
318 break;
319 case smMTime:
320 result = a.song.getMTime() > b.song.getMTime();
321 break;
322 case smCustomFormat:
323 result = cmp(a.song.toString(Config.browser_sort_format), b.song.toString(Config.browser_sort_format)) < 0;
324 break;
326 break;
327 default: // there is no other option, silence compiler
328 assert(false);
331 else
332 result = a.type < b.type;
333 return result;
336 std::string Timestamp(time_t t)
338 char result[32];
339 # ifdef WIN32
340 result[strftime(result, 31, "%x %X", localtime(&t))] = 0;
341 # else
342 tm info;
343 result[strftime(result, 31, "%x %X", localtime_r(&t, &info))] = 0;
344 # endif // WIN32
345 return result;
348 void UpdateSongList(Menu<MPD::Song> *menu)
350 bool bold = 0;
351 for (size_t i = 0; i < menu->Size(); ++i)
353 for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
355 if (myPlaylist->Items->at(j).getHash() == menu->at(i).getHash())
357 bold = 1;
358 break;
361 menu->Bold(i, bold);
362 bold = 0;
364 menu->Refresh();
367 #ifdef HAVE_TAGLIB_H
368 std::string FindSharedDir(Menu<MPD::Song> *menu)
370 MPD::SongList list;
371 for (size_t i = 0; i < menu->Size(); ++i)
372 list.push_back(&(*menu)[i]);
373 return FindSharedDir(list);
376 std::string FindSharedDir(const MPD::SongList &v)
378 if (v.empty()) // this should never happen, but in case...
379 FatalError("empty SongList passed to FindSharedDir(const SongList &)!");
380 size_t i = -1;
381 std::string first = v.front()->GetDirectory();
382 for (MPD::SongList::const_iterator it = ++v.begin(); it != v.end(); ++it)
384 size_t j = 0;
385 std::string dir = (*it)->GetDirectory();
386 size_t length = std::min(first.length(), dir.length());
387 while (!first.compare(j, 1, dir, j, 1) && j < length && j < i)
388 ++j;
389 i = j;
391 return i ? first.substr(0, i) : "/";
393 #endif // HAVE_TAGLIB_H
395 std::string FindSharedDir(const std::string &one, const std::string &two)
397 if (one == two)
398 return one;
399 size_t i = 0;
400 while (!one.compare(i, 1, two, i, 1))
401 ++i;
402 i = one.rfind("/", i);
403 return i != std::string::npos ? one.substr(0, i) : "/";
406 std::string GetLineValue(std::string &line, char a, char b, bool once)
408 int pos[2] = { -1, -1 };
409 char x = a;
410 size_t i = 0;
411 while ((i = line.find(x, i)) != std::string::npos && pos[1] < 0)
413 if (i && line[i-1] == '\\')
415 i++;
416 continue;
418 if (once)
419 line[i] = 0;
420 pos[pos[0] >= 0] = i++;
421 if (x == a)
422 x = b;
424 ++pos[0];
425 std::string result = pos[0] >= 0 && pos[1] >= 0 ? line.substr(pos[0], pos[1]-pos[0]) : "";
427 // replace \a and \b with a and b respectively
428 char r1[] = "\\ ", r2[] = " ";
429 r1[1] = r2[0] = a;
430 Replace(result, r1, r2);
431 if (a != b)
433 r1[1] = r2[0] = b;
434 Replace(result, r1, r2);
437 return result;
440 std::string ExtractTopName(const std::string &s)
442 size_t slash = s.rfind("/");
443 return slash != std::string::npos ? s.substr(++slash) : s;
446 std::string PathGoDownOneLevel(const std::string &path)
448 size_t i = path.rfind('/');
449 return i == std::string::npos ? "/" : path.substr(0, i);
452 std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, size_t &pos, size_t width)
454 std::basic_string<my_char_t> s(str);
455 if (!Config.header_text_scrolling)
456 return s;
457 std::basic_string<my_char_t> result;
458 size_t len = Window::Length(s);
460 if (len > width)
462 s += U(" ** ");
463 len = 0;
464 std::basic_string<my_char_t>::const_iterator b = s.begin(), e = s.end();
465 for (std::basic_string<my_char_t>::const_iterator it = b+pos; it < e && len < width; ++it)
467 if ((len += wcwidth(*it)) > width)
468 break;
469 result += *it;
471 if (++pos >= s.length())
472 pos = 0;
473 for (; len < width; ++b)
475 if ((len += wcwidth(*b)) > width)
476 break;
477 result += *b;
480 else
481 result = s;
482 return result;
485 bool isInteger(const char *s)
487 assert(s);
488 if (*s == '\0')
489 return false;
490 for (const char *it = s; *it != '\0'; ++it)
491 if (!isdigit(*it) && (it != s || *it != '-'))
492 return false;
493 return true;