From 4433958db07ddf81cf2bc44cd54ee5ff646a1322 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Sun, 13 Mar 2016 23:48:33 +0300 Subject: [PATCH] Settings code review/style fixes. --- platform/settings.cpp | 49 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/platform/settings.cpp b/platform/settings.cpp index 58a0155820..500f249b9d 100644 --- a/platform/settings.cpp +++ b/platform/settings.cpp @@ -17,22 +17,18 @@ #include "std/iostream.hpp" #include "std/sstream.hpp" -#define FIRST_LAUNCH_KEY "FirstLaunchOnDate" - namespace { -char const DELIM_CHAR = '='; -} - -char const * settings::kLocationStateMode = "LastLocationStateMode"; -char const * settings::kMeasurementUnits = "Units"; +constexpr char kDelimChar = '='; +} // namespace namespace settings { +char const * kLocationStateMode = "LastLocationStateMode"; +char const * kMeasurementUnits = "Units"; + StringStorage::StringStorage() { - lock_guard guard(m_mutex); - try { string settingsPath = GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME); @@ -41,13 +37,12 @@ StringStorage::StringStorage() istream stream(&buffer); string line; - while (stream.good()) + while (getline(stream, line)) { - std::getline(stream, line); if (line.empty()) continue; - size_t const delimPos = line.find(DELIM_CHAR); + size_t const delimPos = line.find(kDelimChar); if (delimPos == string::npos) continue; @@ -71,9 +66,9 @@ void StringStorage::Save() const for (auto const & value : m_values) { string line(value.first); - line += DELIM_CHAR; + line += kDelimChar; line += value.second; - line += "\n"; + line += '\n'; file.Write(line.data(), line.size()); } } @@ -151,9 +146,8 @@ bool FromStringArray(string const & s, T(&arr)[N]) { istringstream in(s); size_t count = 0; - while (in.good() && count < N) + while (count < N && in >> arr[count]) { - in >> arr[count]; if (!std::isfinite(arr[count])) return false; ++count; @@ -161,7 +155,7 @@ bool FromStringArray(string const & s, T(&arr)[N]) return (!in.fail() && count == N); } -} +} // namespace impl template <> string ToString(m2::AnyRectD const & rect) @@ -217,6 +211,7 @@ string ToString(bool const & v) { return v ? "true" : "false"; } + template <> bool FromString(string const & str, bool & v) { @@ -244,7 +239,7 @@ template bool FromStringScalar(string const & str, T & v) { istringstream stream(str); - if (stream.good()) + if (stream) { stream >> v; return !stream.fail(); @@ -252,7 +247,7 @@ bool FromStringScalar(string const & str, T & v) else return false; } -} +} // namespace impl template <> string ToString(double const & v) @@ -324,14 +319,15 @@ string ToStringPair(TPair const & value) stream << value.first << " " << value.second; return stream.str(); } + template bool FromStringPair(string const & str, TPair & value) { istringstream stream(str); - if (stream.good()) + if (stream) { stream >> value.first; - if (stream.good()) + if (stream) { stream >> value.second; return !stream.fail(); @@ -339,7 +335,7 @@ bool FromStringPair(string const & str, TPair & value) } return false; } -} +} // namespace impl typedef pair IPairT; typedef pair DPairT; @@ -349,6 +345,7 @@ string ToString(IPairT const & v) { return impl::ToStringPair(v); } + template <> bool FromString(string const & s, IPairT & v) { @@ -360,6 +357,7 @@ string ToString(DPairT const & v) { return impl::ToStringPair(v); } + template <> bool FromString(string const & s, DPairT & v) { @@ -424,13 +422,14 @@ bool FromString(string const & s, location::EMyPositi bool IsFirstLaunchForDate(int date) { + constexpr char const * kFirstLaunchKey = "FirstLaunchOnDate"; int savedDate; - if (!Get(FIRST_LAUNCH_KEY, savedDate) || savedDate < date) + if (!Get(kFirstLaunchKey, savedDate) || savedDate < date) { - Set(FIRST_LAUNCH_KEY, date); + Set(kFirstLaunchKey, date); return true; } else return false; } -} +} // namespace settings