replace a few string algorithms with boost utilities

This commit is contained in:
Andrzej Rybczak
2012-10-04 22:33:17 +02:00
parent 5908250c18
commit 8db773cfec
28 changed files with 138 additions and 198 deletions

View File

@@ -88,6 +88,14 @@ dnl =============================
AS_IF([test -z "${BOOST_LIB_SUFFIX+x}"], [BOOST_LIB_SUFFIX=-mt]) AS_IF([test -z "${BOOST_LIB_SUFFIX+x}"], [BOOST_LIB_SUFFIX=-mt])
AC_ARG_VAR([BOOST_LIB_SUFFIX], [Boost library name suffix [default=-mt]]) AC_ARG_VAR([BOOST_LIB_SUFFIX], [Boost library name suffix [default=-mt]])
dnl ======================================
dnl = checking for various boost headers =
dnl ======================================
AC_CHECK_HEADERS([boost/lexical_cast.hpp], ,
AC_MSG_ERROR(boost/lexical_cast.hpp is missing))
AC_CHECK_HEADERS([boost/algorithm/string.hpp], ,
AC_MSG_ERROR(boost/algorithm/string.hpp is missing))
dnl ============================= dnl =============================
dnl = checking for boost.locale = dnl = checking for boost.locale =
dnl ============================= dnl =============================

View File

@@ -58,7 +58,6 @@ noinst_HEADERS = \
utility/comparators.h \ utility/comparators.h \
utility/html.h \ utility/html.h \
utility/string.h \ utility/string.h \
utility/numeric_conversions.h \
utility/type_conversions.h \ utility/type_conversions.h \
utility/wide_string.h \ utility/wide_string.h \
bindings.h \ bindings.h \

View File

