Merge lp:~charlesk/indicator-datetime/lp-1312775-comments-in-etc-timezone into lp:indicator-datetime/14.04

Proposed by Charles Kerr
Status: Superseded
Proposed branch: lp:~charlesk/indicator-datetime/lp-1312775-comments-in-etc-timezone
Merge into: lp:indicator-datetime/14.04
Diff against target: 108 lines (+69/-13)
2 files modified
src/timezone-file.cpp (+56/-13)
tests/test-timezone-file.cpp (+13/-0)
To merge this branch: bzr merge lp:~charlesk/indicator-datetime/lp-1312775-comments-in-etc-timezone
Reviewer Review Type Date Requested Status
Ted Gould (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+218298@code.launchpad.net

This proposal has been superseded by a proposal from 2014-06-10.

Commit message

when src/timezone-file.c reads the contents of a file, skip lines that begin with '#'

Description of the change

when src/timezone-file.c reads the contents of a file, skip lines that begin with '#'

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) wrote :

Uhg, silly we have to do this.

review: Approve

Unmerged revisions

341. By Charles Kerr

when src/timezone-file.c reads the contents of a file, skip lines that begin with '#' because they're comments. Add test.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/timezone-file.cpp'
2--- src/timezone-file.cpp 2014-02-02 21:28:52 +0000
3+++ src/timezone-file.cpp 2014-05-05 15:07:52 +0000
4@@ -22,6 +22,59 @@
5 #include <cerrno>
6 #include <cstdlib>
7
8+namespace
9+{
10+ std::string get_timezone_from_file(const std::string& filename)
11+ {
12+ GError * error;
13+ GIOChannel * io_channel;
14+ std::string ret;
15+
16+ // read through filename line-by-line until we fine a nonempty non-comment line
17+ error = nullptr;
18+ io_channel = g_io_channel_new_file(filename.c_str(), "r", &error);
19+ if (error == nullptr)
20+ {
21+ auto line = g_string_new(nullptr);
22+
23+ while(ret.empty())
24+ {
25+ const auto io_status = g_io_channel_read_line_string(io_channel, line, nullptr, &error);
26+ if ((io_status == G_IO_STATUS_EOF) || (io_status == G_IO_STATUS_ERROR))
27+ break;
28+ if (error != nullptr)
29+ break;
30+
31+ g_strstrip(line->str);
32+
33+ if (!line->len) // skip empty lines
34+ continue;
35+
36+ if (*line->str=='#') // skip comments
37+ continue;
38+
39+ ret = line->str;
40+ }
41+
42+ g_string_free(line, true);
43+ }
44+
45+ if (io_channel != nullptr)
46+ {
47+ g_io_channel_shutdown(io_channel, false, nullptr);
48+ g_io_channel_unref(io_channel);
49+ }
50+
51+ if (error != nullptr)
52+ {
53+ g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, filename.c_str(), error->message);
54+ g_error_free(error);
55+ }
56+
57+ return ret;
58+ }
59+}
60+
61 namespace unity {
62 namespace indicator {
63 namespace datetime {
64@@ -95,20 +148,10 @@
65 void
66 FileTimezone::reload()
67 {
68- GError * err = nullptr;
69- gchar * str = nullptr;
70+ const auto new_timezone = get_timezone_from_file(m_filename);
71
72- if (!g_file_get_contents(m_filename.c_str(), &str, nullptr, &err))
73- {
74- g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, m_filename.c_str(), err->message);
75- g_error_free(err);
76- }
77- else
78- {
79- g_strstrip(str);
80- timezone.set(str);
81- g_free(str);
82- }
83+ if (!new_timezone.empty())
84+ timezone.set(new_timezone);
85 }
86
87 } // namespace datetime
88
89=== modified file 'tests/test-timezone-file.cpp'
90--- tests/test-timezone-file.cpp 2014-01-22 15:06:01 +0000
91+++ tests/test-timezone-file.cpp 2014-05-05 15:07:52 +0000
92@@ -131,3 +131,16 @@
93 ASSERT_TRUE(changed);
94 ASSERT_EQ(changed_timezone, tz.timezone.get());
95 }
96+
97+
98+/**
99+ * Test that timezone-file picks up the initial value
100+ */
101+TEST_F(TimezoneFixture, IgnoreComments)
102+{
103+ const std::string comment = "# Created by cloud-init v. 0.7.5 on Thu, 24 Apr 2014 14:03:29 +0000";
104+ const std::string expected_timezone = "Europe/Berlin";
105+ set_file(comment + "\n" + expected_timezone);
106+ FileTimezone tz(TIMEZONE_FILE);
107+ ASSERT_EQ(expected_timezone, tz.timezone.get());
108+}

Subscribers

People subscribed via source and target branches