From 5fe3b598e6da0fbef6aa1933d6b811c31ae608a6 Mon Sep 17 00:00:00 2001 From: Thomas Kent Date: Fri, 3 Jun 2016 15:10:47 -0500 Subject: [PATCH 1/7] Support for semicolon comment delimiter INI style configuration files should support the semicolon ';' character, when it is the first character on the line only, as a comment delimiter. Because the '#' style comments are not allowed in strict MS INI files, this gives an opening for files to be compatible in both and still have comments. --- doc/overview.xml | 5 ++++- src/config_file.cpp | 3 +++ test/parsers_test.cpp | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/overview.xml b/doc/overview.xml index f7de2968cc..dae3c808e6 100644 --- a/doc/overview.xml +++ b/doc/overview.xml @@ -500,6 +500,9 @@ notify(vm); The # character introduces a comment that spans until the end of the line. + The ; character at the beginning of + a line makes the entire line a comment. + The option names are relative to the section names, so @@ -649,4 +652,4 @@ gui.accessibility.visual_bell=yes sgml-parent-document: ("program_options.xml" "section") sgml-set-face: t End: ---> \ No newline at end of file +--> diff --git a/src/config_file.cpp b/src/config_file.cpp index f2a57b4b82..89a1ea8184 100644 --- a/src/config_file.cpp +++ b/src/config_file.cpp @@ -90,6 +90,9 @@ namespace boost { namespace program_options { namespace detail { // strip '#' comments and whitespace if ((n = s.find('#')) != string::npos) s = s.substr(0, n); + // if the first character is a ';' line is a comment + if (!s.empty() && ';' == s.at(0)) + s = ""; s = trim_ws(s); if (!s.empty()) { diff --git a/test/parsers_test.cpp b/test/parsers_test.cpp index cc1bdd3f2e..2c1dbcc847 100644 --- a/test/parsers_test.cpp +++ b/test/parsers_test.cpp @@ -211,6 +211,7 @@ void test_config_file(const char* config_file) const char content1[] = " gv1 = 0#asd\n" + "; semi comment\n" "empty_value = \n" "plug3 = 7\n" "b = true\n" From 39a881969e3bd04bba6e2d5b5a83f41defabed42 Mon Sep 17 00:00:00 2001 From: Thomas Kent Date: Fri, 26 Mar 2021 05:48:54 -0500 Subject: [PATCH 2/7] Using `s.begin()` to match nearby code. --- src/config_file.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config_file.cpp b/src/config_file.cpp index 89a1ea8184..3d7a3ffb10 100644 --- a/src/config_file.cpp +++ b/src/config_file.cpp @@ -91,7 +91,7 @@ namespace boost { namespace program_options { namespace detail { if ((n = s.find('#')) != string::npos) s = s.substr(0, n); // if the first character is a ';' line is a comment - if (!s.empty() && ';' == s.at(0)) + if (!s.empty() && ';' == s.begin())) s = ""; s = trim_ws(s); From 3307429ef777cbb71956928c4b45e40814fa4ec7 Mon Sep 17 00:00:00 2001 From: Thomas Kent Date: Fri, 26 Mar 2021 05:52:19 -0500 Subject: [PATCH 3/7] Only trim if it isn't a full-line comment. --- src/config_file.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/config_file.cpp b/src/config_file.cpp index 3d7a3ffb10..ec3e49f9ed 100644 --- a/src/config_file.cpp +++ b/src/config_file.cpp @@ -91,9 +91,12 @@ namespace boost { namespace program_options { namespace detail { if ((n = s.find('#')) != string::npos) s = s.substr(0, n); // if the first character is a ';' line is a comment - if (!s.empty() && ';' == s.begin())) + if (!s.empty() && ';' == s.begin())) { s = ""; - s = trim_ws(s); + } + else { + s = trim_ws(s); + } if (!s.empty()) { // Handle section name From f9f3515d63f150f039317cf05d5daf2b6b2b7f07 Mon Sep 17 00:00:00 2001 From: Thomas Kent Date: Fri, 26 Mar 2021 06:09:44 -0500 Subject: [PATCH 4/7] Missed de-reference --- src/config_file.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config_file.cpp b/src/config_file.cpp index ec3e49f9ed..bdc2279736 100644 --- a/src/config_file.cpp +++ b/src/config_file.cpp @@ -91,7 +91,7 @@ namespace boost { namespace program_options { namespace detail { if ((n = s.find('#')) != string::npos) s = s.substr(0, n); // if the first character is a ';' line is a comment - if (!s.empty() && ';' == s.begin())) { + if (!s.empty() && ';' == *s.begin())) { s = ""; } else { From c65503376dcb83028c4276e914d75f4925c9ca62 Mon Sep 17 00:00:00 2001 From: Thomas Kent Date: Fri, 26 Mar 2021 06:17:47 -0500 Subject: [PATCH 5/7] Bad syntax --- src/config_file.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config_file.cpp b/src/config_file.cpp index bdc2279736..1373462fbe 100644 --- a/src/config_file.cpp +++ b/src/config_file.cpp @@ -91,7 +91,7 @@ namespace boost { namespace program_options { namespace detail { if ((n = s.find('#')) != string::npos) s = s.substr(0, n); // if the first character is a ';' line is a comment - if (!s.empty() && ';' == *s.begin())) { + if (!s.empty() && ';' == *s.begin()) { s = ""; } else { From 4e4227b6a778731008bba1bc25fadcd3e688c80b Mon Sep 17 00:00:00 2001 From: Thomas Kent Date: Tue, 30 Mar 2021 20:55:10 -0500 Subject: [PATCH 6/7] Commented out assignment doesn't increase the count of values. --- test/config_test.cfg | 2 ++ test/parsers_test.cpp | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test/config_test.cfg b/test/config_test.cfg index 1530f7c8ab..a84e860c22 100644 --- a/test/config_test.cfg +++ b/test/config_test.cfg @@ -1,4 +1,6 @@ gv1 = 0#asd +; semi test +; semi_value = 9 empty_value = plug3 = 7 b = true diff --git a/test/parsers_test.cpp b/test/parsers_test.cpp index faf5ab27e8..9a7056da3f 100644 --- a/test/parsers_test.cpp +++ b/test/parsers_test.cpp @@ -257,6 +257,7 @@ void test_config_file(const char* config_file) desc.add_options() ("gv1", new untyped_value) ("gv2", new untyped_value) + ("semi_value", new untyped_value) ("empty_value", new untyped_value) ("plug*", new untyped_value) ("m1.v1", new untyped_value) @@ -268,6 +269,7 @@ void test_config_file(const char* config_file) const char content1[] = " gv1 = 0#asd\n" "; semi comment\n" + "; semi_value = 9\n" "empty_value = \n" "plug3 = 7\n" "b = true\n" @@ -383,7 +385,4 @@ int main(int, char* av[]) test_command_line(); test_config_file(av[1]); test_environment(); - test_unregistered(); - return 0; -} - + test_unregister \ No newline at end of file From a6d8665a4b55c028e25bded7cc52100464aedf2b Mon Sep 17 00:00:00 2001 From: Thomas Kent Date: Tue, 30 Mar 2021 22:06:23 -0500 Subject: [PATCH 7/7] Broken file --- test/parsers_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/parsers_test.cpp b/test/parsers_test.cpp index 9a7056da3f..6b883cca46 100644 --- a/test/parsers_test.cpp +++ b/test/parsers_test.cpp @@ -385,4 +385,5 @@ int main(int, char* av[]) test_command_line(); test_config_file(av[1]); test_environment(); - test_unregister \ No newline at end of file + test_unregistered(); +}