@@ -21,6 +21,8 @@
#include <cassert> #include <cassert>
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <boost/locale/conversion.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@@ -1192,7 +1194,7 @@ void SetCrossfade::run()
Statusbar::put() << "Set crossfade to: "; Statusbar::put() << "Set crossfade to: ";
std::string crossfade = wFooter->getString(3); std::string crossfade = wFooter->getString(3);
Statusbar::unlock(); Statusbar::unlock();
int cf = stringToInt(crossfade); int cf = boost::lexical_cast<int>(crossfade);
if (cf > 0) if (cf > 0)
{ {
Config.crossfade_time = cf; Config.crossfade_time = cf;
@@ -1503,11 +1505,11 @@ void ToggleScreenLock::run()
{ {
Statusbar::lock(); Statusbar::lock();
Statusbar::put() << "% of the locked screen's width to be reserved (20-80): "; Statusbar::put() << "% of the locked screen's width to be reserved (20-80): ";
std::string str_part = wFooter->getString(intTo<std::string>::apply(Config.locked_screen_width_part*100)); std::string str_part = wFooter->getString(boost::lexical_cast<std::string>(Config.locked_screen_width_part*100));
Statusbar::unlock(); Statusbar::unlock();
if (str_part.empty()) if (str_part.empty())
return; return;
part = stringToInt(str_part); part = boost::lexical_cast<int>(str_part);
} }
if (part < 20 || part > 80) if (part < 20 || part > 80)
{ {
@@ -1562,7 +1564,8 @@ void JumpToPositionInSong::run()
int newpos = 0; int newpos = 0;
if (position.find(':') != std::string::npos) // probably time in mm:ss if (position.find(':') != std::string::npos) // probably time in mm:ss
{ {
newpos = stringToInt(position)*60 + stringToInt(position.substr(position.find(':')+1)); newpos = boost::lexical_cast<int>(position)*60
+ boost::lexical_cast<int>(position.substr(position.find(':')+1));
if (newpos >= 0 && newpos <= Mpd.GetTotalTime()) if (newpos >= 0 && newpos <= Mpd.GetTotalTime())
Mpd.Seek(newpos); Mpd.Seek(newpos);
else else
@@ -1570,7 +1573,7 @@ void JumpToPositionInSong::run()
} }
else if (position.find('s') != std::string::npos) // probably position in seconds else if (position.find('s') != std::string::npos) // probably position in seconds
{ {
newpos = stringToInt(position); newpos = boost::lexical_cast<int>(position);
if (newpos >= 0 && newpos <= Mpd.GetTotalTime()) if (newpos >= 0 && newpos <= Mpd.GetTotalTime())
Mpd.Seek(newpos); Mpd.Seek(newpos);
else else
@@ -1578,7 +1581,7 @@ void JumpToPositionInSong::run()
} }
else else
{ {
newpos = stringToInt(position); newpos = boost::lexical_cast<int>(position);
if (newpos >= 0 && newpos <= 100) if (newpos >= 0 && newpos <= 100)
Mpd.Seek(Mpd.GetTotalTime()*newpos/100.0); Mpd.Seek(Mpd.GetTotalTime()*newpos/100.0);
else else
@@ -1934,14 +1937,14 @@ void AddRandomItems::run()
if (answer != 's') if (answer != 's')
{ {
tag_type = charToTagType(answer); tag_type = charToTagType(answer);
tag_type_str = lowercase(tagTypeToString(tag_type)); tag_type_str = boost::locale::to_lower(tagTypeToString(tag_type));
} }
else else
tag_type_str = "song"; tag_type_str = "song";
Statusbar::lock(); Statusbar::lock();
Statusbar::put() << "Number of random " << tag_type_str << "s: "; Statusbar::put() << "Number of random " << tag_type_str << "s: ";
size_t number = stringToLongInt(wFooter->getString()); size_t number = boost::lexical_cast<size_t>(wFooter->getString());
Statusbar::unlock(); Statusbar::unlock();
if (number && (answer == 's' ? Mpd.AddRandomSongs(number) : Mpd.AddRandomTag(tag_type, number))) if (number && (answer == 's' ? Mpd.AddRandomSongs(number) : Mpd.AddRandomTag(tag_type, number)))
Statusbar::msg("%zu random %s%s added to playlist", number, tag_type_str.c_str(), number == 1 ? "" : "s"); Statusbar::msg("%zu random %s%s added to playlist", number, tag_type_str.c_str(), number == 1 ? "" : "s");
@@ -2005,7 +2008,7 @@ void ToggleLibraryTagType::run()
std::string item_type = tagTypeToString(Config.media_lib_primary_tag); std::string item_type = tagTypeToString(Config.media_lib_primary_tag);
myLibrary->Tags.setTitle(Config.titles_visibility ? item_type + "s" : ""); myLibrary->Tags.setTitle(Config.titles_visibility ? item_type + "s" : "");
myLibrary->Tags.reset(); myLibrary->Tags.reset();
item_type = lowercase(item_type); item_type = boost::locale::to_lower(item_type);
std::string and_mtime = Config.media_library_sort_by_mtime ? std::string and_mtime = Config.media_library_sort_by_mtime ?
" and mtime" : " and mtime" :
""; "";
@@ -2088,7 +2091,7 @@ void SetSelectedItemsPriority::run()
Statusbar::unlock(); Statusbar::unlock();
if (!isInteger(strprio.c_str(), true)) if (!isInteger(strprio.c_str(), true))
return; return;
int prio = stringToInt(strprio); int prio = boost::lexical_cast<int>(strprio);
if (prio < 0 || prio > 255) if (prio < 0 || prio > 255)
{ {
Statusbar::msg("Number is out of range"); Statusbar::msg("Number is out of range");
@@ -2112,7 +2115,7 @@ void FilterPlaylistOnPriorities::run()
Statusbar::unlock(); Statusbar::unlock();
if (!isInteger(strprio.c_str(), false)) if (!isInteger(strprio.c_str(), false))
return; return;
unsigned prio = stringToInt(strprio); unsigned prio = boost::lexical_cast<unsigned>(strprio);
myPlaylist->main().filter(myPlaylist->main().begin(), myPlaylist->main().end(), myPlaylist->main().filter(myPlaylist->main().begin(), myPlaylist->main().end(),
[prio](const NC::Menu<MPD::Song>::Item &s) { [prio](const NC::Menu<MPD::Song>::Item &s) {
return s.value().getPrio() > prio; return s.value().getPrio() > prio;

View File

@@ -18,6 +18,7 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <boost/algorithm/string/trim.hpp>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "global.h" #include "global.h"
@@ -312,7 +313,7 @@ bool BindingsConfiguration::read(const std::string &file)
} }
else if (isspace(line[0])) // name of action to be bound else if (isspace(line[0])) // name of action to be bound
{ {
trim(line); boost::trim(line);
auto action = parseActionLine(line, error); auto action = parseActionLine(line, error);
if (action) if (action)
actions.push_back(action); actions.push_back(action);

View File

@@ -22,6 +22,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <boost/locale/conversion.hpp>
#include <algorithm> #include <algorithm>
#include "browser.h" #include "browser.h"
@@ -32,14 +33,15 @@
#include "helpers.h" #include "helpers.h"
#include "playlist.h" #include "playlist.h"
#include "regex_filter.h" #include "regex_filter.h"
#include "screen_switcher.h"
#include "settings.h" #include "settings.h"
#include "status.h" #include "status.h"
#include "statusbar.h" #include "statusbar.h"
#include "tag_editor.h" #include "tag_editor.h"
#include "utility/comparators.h"
#include "title.h" #include "title.h"
#include "tags.h" #include "tags.h"
#include "screen_switcher.h" #include "utility/comparators.h"
#include "utility/string.h"
using namespace std::placeholders; using namespace std::placeholders;
@@ -587,7 +589,7 @@ bool hasSupportedExtension(const std::string &file)
if (last_dot > file.length()) if (last_dot > file.length())
return false; return false;
std::string ext = lowercase(file.substr(last_dot+1)); std::string ext = boost::locale::to_lower(file.substr(last_dot+1));
return SupportedExtensions.find(ext) != SupportedExtensions.end(); return SupportedExtensions.find(ext) != SupportedExtensions.end();
} }

View File

@@ -20,6 +20,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <boost/algorithm/string/replace.hpp>
#include <iostream> #include <iostream>
#include "actions.h" #include "actions.h"
@@ -181,12 +182,13 @@ void ParseArgv(int argc, char **argv)
now_playing_format = "{"; now_playing_format = "{";
now_playing_format += argv[i]; now_playing_format += argv[i];
now_playing_format += "}"; now_playing_format += "}";
replace(now_playing_format, "\\n", "\n"); boost::replace_all(now_playing_format, "\\n", "\n");
replace(now_playing_format, "\\t", "\t"); boost::replace_all(now_playing_format, "\\t", "\t");
} }
} }
std::cout << Charset::utf8ToLocale( std::string np = Mpd.GetCurrentlyPlayingSong().toString(
Mpd.GetCurrentlyPlayingSong().toString(now_playing_format, Config.tags_separator)) << "\n"; now_playing_format, Config.tags_separator);
std::cout << Charset::utf8ToLocale(np) << "\n";
} }
exit(0); exit(0);
} }

View File

@@ -28,6 +28,7 @@
#include "playlist.h" #include "playlist.h"
#include "global.h" #include "global.h"
#include "tag_editor.h" #include "tag_editor.h"
#include "utility/string.h"
#include "utility/type_conversions.h" #include "utility/type_conversions.h"
using Global::myScreen; using Global::myScreen;

View File

@@ -75,7 +75,7 @@ std::string keyToString(const Key &key, bool *print_backspace)
else if (key >= Key(KEY_F1, Key::NCurses) && key <= Key(KEY_F12, Key::NCurses)) else if (key >= Key(KEY_F1, Key::NCurses) && key <= Key(KEY_F12, Key::NCurses))
{ {
result += "F"; result += "F";
result += intTo<std::string>::apply(key.getChar()-264); result += boost::lexical_cast<std::string>(key.getChar()-264);
} }
else if ((key == Key(KEY_BACKSPACE, Key::NCurses) || key == Key(KEY_BACKSPACE_2, Key::Standard))) else if ((key == Key(KEY_BACKSPACE, Key::NCurses) || key == Key(KEY_BACKSPACE_2, Key::Standard)))
{ {

View File

@@ -26,6 +26,7 @@
#include "screen.h" #include "screen.h"
#include "settings.h" #include "settings.h"
#include "status.h" #include "status.h"
#include "utility/string.h"
#include "utility/wide_string.h" #include "utility/wide_string.h"
inline HasColumns *hasColumns(BaseScreen *screen) inline HasColumns *hasColumns(BaseScreen *screen)

View File

@@ -31,6 +31,7 @@
#include <cassert> #include <cassert>
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <boost/locale/conversion.hpp>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@@ -41,6 +42,7 @@
#include "statusbar.h" #include "statusbar.h"
#include "title.h" #include "title.h"
#include "screen_switcher.h" #include "screen_switcher.h"
#include "utility/string.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;
@@ -112,7 +114,7 @@ void Lastfm::Load()
w.reset(); w.reset();
std::string artist = itsArgs.find("artist")->second; std::string artist = itsArgs.find("artist")->second;
std::string file = lowercase(artist + ".txt"); std::string file = boost::locale::to_lower(artist + ".txt");
removeInvalidCharsFromFilename(file); removeInvalidCharsFromFilename(file);
itsFilename = itsFolder + "/" + file; itsFilename = itsFolder + "/" + file;

View File

@@ -22,9 +22,11 @@
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
#include <boost/algorithm/string/trim.hpp>
#include "curl_handle.h" #include "curl_handle.h"
#include "settings.h" #include "settings.h"
#include "utility/html.h" #include "utility/html.h"
#include "utility/string.h"
const char *LastfmService::baseURL = "http://ws.audioscrobbler.com/2.0/?api_key=d94e5b6e26469a2d1ffae8ef20131b79&method="; const char *LastfmService::baseURL = "http://ws.audioscrobbler.com/2.0/?api_key=d94e5b6e26469a2d1ffae8ef20131b79&method=";
@@ -92,7 +94,7 @@ bool LastfmService::actionFailed(const std::string &data)
void LastfmService::postProcess(std::string &data) void LastfmService::postProcess(std::string &data)
{ {
stripHtmlTags(data); stripHtmlTags(data);
trim(data); boost::trim(data);
} }
/***********************************************************************/ /***********************************************************************/

View File

@@ -36,6 +36,7 @@
#include "statusbar.h" #include "statusbar.h"
#include "title.h" #include "title.h"
#include "screen_switcher.h" #include "screen_switcher.h"
#include "utility/string.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;

View File

@@ -24,6 +24,8 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/trim.hpp>
#include "charset.h" #include "charset.h"
#include "lyrics_fetcher.h" #include "lyrics_fetcher.h"
@@ -55,8 +57,8 @@ LyricsFetcher::Result LyricsFetcher::fetch(const std::string &artist, const std:
result.first = false; result.first = false;
std::string url = getURL(); std::string url = getURL();
replace(url, "%artist%", artist.c_str()); boost::replace_all(url, "%artist%", artist.c_str());
replace(url, "%title%", title.c_str()); boost::replace_all(url, "%title%", title.c_str());
std::string data; std::string data;
CURLcode code = Curl::perform(data, url); CURLcode code = Curl::perform(data, url);
@@ -101,7 +103,7 @@ bool LyricsFetcher::getContent(const char *open_tag, const char *close_tag, std:
void LyricsFetcher::postProcess(std::string &data) void LyricsFetcher::postProcess(std::string &data)
{ {
stripHtmlTags(data); stripHtmlTags(data);
trim(data); boost::trim(data);
} }
/***********************************************************************/ /***********************************************************************/
@@ -136,9 +138,9 @@ LyricsFetcher::Result LyricwikiFetcher::fetch(const std::string &artist, const s
return result; return result;
} }
replace(data, "<br />", "\n"); boost::replace_all(data, "<br />", "\n");
stripHtmlTags(data); stripHtmlTags(data);
trim(data); boost::trim(data);
result.second = data; result.second = data;
result.first = true; result.first = true;
@@ -227,8 +229,8 @@ void MetrolyricsFetcher::postProcess(std::string &data)
// some of lyrics have both \n chars and <br />, html tags // some of lyrics have both \n chars and <br />, html tags
// are always present whereas \n chars are not, so we need to // are always present whereas \n chars are not, so we need to
// throw them away to avoid having line breaks doubled. // throw them away to avoid having line breaks doubled.
replace(data, "&#10;", ""); boost::replace_all(data, "&#10;", "");
replace(data, "<br />", "\n"); boost::replace_all(data, "<br />", "\n");
data = unescapeHtmlUtf8(data); data = unescapeHtmlUtf8(data);
LyricsFetcher::postProcess(data); LyricsFetcher::postProcess(data);
} }

View File

@@ -18,6 +18,7 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <boost/locale/conversion.hpp>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cassert> #include <cassert>
@@ -809,7 +810,8 @@ void MediaLibrary::toggleColumnsMode()
nextColumn(); nextColumn();
if (Config.titles_visibility) if (Config.titles_visibility)
{ {
std::string item_type = lowercase(tagTypeToString(Config.media_lib_primary_tag)); std::string item_type = boost::locale::to_lower(
tagTypeToString(Config.media_lib_primary_tag));
std::string and_mtime = Config.media_library_sort_by_mtime ? " and mtime" : ""; std::string and_mtime = Config.media_library_sort_by_mtime ? " and mtime" : "";
Albums.setTitle("Albums (sorted by " + item_type + and_mtime + ")"); Albums.setTitle("Albums (sorted by " + item_type + and_mtime + ")");
} }
@@ -846,7 +848,8 @@ void MediaLibrary::toggleSortMode()
Songs.clear(); Songs.clear();
if (Config.titles_visibility) if (Config.titles_visibility)
{ {
std::string item_type = lowercase(tagTypeToString(Config.media_lib_primary_tag)); std::string item_type = boost::locale::to_lower(
tagTypeToString(Config.media_lib_primary_tag));
std::string and_mtime = Config.media_library_sort_by_mtime ? " and mtime" : ""; std::string and_mtime = Config.media_library_sort_by_mtime ? " and mtime" : "";
Albums.setTitle("Albums (sorted by " + item_type + and_mtime + ")"); Albums.setTitle("Albums (sorted by " + item_type + and_mtime + ")");
} }
@@ -887,7 +890,8 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
} }
if (primary_tag.empty()) if (primary_tag.empty())
{ {
std::string item_type = lowercase(tagTypeToString(Config.media_lib_primary_tag)); std::string item_type = boost::locale::to_lower(
tagTypeToString(Config.media_lib_primary_tag));
Statusbar::msg("Can't use this function because the song has no %s tag set", item_type.c_str()); Statusbar::msg("Can't use this function because the song has no %s tag set", item_type.c_str());
return; return;
} }
@@ -975,7 +979,8 @@ void MediaLibrary::AddToPlaylist(bool add_n_play)
if ((!Tags.empty() && isActiveWindow(Tags)) if ((!Tags.empty() && isActiveWindow(Tags))
|| (isActiveWindow(Albums) && Albums.current().value().isAllTracksEntry())) || (isActiveWindow(Albums) && Albums.current().value().isAllTracksEntry()))
{ {
std::string tag_type = lowercase(tagTypeToString(Config.media_lib_primary_tag)); std::string tag_type = boost::locale::to_lower(
tagTypeToString(Config.media_lib_primary_tag));
Statusbar::msg("Songs with %s = \"%s\" added", tag_type.c_str(), Tags.current().value().tag().c_str()); Statusbar::msg("Songs with %s = \"%s\" added", tag_type.c_str(), Tags.current().value().tag().c_str());
} }
else if (isActiveWindow(Albums)) else if (isActiveWindow(Albums))

View File

@@ -18,8 +18,8 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <boost/algorithm/string/split.hpp>
#include "mutable_song.h" #include "mutable_song.h"
#include "utility/string.h"
namespace MPD {// namespace MPD {//
@@ -168,7 +168,8 @@ void MutableSong::setDuration(unsigned int duration)
void MutableSong::setTags(SetFunction set, const std::string &value, const std::string &delimiter) void MutableSong::setTags(SetFunction set, const std::string &value, const std::string &delimiter)
{ {
auto tags = split(value, delimiter); std::vector<std::string> tags;
boost::iter_split(tags, value, boost::first_finder(delimiter));
size_t i = 0; size_t i = 0;
for (; i < tags.size(); ++i) for (; i < tags.size(); ++i)
(this->*set)(tags[i], i); (this->*set)(tags[i], i);

View File

@@ -37,6 +37,7 @@
#include "charset.h" #include "charset.h"
#include "cmdargs.h" #include "cmdargs.h"
#include "global.h" #include "global.h"
#include "error.h"
#include "helpers.h" #include "helpers.h"
#include "lyrics.h" #include "lyrics.h"
#include "playlist.h" #include "playlist.h"
@@ -46,6 +47,15 @@
#include "visualizer.h" #include "visualizer.h"
#include "title.h" #include "title.h"
namespace boost {//
void throw_exception(const std::exception &e)
{
FatalError(std::string("Exception thrown: ") + e.what());
}
}
namespace namespace
{ {
std::ofstream errorlog; std::ofstream errorlog;

View File

@@ -171,7 +171,7 @@ void PlaylistEditor::update()
{ {
title = "Playlist content"; title = "Playlist content";
title += " ("; title += " (";
title += unsignedLongIntTo<std::string>::apply(Content.size()); title += boost::lexical_cast<std::string>(Content.size());
title += " item"; title += " item";
if (Content.size() == 1) if (Content.size() == 1)
title += ")"; title += ")";

View File

@@ -26,6 +26,7 @@
# include <sys/stat.h> # include <sys/stat.h>
#endif // WIN32 #endif // WIN32
#include <algorithm> #include <algorithm>
#include <boost/algorithm/string/trim.hpp>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <string> #include <string>
@@ -101,7 +102,7 @@ namespace
if (equal == std::string::npos) if (equal == std::string::npos)
return ""; return "";
std::string result = s.substr(0, equal); std::string result = s.substr(0, equal);
trim(result); boost::trim(result);
return result; return result;
} }
@@ -346,33 +347,33 @@ void Configuration::Read()
} }
else if (name == "mpd_port") else if (name == "mpd_port")
{ {
if (stringToInt(v)) if (boost::lexical_cast<int>(v))
mpd_port = stringToInt(v); mpd_port = boost::lexical_cast<int>(v);
} }
else if (name == "mpd_connection_timeout") else if (name == "mpd_connection_timeout")
{ {
if (stringToInt(v)) if (boost::lexical_cast<int>(v))
mpd_connection_timeout = stringToInt(v); mpd_connection_timeout = boost::lexical_cast<int>(v);
} }
else if (name == "mpd_crossfade_time") else if (name == "mpd_crossfade_time")
{ {
if (stringToInt(v) > 0) if (boost::lexical_cast<int>(v) > 0)
crossfade_time = stringToInt(v); crossfade_time = boost::lexical_cast<int>(v);
} }
else if (name == "seek_time") else if (name == "seek_time")
{ {
if (stringToInt(v) > 0) if (boost::lexical_cast<int>(v) > 0)
seek_time = stringToInt(v); seek_time = boost::lexical_cast<int>(v);
} }
else if (name == "playlist_disable_highlight_delay") else if (name == "playlist_disable_highlight_delay")
{ {
if (stringToInt(v) >= 0) if (boost::lexical_cast<int>(v) >= 0)
playlist_disable_highlight_delay = stringToInt(v); playlist_disable_highlight_delay = boost::lexical_cast<int>(v);
} }
else if (name == "message_delay_time") else if (name == "message_delay_time")
{ {
if (stringToInt(v) > 0) if (boost::lexical_cast<int>(v) > 0)
message_delay_time = stringToInt(v); message_delay_time = boost::lexical_cast<int>(v);
} }
else if (name == "song_list_format") else if (name == "song_list_format")
{ {
@@ -775,20 +776,20 @@ void Configuration::Read()
else if (name == "lines_scrolled") else if (name == "lines_scrolled")
{ {
if (!v.empty()) if (!v.empty())
lines_scrolled = stringToInt(v); lines_scrolled = boost::lexical_cast<int>(v);
} }
else if (name == "search_engine_default_search_mode") else if (name == "search_engine_default_search_mode")
{ {
if (!v.empty()) if (!v.empty())
{ {
unsigned mode = stringToInt(v); unsigned mode = boost::lexical_cast<unsigned>(v);
if (--mode < 3) if (--mode < 3)
search_engine_default_search_mode = mode; search_engine_default_search_mode = mode;
} }
} }
else if (name == "visualizer_sync_interval") else if (name == "visualizer_sync_interval")
{ {
unsigned interval = stringToInt(v); unsigned interval = boost::lexical_cast<unsigned>(v);
if (interval) if (interval)
visualizer_sync_interval = interval; visualizer_sync_interval = interval;
} }
@@ -803,7 +804,7 @@ void Configuration::Read()
} }
else if (name == "locked_screen_width_part") else if (name == "locked_screen_width_part")
{ {
int part = stringToInt(v); int part = boost::lexical_cast<int>(v);
if (part) if (part)
locked_screen_width_part = part/100.0; locked_screen_width_part = part/100.0;
} }
@@ -937,7 +938,13 @@ void Configuration::GenerateColumns()
col.color = stringToColor(getEnclosedString(song_list_columns_format, '[', ']', &pos)); col.color = stringToColor(getEnclosedString(song_list_columns_format, '[', ']', &pos));
std::string tag_type = getEnclosedString(song_list_columns_format, '{', '}', &pos); std::string tag_type = getEnclosedString(song_list_columns_format, '{', '}', &pos);
col.fixed = *width.rbegin() == 'f'; if (*width.rbegin() == 'f')
{
col.fixed = true;
width.resize(width.size()-1);
}
else
col.fixed = false;
// alternative name // alternative name
size_t tag_type_colon_pos = tag_type.find(':'); size_t tag_type_colon_pos = tag_type.find(':');
@@ -973,7 +980,7 @@ void Configuration::GenerateColumns()
else // empty column else // empty column
col.display_empty_tag = 0; col.display_empty_tag = 0;
col.width = stringToInt(width); col.width = boost::lexical_cast<int>(width);
columns.push_back(col); columns.push_back(col);
} }

View File

@@ -20,11 +20,12 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include "song.h" #include "song.h"
#include "utility/numeric_conversions.h"
#include "utility/type_conversions.h" #include "utility/type_conversions.h"
#include "utility/wide_string.h" #include "utility/wide_string.h"
#include "window.h" #include "window.h"
@@ -193,7 +194,7 @@ std::string Song::getPriority(unsigned idx) const
assert(m_song); assert(m_song);
if (idx > 0) if (idx > 0)
return ""; return "";
return unsignedIntTo<std::string>::apply(getPrio()); return boost::lexical_cast<std::string>(getPrio());
} }
std::string MPD::Song::getTags(GetFunction f, const std::string &tags_separator) const std::string MPD::Song::getTags(GetFunction f, const std::string &tags_separator) const
@@ -281,9 +282,9 @@ std::string Song::ShowTime(unsigned length)
std::string result; std::string result;
if (hours > 0) if (hours > 0)
result = print<32, std::string>::apply("%d:%02d:%02d", hours, minutes, seconds); result = (boost::format("%d:%02d:%02d") % hours % minutes % seconds).str();
else else
result = print<32, std::string>::apply("%d:%02d", minutes, seconds); result = (boost::format("%d:%02d") % minutes % seconds).str();
return result; return result;
} }
@@ -309,7 +310,7 @@ bool MPD::Song::isFormatOk(const std::string &type, const std::string &fmt)
while (isdigit(fmt[++i])) { } while (isdigit(fmt[++i])) { }
if (!charToGetFunction(fmt[i])) if (!charToGetFunction(fmt[i]))
{ {
std::cerr << type << ": invalid character at position " << unsignedLongIntTo<std::string>::apply(i+1) << ": '" << fmt[i] << "'\n"; std::cerr << type << ": invalid character at position " << boost::lexical_cast<std::string>(i+1) << ": '" << fmt[i] << "'\n";
return false; return false;
} }
} }

View File

@@ -37,6 +37,7 @@
#include "tag_editor.h" #include "tag_editor.h"
#include "visualizer.h" #include "visualizer.h"
#include "title.h" #include "title.h"
#include "utility/string.h"
using Global::myScreen; using Global::myScreen;
@@ -325,7 +326,7 @@ void Status::Changes::elapsedTime()
if (Config.display_bitrate && Mpd.GetBitrate()) if (Config.display_bitrate && Mpd.GetBitrate())
{ {
tracklength += " "; tracklength += " ";
tracklength += intTo<std::string>::apply(Mpd.GetBitrate()); tracklength += boost::lexical_cast<std::string>(Mpd.GetBitrate());
tracklength += " kbps"; tracklength += " kbps";
} }
@@ -359,7 +360,7 @@ void Status::Changes::elapsedTime()
if (Config.display_bitrate && Mpd.GetBitrate()) if (Config.display_bitrate && Mpd.GetBitrate())
{ {
tracklength += " ["; tracklength += " [";
tracklength += intTo<std::string>::apply(Mpd.GetBitrate()); tracklength += boost::lexical_cast<std::string>(Mpd.GetBitrate());
tracklength += " kbps]"; tracklength += " kbps]";
} }
tracklength += " ["; tracklength += " [";
@@ -498,7 +499,7 @@ void Status::Changes::mixer()
VolumeState += "n/a"; VolumeState += "n/a";
else else
{ {
VolumeState += intTo<std::string>::apply(volume); VolumeState += boost::lexical_cast<std::string>(volume);
VolumeState += "%"; VolumeState += "%";
} }
*wHeader << Config.volume_color; *wHeader << Config.volume_color;

