From 25deec013c1b0586c6806d2b6544c75f84d8bae1 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Fri, 14 Aug 2009 04:23:06 +0200 Subject: [PATCH] validate correctness of song formats at start --- src/settings.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/settings.cpp b/src/settings.cpp index da88e69d..513f513b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -24,6 +24,7 @@ # include #endif // WIN32 #include +#include #include "global.h" #include "helpers.h" @@ -61,6 +62,20 @@ namespace key[1] = !two.empty() && two[0] == '\'' ? two[1] : (atoi(two.c_str()) == 0 ? null_key : atoi(two.c_str())); } + void ValidateSongFormat(const std::string &type, const std::string &s) + { + int braces = 0; + for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) + { + if (*it == '{') + ++braces; + else if (*it == '}') + --braces; + } + if (braces) + throw std::runtime_error(type + ": number of opening and closing braces does not equal!"); + } + Border IntoBorder(const std::string &color) { return Border(IntoColor(color)); @@ -522,7 +537,12 @@ void ReadConfiguration(ncmpcpp_config &conf) else if (cl.find("song_list_format") != std::string::npos) { if (!v.empty()) - conf.song_list_format = v; + { + ValidateSongFormat("song_list_format", v); + conf.song_list_format = '{'; + conf.song_list_format += v; + conf.song_list_format += '}'; + } } else if (cl.find("song_columns_list_format") != std::string::npos) { @@ -532,17 +552,32 @@ void ReadConfiguration(ncmpcpp_config &conf) else if (cl.find("song_status_format") != std::string::npos) { if (!v.empty()) - conf.song_status_format = v; + { + ValidateSongFormat("song_status_format", v); + conf.song_status_format = '{'; + conf.song_status_format += v; + conf.song_status_format += '}'; + } } else if (cl.find("song_library_format") != std::string::npos) { if (!v.empty()) - conf.song_library_format = v; + { + ValidateSongFormat("song_library_format", v); + conf.song_library_format = '{'; + conf.song_library_format += v; + conf.song_library_format += '}'; + } } else if (cl.find("tag_editor_album_format") != std::string::npos) { if (!v.empty()) - conf.tag_editor_album_format = v; + { + ValidateSongFormat("tag_editor_album_format", v); + conf.tag_editor_album_format = '{'; + conf.tag_editor_album_format += v; + conf.tag_editor_album_format += '}'; + } } else if (cl.find("external_editor") != std::string::npos) { @@ -562,12 +597,22 @@ void ReadConfiguration(ncmpcpp_config &conf) else if (cl.find("alternative_header_first_line_format") != std::string::npos) { if (!v.empty()) - conf.new_header_first_line = v; + { + ValidateSongFormat("alternative_header_first_line_format", v); + conf.new_header_first_line = '{'; + conf.new_header_first_line += v; + conf.new_header_first_line += '}'; + } } else if (cl.find("alternative_header_second_line_format") != std::string::npos) { if (!v.empty()) - conf.new_header_second_line = v; + { + ValidateSongFormat("alternative_header_second_line_format", v); + conf.new_header_second_line = '{'; + conf.new_header_second_line += v; + conf.new_header_second_line += '}'; + } } else if (cl.find("browser_playlist_prefix") != std::string::npos) { @@ -744,7 +789,12 @@ void ReadConfiguration(ncmpcpp_config &conf) else if (cl.find("song_window_title_format") != std::string::npos) { if (!v.empty()) - conf.song_window_title_format = v; + { + ValidateSongFormat("song_window_title_format", v); + conf.song_window_title_format = '{'; + conf.song_window_title_format += v; + conf.song_window_title_format += '}'; + } } else if (cl.find("empty_tag") != std::string::npos) {