diff options
author | rachytski <siarhei.rachytski@gmail.com> | 2012-06-05 21:38:15 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:39:27 +0300 |
commit | 966d2a6f6288c52e3a004ca5f7d2530368c24442 (patch) | |
tree | 8307f483bcc51b8f06d8c9f1535c3d18d92b24fa /base | |
parent | 1bd70241ca3d79de5df024d0dcd14fdca558261a (diff) |
added strings localization mechanism and used it in CountryStatusDisplay.
Diffstat (limited to 'base')
-rw-r--r-- | base/base.pro | 4 | ||||
-rw-r--r-- | base/base_tests/base_tests.pro | 5 | ||||
-rw-r--r-- | base/base_tests/string_format_test.cpp | 11 | ||||
-rw-r--r-- | base/string_format.cpp | 53 | ||||
-rw-r--r-- | base/string_format.hpp | 73 | ||||
-rw-r--r-- | base/strings_bundle.cpp | 30 | ||||
-rw-r--r-- | base/strings_bundle.hpp | 23 |
7 files changed, 197 insertions, 2 deletions
diff --git a/base/base.pro b/base/base.pro index da59a8bca6..7e8cdb6d7a 100644 --- a/base/base.pro +++ b/base/base.pro @@ -26,6 +26,8 @@ SOURCES += \ threaded_container.cpp \ resource_pool.cpp \ fence_manager.cpp \ + strings_bundle.cpp \ + string_format.cpp \ HEADERS += \ SRC_FIRST.hpp \ @@ -73,3 +75,5 @@ HEADERS += \ threaded_priority_queue.hpp \ std_serialization.hpp \ fence_manager.hpp \ + strings_bundle.hpp \ + string_format.hpp \ diff --git a/base/base_tests/base_tests.pro b/base/base_tests/base_tests.pro index 022ad76c62..021127f81c 100644 --- a/base/base_tests/base_tests.pro +++ b/base/base_tests/base_tests.pro @@ -33,9 +33,10 @@ SOURCES += \ threaded_list_test.cpp \ condition_test.cpp \ containers_test.cpp \ - fence_manager_test.cpp - + fence_manager_test.cpp \ + string_format_test.cpp \ HEADERS += + diff --git a/base/base_tests/string_format_test.cpp b/base/base_tests/string_format_test.cpp new file mode 100644 index 0000000000..0849838e65 --- /dev/null +++ b/base/base_tests/string_format_test.cpp @@ -0,0 +1,11 @@ +#include "../../testing/testing.hpp" +#include "../string_format.hpp" + +UNIT_TEST(StringFormat_Smoke) +{ + TEST_EQUAL(strings::Format("this is % % % %", "a", "very", "simple", "test"), "this is a very simple test", ()); + + TEST_EQUAL(strings::Format("this", "a"), "this", ()); + + TEST_EQUAL(strings::Format("this % %", "is"), "this is %", ()); +} diff --git a/base/string_format.cpp b/base/string_format.cpp new file mode 100644 index 0000000000..64dd6c36a5 --- /dev/null +++ b/base/string_format.cpp @@ -0,0 +1,53 @@ +#include "string_format.hpp" +#include "logging.hpp" + +namespace strings +{ + string const FormatImpl(string const & s, list<string> const & l) + { + size_t offs = 0; + list<size_t> fieldOffs; + + string temp = s; + + LOG(LINFO, (s)); + + while (true) + { + offs = temp.find("%", offs); + if (offs == string::npos) + break; + else + { + if ((offs != 0) && (temp[offs - 1] == '\\')) + { + temp = temp.erase(offs - 1, 1); + --offs; + } + else + fieldOffs.push_back(offs); + + ++offs; + } + } + + offs = 0; + + string res = temp; + + list<size_t>::const_iterator offsIt; + list<string>::const_iterator strIt; + + for (offsIt = fieldOffs.begin(), strIt = l.begin(); + (offsIt != fieldOffs.end()) && (strIt != l.end()); + ++offsIt, ++strIt) + { + res.replace(*offsIt + offs, 1, *strIt); + offs += strIt->size() - 1; + } + + LOG(LINFO, (res)); + + return res; + } +} diff --git a/base/string_format.hpp b/base/string_format.hpp new file mode 100644 index 0000000000..d97a8faeb9 --- /dev/null +++ b/base/string_format.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include "../std/string.hpp" +#include "../std/sstream.hpp" +#include "../std/list.hpp" + +namespace strings +{ + template <typename T> + string ToString(T t) + { + ostringstream out; + out << t; + return out.str(); + } + + string const FormatImpl(string const & s, list<string> const & l); + + template <typename T1> + string const Format(string const & s, T1 const & t1) + { + list<string> l; + l.push_back(ToString(t1)); + + return FormatImpl(s, l); + } + + template <typename T1, typename T2> + string const Format(string const & s, T1 const & t1, T2 const & t2) + { + list<string> l; + l.push_back(ToString(t1)); + l.push_back(ToString(t2)); + + return FormatImpl(s, l); + } + + template <typename T1, typename T2, typename T3> + string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3) + { + list<string> l; + l.push_back(ToString(t1)); + l.push_back(ToString(t2)); + l.push_back(ToString(t3)); + + return FormatImpl(s, l); + } + + template <typename T1, typename T2, typename T3, typename T4> + string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4) + { + list<string> l; + l.push_back(ToString(t1)); + l.push_back(ToString(t2)); + l.push_back(ToString(t3)); + l.push_back(ToString(t4)); + + return FormatImpl(s, l); + } + + template <typename T1, typename T2, typename T3, typename T4, typename T5> + string const Format(string const & s, T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5) + { + list<string> l; + l.push_back(ToString(t1)); + l.push_back(ToString(t2)); + l.push_back(ToString(t3)); + l.push_back(ToString(t4)); + l.push_back(ToString(t5)); + + return FormatImpl(s, l); + } +} diff --git a/base/strings_bundle.cpp b/base/strings_bundle.cpp new file mode 100644 index 0000000000..1706853658 --- /dev/null +++ b/base/strings_bundle.cpp @@ -0,0 +1,30 @@ +#include "strings_bundle.hpp" + +void StringsBundle::SetDefaultString(string const & name, string const & value) +{ + m_defValues[name] = value; +} + +void StringsBundle::SetString(string const & name, string const & value) +{ + m_values[name] = value; +} + +string const StringsBundle::GetString(string const & name) const +{ + TStringMap::const_iterator it = m_values.find(name); + if (it != m_values.end()) + return it->second; + else + { + it = m_defValues.find(name); + if (it != m_defValues.end()) + return it->second; + } + return ""; +} + +string const FormatString(char const * msg, ...) +{ + return ""; +} diff --git a/base/strings_bundle.hpp b/base/strings_bundle.hpp new file mode 100644 index 0000000000..fa864e4fef --- /dev/null +++ b/base/strings_bundle.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "../std/string.hpp" +#include "../std/map.hpp" + +class StringsBundle +{ +private: + + typedef map<string, string> TStringMap; + TStringMap m_values; + TStringMap m_defValues; + +public: + + void SetDefaultString(string const & name, string const & value); + void SetString(string const & name, string const & value); + string const GetString(string const & name) const; +}; + +string const & FormatString(string const & msg); + + |