View File

@@ -21,8 +21,8 @@
#ifndef NCMPCPP_STRBUFFER_H #ifndef NCMPCPP_STRBUFFER_H
#define NCMPCPP_STRBUFFER_H #define NCMPCPP_STRBUFFER_H
#include <boost/lexical_cast.hpp>
#include <set> #include <set>
#include "utility/numeric_conversions.h"
#include "window.h" #include "window.h"
namespace NC {// namespace NC {//
@@ -130,25 +130,25 @@ public:
BasicBuffer<CharT> &operator<<(int n) BasicBuffer<CharT> &operator<<(int n)
{ {
m_string += intTo<StringType>::apply(n); m_string += boost::lexical_cast<StringType>(n);
return *this; return *this;
} }
BasicBuffer<CharT> &operator<<(long int n) BasicBuffer<CharT> &operator<<(long int n)
{ {
m_string += longIntTo<StringType>::apply(n); m_string += boost::lexical_cast<StringType>(n);
return *this; return *this;
} }
BasicBuffer<CharT> &operator<<(unsigned int n) BasicBuffer<CharT> &operator<<(unsigned int n)
{ {
m_string += unsignedIntTo<StringType>::apply(n); m_string += boost::lexical_cast<StringType>(n);
return *this; return *this;
} }
BasicBuffer<CharT> &operator<<(unsigned long int n) BasicBuffer<CharT> &operator<<(unsigned long int n)
{ {
m_string += unsignedLongIntTo<StringType>::apply(n); m_string += boost::lexical_cast<StringType>(n);
return *this; return *this;
} }

View File

@@ -22,6 +22,7 @@
#ifdef HAVE_TAGLIB_H #ifdef HAVE_TAGLIB_H
#include <boost/locale/conversion.hpp>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@@ -475,9 +476,9 @@ void TagEditor::enterPressed()
for (unsigned i = 1; i <= EditedSongs.size(); ++i, ++it) for (unsigned i = 1; i <= EditedSongs.size(); ++i, ++it)
{ {
if (Config.tag_editor_extended_numeration) if (Config.tag_editor_extended_numeration)
(*it)->setTrack(unsignedIntTo<std::string>::apply(i) + "/" + unsignedIntTo<std::string>::apply(EditedSongs.size())); (*it)->setTrack(boost::lexical_cast<std::string>(i) + "/" + boost::lexical_cast<std::string>(EditedSongs.size()));
else else
(*it)->setTrack(unsignedIntTo<std::string>::apply(i)); (*it)->setTrack(boost::lexical_cast<std::string>(i));
} }
Statusbar::msg("Tracks numbered"); Statusbar::msg("Tracks numbered");
} }
@@ -1034,7 +1035,7 @@ void LowerAllLetters(MPD::MutableSong &s)
{ {
unsigned i = 0; unsigned i = 0;
for (std::string tag; !(tag = (s.*m->Get)(i)).empty(); ++i) for (std::string tag; !(tag = (s.*m->Get)(i)).empty(); ++i)
(s.*m->Set)(ToString(lowercase(ToWString(tag))), i); (s.*m->Set)(boost::locale::to_lower(tag), i);
} }
} }
@@ -1046,7 +1047,7 @@ void GetPatternList()
if (input.is_open()) if (input.is_open())
{ {
std::string line; std::string line;
while (getline(input, line)) while (std::getline(input, line))
if (!line.empty()) if (!line.empty())
Patterns.push_back(line); Patterns.push_back(line);
input.close(); input.close();

View File

@@ -35,7 +35,6 @@
#include "global.h" #include "global.h"
#include "settings.h" #include "settings.h"
#include "utility/numeric_conversions.h"
#include "utility/wide_string.h" #include "utility/wide_string.h"
namespace {// namespace {//
@@ -54,8 +53,8 @@ void readCommonTags(MPD::MutableSong &s, TagLib::Tag *tag)
s.setTitle(tag->title().to8Bit(true)); s.setTitle(tag->title().to8Bit(true));
s.setArtist(tag->artist().to8Bit(true)); s.setArtist(tag->artist().to8Bit(true));
s.setAlbum(tag->album().to8Bit(true)); s.setAlbum(tag->album().to8Bit(true));
s.setDate(intTo<std::string>::apply(tag->year())); s.setDate(boost::lexical_cast<std::string>(tag->year()));
s.setTrack(intTo<std::string>::apply(tag->track())); s.setTrack(boost::lexical_cast<std::string>(tag->track()));
s.setGenre(tag->genre().to8Bit(true)); s.setGenre(tag->genre().to8Bit(true));
s.setComment(tag->comment().to8Bit(true)); s.setComment(tag->comment().to8Bit(true));
} }
@@ -132,8 +131,8 @@ void writeCommonTags(const MPD::MutableSong &s, TagLib::Tag *tag)
tag->setTitle(ToWString(s.getTitle())); tag->setTitle(ToWString(s.getTitle()));
tag->setArtist(ToWString(s.getArtist())); tag->setArtist(ToWString(s.getArtist()));
tag->setAlbum(ToWString(s.getAlbum())); tag->setAlbum(ToWString(s.getAlbum()));
tag->setYear(stringToInt(s.getDate())); tag->setYear(boost::lexical_cast<TagLib::uint>(s.getDate()));
tag->setTrack(stringToInt(s.getTrack())); tag->setTrack(boost::lexical_cast<TagLib::uint>(s.getTrack()));
tag->setGenre(ToWString(s.getGenre())); tag->setGenre(ToWString(s.getGenre()));
tag->setComment(ToWString(s.getComment())); tag->setComment(ToWString(s.getComment()));
} }

