Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/shared/Dictionary.cpp

Issue 10897028: Create a shared dictionary class for plugin and engine (Closed)
Patch Set: Created June 7, 2013, 12:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include <fstream>
2 #include <stdexcept>
3 #include <Windows.h>
4
5 #include "Dictionary.h"
6 #include "Utils.h"
7
8 Dictionary* Dictionary::instance = 0;
9
10 const std::wstring baseLocale = L"en";
11
12 Dictionary::Dictionary(const std::wstring& locale)
13 {
14 std::wstring basePath = GetDllDir() + L"locales\\";
15
16 // Always load base locale first - that's our fallback
17 ReadDictionary(basePath, baseLocale);
18
19 // Now try to load by full locale code
20 if (locale != baseLocale && !ReadDictionary(basePath, locale))
Felix Dahlke 2013/06/11 12:31:14 It looks like if this or the other ReadDictionary
Wladimir Palant 2013/06/12 13:46:07 Yes, it is intended - even incomplete localization
Felix Dahlke 2013/06/13 18:07:37 Okay, just wasn't sure if it was intended.
21 {
22 // Fall back to short locale name
23 size_t pos = locale.find(L'-');
24 if (pos != std::wstring::npos && locale.compare(0, pos, baseLocale) != 0)
25 ReadDictionary(basePath, locale.substr(0, pos));
26 }
27 }
28
29 void Dictionary::Create(const std::wstring& locale)
30 {
31 if (!instance)
32 instance = new Dictionary(locale);
33 }
34
35 Dictionary* Dictionary::GetInstance()
36 {
37 if (!instance)
38 throw std::runtime_error("Attempt to access dictionary before creating");
39
40 return instance;
41 }
42
43 bool Dictionary::ReadDictionary(const std::wstring& basePath, const std::wstring & locale)
44 {
45 std::ifstream stream(basePath + locale + L".ini");
Oleksandr 2013/06/18 07:41:09 I think it wouldn't hurt to use "filebuf::sh_read
Wladimir Palant 2013/06/18 08:17:48 Good point even though this is non-standard, will
Wladimir Palant 2013/11/05 11:30:25 After looking into this I am less convinced that t
Eric 2013/11/05 13:56:06 The full constructor is declared thus: explic
Wladimir Palant 2013/11/06 08:51:46 Not really. If you look at http://msdn.microsoft.c
Eric 2013/11/06 17:49:53 After realizing that I hadn't remembered what was
Felix Dahlke 2013/11/06 23:09:50 This seems a bit like premature optimisation to me
Wladimir Palant 2013/11/07 06:53:57 According to http://msdn.microsoft.com/en-us/libra
46 if (stream.fail())
47 return false;
48
49 std::string section;
50 while (!stream.eof())
Felix Dahlke 2013/06/11 12:31:14 I think the following would suffice, it'd be a bit
Wladimir Palant 2013/06/12 13:46:07 IMHO that will get us into an endless loop - std::
Felix Dahlke 2013/06/13 18:07:37 No, it wouldn't be an endless loop, this is the ca
51 {
52 std::string line;
53 std::getline(stream, line);
54 if (stream.fail())
55 return false;
56
57 line = ::TrimString(line);
58 if (line.size() >= 2 && line[0] == '[' && line[line.size() - 1] == ']')
59 {
60 // Section header
61 section = line.substr(1, line.size() - 2);
62 }
63 else if (line.size() >= 1 && line[0] == '#')
Felix Dahlke 2013/06/11 12:31:14 This won't allow for comments preceeded by whitesp
Wladimir Palant 2013/06/12 13:46:07 We call TrimLine earlier so whitespace is allowed.
Felix Dahlke 2013/06/13 18:07:37 Hm. One benefit of this is that the translated str
64 {
65 // Comment
66 continue;
67 }
68 else
69 {
70 // Value
71 size_t pos = line.find('=');
72 if (pos != std::string::npos)
73 {
74 std::string key = ::TrimString(line.substr(0, pos));
75 std::string value = ::TrimString(line.substr(pos + 1));
76 data[KeyType(section, key)] = ToUtf16String(value);
77 }
78 }
79 }
80 return true;
81 }
82
83 std::wstring Dictionary::Lookup(const std::string& section, const std::string& k ey) const
84 {
85 DataType::const_iterator it = data.find(KeyType(section, key));
86 if (it == data.end())
87 return L"### MISSING STRING [" + ToUtf16String(section) + L", " + ToUtf16Str ing(key) + L"] ###";
88
89 return it->second;
90 }
OLDNEW
« src/plugin/PluginClass.cpp ('K') | « src/shared/Dictionary.h ('k') | src/shared/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld