diff --git a/src/helpers.cpp b/src/helpers.cpp index 124e6a43..81cf8ef3 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -147,13 +147,15 @@ void ParseArgv(int argc, char **argv) { if (argc > ++i) { - // apply additional pair of braces - now_playing_format = "{"; - now_playing_format += argv[i]; - now_playing_format += "}"; - Replace(now_playing_format, "\\n", "\n"); - Replace(now_playing_format, "\\t", "\t"); - MPD::Song::ValidateFormat("now-playing format", now_playing_format); + if (MPD::Song::isFormatOk("now-playing format", now_playing_format)) + { + // apply additional pair of braces + now_playing_format = "{"; + now_playing_format += argv[i]; + now_playing_format += "}"; + Replace(now_playing_format, "\\n", "\n"); + Replace(now_playing_format, "\\t", "\t"); + } } std::cout << utf_to_locale_cpy(Mpd.GetCurrentSong().toString(now_playing_format)) << "\n"; } diff --git a/src/settings.cpp b/src/settings.cpp index 439e06e4..b4b5c028 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -691,9 +691,8 @@ void NcmpcppConfig::Read() } else if (cl.find("song_list_format") != std::string::npos) { - if (!v.empty()) + if (!v.empty() && MPD::Song::isFormatOk("song_list_format", v)) { - MPD::Song::ValidateFormat("song_list_format", v); song_list_format = '{'; song_list_format += v; song_list_format += '}'; @@ -706,9 +705,8 @@ void NcmpcppConfig::Read() } else if (cl.find("song_status_format") != std::string::npos) { - if (!v.empty()) + if (!v.empty() && MPD::Song::isFormatOk("song_status_format", v)) { - MPD::Song::ValidateFormat("song_status_format", v); song_status_format = '{'; song_status_format += v; song_status_format += '}'; @@ -725,9 +723,8 @@ void NcmpcppConfig::Read() } else if (cl.find("song_library_format") != std::string::npos) { - if (!v.empty()) + if (!v.empty() && MPD::Song::isFormatOk("song_library_format", v)) { - MPD::Song::ValidateFormat("song_library_format", v); song_library_format = '{'; song_library_format += v; song_library_format += '}'; @@ -735,9 +732,8 @@ void NcmpcppConfig::Read() } else if (cl.find("tag_editor_album_format") != std::string::npos) { - if (!v.empty()) + if (!v.empty() && MPD::Song::isFormatOk("tag_editor_album_format", v)) { - MPD::Song::ValidateFormat("tag_editor_album_format", v); tag_editor_album_format = '{'; tag_editor_album_format += v; tag_editor_album_format += '}'; @@ -760,9 +756,8 @@ void NcmpcppConfig::Read() } else if (cl.find("alternative_header_first_line_format") != std::string::npos) { - if (!v.empty()) + if (!v.empty() && MPD::Song::isFormatOk("alternative_header_first_line_format", v)) { - MPD::Song::ValidateFormat("alternative_header_first_line_format", v); new_header_first_line = '{'; new_header_first_line += v; new_header_first_line += '}'; @@ -770,9 +765,8 @@ void NcmpcppConfig::Read() } else if (cl.find("alternative_header_second_line_format") != std::string::npos) { - if (!v.empty()) + if (!v.empty() && MPD::Song::isFormatOk("alternative_header_second_line_format", v)) { - MPD::Song::ValidateFormat("alternative_header_second_line_format", v); new_header_second_line = '{'; new_header_second_line += v; new_header_second_line += '}'; @@ -1096,9 +1090,8 @@ void NcmpcppConfig::Read() } else if (cl.find("song_window_title_format") != std::string::npos) { - if (!v.empty()) + if (!v.empty() && MPD::Song::isFormatOk("song_window_title_format", v)) { - MPD::Song::ValidateFormat("song_window_title_format", v); song_window_title_format = '{'; song_window_title_format += v; song_window_title_format += '}'; diff --git a/src/song.cpp b/src/song.cpp index a0e9e7b6..c90b3fca 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -492,7 +493,7 @@ std::string MPD::Song::ShowTime(int length) return ss.str(); } -void MPD::Song::ValidateFormat(const std::string &type, const std::string &s) +bool MPD::Song::isFormatOk(const std::string &type, const std::string &s) { int braces = 0; for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) @@ -503,7 +504,22 @@ void MPD::Song::ValidateFormat(const std::string &type, const std::string &s) --braces; } if (braces) - FatalError(type + ": number of opening and closing braces does not equal!"); + { + std::cerr << type << ": number of opening and closing braces does not equal!\n"; + return false; + } + + for (size_t i = s.find('%'); i != std::string::npos; i = s.find('%', i)) + { + if (isdigit(s[++i])) + while (isdigit(s[++i])) { } + if (!toGetFunction(s[i])) + { + std::cerr << type << ": invalid character at position " << IntoStr(s[i]) << ": '" << s[i] << "'\n"; + return false; + } + } + return true; } void MPD::Song::SetHashAndSlash() diff --git a/src/song.h b/src/song.h index 27f29ed7..ee305a45 100644 --- a/src/song.h +++ b/src/song.h @@ -103,7 +103,7 @@ namespace MPD Song &operator=(const Song &); static std::string ShowTime(int); - static void ValidateFormat(const std::string &type, const std::string &format); + static bool isFormatOk(const std::string &type, const std::string &format); private: void SetHashAndSlash();