View File

@@ -22,6 +22,8 @@
#ifdef HAVE_TAGLIB_H #ifdef HAVE_TAGLIB_H
#include <boost/locale/conversion.hpp>
// taglib includes // taglib includes
#include <fileref.h> #include <fileref.h>
#include <tag.h> #include <tag.h>
@@ -39,6 +41,7 @@
#include "title.h" #include "title.h"
#include "tags.h" #include "tags.h"
#include "screen_switcher.h" #include "screen_switcher.h"
#include "utility/string.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;
@@ -181,7 +184,7 @@ bool TinyTagEditor::getTags()
return false; return false;
std::string ext = itsEdited.getURI(); std::string ext = itsEdited.getURI();
ext = lowercase(ext.substr(ext.rfind(".")+1)); ext = boost::locale::to_lower(ext.substr(ext.rfind(".")+1));
w.clear(); w.clear();
w.reset(); w.reset();

View File

@@ -20,6 +20,7 @@
#include <locale> #include <locale>
#include "comparators.h" #include "comparators.h"
#include "utility/string.h"
namespace {// namespace {//

View File

@@ -18,8 +18,9 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <boost/algorithm/string/replace.hpp>
#include "utility/html.h" #include "utility/html.h"
#include "utility/string.h" //#include "utility/string.h"
std::string unescapeHtmlUtf8(const std::string &data) std::string unescapeHtmlUtf8(const std::string &data)
{ {
@@ -58,10 +59,10 @@ void stripHtmlTags(std::string &s)
size_t j = s.find(">", i)+1; size_t j = s.find(">", i)+1;
s.replace(i, j-i, ""); s.replace(i, j-i, "");
} }
replace(s, "&#039;", "'"); boost::replace_all(s, "&#039;", "'");
replace(s, "&amp;", "&"); boost::replace_all(s, "&amp;", "&");
replace(s, "&quot;", "\""); boost::replace_all(s, "&quot;", "\"");
replace(s, "&nbsp;", " "); boost::replace_all(s, "&nbsp;", " ");
for (size_t i = 0; i < s.length(); ++i) for (size_t i = 0; i < s.length(); ++i)
{ {
if (erase) if (erase)

View File

@@ -1,75 +0,0 @@
/***************************************************************************
* Copyright (C) 2008-2012 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* 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., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <string>
#include "string.h"
#ifndef NCMPCPP_UTILITY_NUMERIC_CONVERSIONS_H
#define NCMPCPP_UTILITY_NUMERIC_CONVERSIONS_H
template <typename R> struct intTo { };
template <> struct intTo<std::string> {
static std::string apply(int n) {
return print<32, std::string>::apply("%d", n);
}
};
template <> struct intTo<std::wstring> {
static std::wstring apply(int n) {
return print<32, std::wstring>::apply(L"%d", n);
}
};
template <typename R> struct longIntTo { };
template <> struct longIntTo<std::string> {
static std::string apply(long int n) {
return print<32, std::string>::apply("%ld", n);
}
};
template <> struct longIntTo<std::wstring> {
static std::wstring apply(long int n) {
return print<32, std::wstring>::apply(L"%ld", n);
}
};
template <typename R> struct unsignedIntTo { };
template <> struct unsignedIntTo<std::string> {
static std::string apply(unsigned int n) {
return print<32, std::string>::apply("%u", n);
}
};
template <> struct unsignedIntTo<std::wstring> {
static std::wstring apply(unsigned int n) {
return print<32, std::wstring>::apply(L"%u", n);
}
};
template <typename R> struct unsignedLongIntTo { };
template <> struct unsignedLongIntTo<std::string> {
static std::string apply(unsigned long int n) {
return print<32, std::string>::apply("%lu", n);
}
};
template <> struct unsignedLongIntTo<std::wstring> {
static std::wstring apply(unsigned long int n) {
return print<32, std::wstring>::apply(L"%lu", n);
}
};
#endif // NCMPCPP_UTILITY_NUMERIC_CONVERSIONS_H

View File

@@ -31,47 +31,8 @@ template <size_t N> size_t const_strlen(const char (&)[N]) {
return N-1; return N-1;
} }
template <size_t N, typename T> struct print { };
template <size_t N> struct print<N, std::string> {
static std::string apply(const char *format, ...) GNUC_PRINTF(1, 2) {
char buf[N];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf)/sizeof(char), format, args);
va_end(args);
return buf;
}
};
template <size_t N> struct print<N, std::wstring> {
static std::wstring apply(const wchar_t *format, ...) {
wchar_t buf[N];
va_list args;
va_start(args, format);
vswprintf(buf, sizeof(buf)/sizeof(wchar_t), format, args);
va_end(args);
return buf;
}
};
template <typename CharT>
std::basic_string<CharT> lowercase(std::basic_string<CharT> s)
{
std::locale loc;
const std::ctype<CharT> &ct = std::use_facet< std::ctype<CharT> >(loc);
for (auto it = s.begin(); it != s.end(); ++it)
*it = ct.tolower(*it);
return s;
}
int stringToInt(const std::string &s);
long stringToLongInt(const std::string &s);
bool isInteger(const char *s, bool accept_signed); bool isInteger(const char *s, bool accept_signed);
std::vector<std::string> split(const std::string &s, const std::string &delimiter);
void replace(std::string &s, const std::string &from, const std::string &to);
void trim(std::string &s);
std::string getBasename(const std::string &path); std::string getBasename(const std::string &path);
std::string getParentDirectory(const std::string &path); std::string getParentDirectory(const std::string &path);
std::string getSharedDirectory(const std::string &dir1, const std::string &dir2); std::string getSharedDirectory(const std::string &dir1, const std::string &dir2);