diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2014-05-05 10:06:34 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2014-05-05 10:06:34 -0500 |
commit | 2b112ebfadb6b6d19553722eca91b797ad60f303 (patch) | |
tree | 815b73d07410af48e294b08babab2cf817415251 | |
parent | 4008e0a91ebdc1cf5c5715a122fed318124c8802 (diff) | |
download | ayatana-indicator-datetime-2b112ebfadb6b6d19553722eca91b797ad60f303.tar.gz ayatana-indicator-datetime-2b112ebfadb6b6d19553722eca91b797ad60f303.tar.bz2 ayatana-indicator-datetime-2b112ebfadb6b6d19553722eca91b797ad60f303.zip |
when src/timezone-file.c reads the contents of a file, skip lines that begin with '#' because they're comments. Add test.
-rw-r--r-- | src/timezone-file.cpp | 69 | ||||
-rw-r--r-- | tests/test-timezone-file.cpp | 13 |
2 files changed, 69 insertions, 13 deletions
diff --git a/src/timezone-file.cpp b/src/timezone-file.cpp index c99897a..bbe48f7 100644 --- a/src/timezone-file.cpp +++ b/src/timezone-file.cpp @@ -22,6 +22,59 @@ #include <cerrno> #include <cstdlib> +namespace +{ + std::string get_timezone_from_file(const std::string& filename) + { + GError * error; + GIOChannel * io_channel; + std::string ret; + + // read through filename line-by-line until we fine a nonempty non-comment line + error = nullptr; + io_channel = g_io_channel_new_file(filename.c_str(), "r", &error); + if (error == nullptr) + { + auto line = g_string_new(nullptr); + + while(ret.empty()) + { + const auto io_status = g_io_channel_read_line_string(io_channel, line, nullptr, &error); + if ((io_status == G_IO_STATUS_EOF) || (io_status == G_IO_STATUS_ERROR)) + break; + if (error != nullptr) + break; + + g_strstrip(line->str); + + if (!line->len) // skip empty lines + continue; + + if (*line->str=='#') // skip comments + continue; + + ret = line->str; + } + + g_string_free(line, true); + } + + if (io_channel != nullptr) + { + g_io_channel_shutdown(io_channel, false, nullptr); + g_io_channel_unref(io_channel); + } + + if (error != nullptr) + { + g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, filename.c_str(), error->message); + g_error_free(error); + } + + return ret; + } +} + namespace unity { namespace indicator { namespace datetime { @@ -95,20 +148,10 @@ FileTimezone::on_file_changed(gpointer gself) void FileTimezone::reload() { - GError * err = nullptr; - gchar * str = nullptr; + const auto new_timezone = get_timezone_from_file(m_filename); - if (!g_file_get_contents(m_filename.c_str(), &str, nullptr, &err)) - { - g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, m_filename.c_str(), err->message); - g_error_free(err); - } - else - { - g_strstrip(str); - timezone.set(str); - g_free(str); - } + if (!new_timezone.empty()) + timezone.set(new_timezone); } } // namespace datetime diff --git a/tests/test-timezone-file.cpp b/tests/test-timezone-file.cpp index 453b353..aec597c 100644 --- a/tests/test-timezone-file.cpp +++ b/tests/test-timezone-file.cpp @@ -131,3 +131,16 @@ TEST_F(TimezoneFixture, ChangedValue) ASSERT_TRUE(changed); ASSERT_EQ(changed_timezone, tz.timezone.get()); } + + +/** + * Test that timezone-file picks up the initial value + */ +TEST_F(TimezoneFixture, IgnoreComments) +{ + const std::string comment = "# Created by cloud-init v. 0.7.5 on Thu, 24 Apr 2014 14:03:29 +0000"; + const std::string expected_timezone = "Europe/Berlin"; + set_file(comment + "\n" + expected_timezone); + FileTimezone tz(TIMEZONE_FILE); + ASSERT_EQ(expected_timezone, tz.timezone.get()); +} |