blob: 57f192234055cecb0b4fb02197f42bea6638b722 [file] [log] [blame]
Ethan Yonker74db1572015-10-28 12:44:49 -05001/*
2 Copyright 2015 _that/TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "../data.hpp"
20#include "pages.hpp"
21#include "resources.hpp"
22
23#include "twmsg.h"
thatd62d8862015-12-20 20:40:15 +010024#include <cctype>
Ethan Yonker74db1572015-10-28 12:44:49 -050025
26std::string Message::GetFormatString(const std::string& name) const
27{
Ethan Yonkerab7955d2015-12-21 10:17:34 -060028 return resourceLookup(name);
Ethan Yonker74db1572015-10-28 12:44:49 -050029}
30
31// Look up in local replacement vars first, if not found then use outer lookup object
32class LocalLookup : public StringLookup
33{
34 const std::vector<std::string>& vars;
35 const StringLookup& next;
36 public:
37 LocalLookup(const std::vector<std::string>& vars, const StringLookup& next) : vars(vars), next(next) {}
38 virtual std::string operator()(const std::string& name) const
39 {
40 if (!name.empty() && isdigit(name[0])) { // {1}..{9}
41 int i = atoi(name.c_str());
42 if (i > 0 && i <= (int)vars.size())
43 return vars[i - 1];
44 }
45 return next(name);
46 }
47};
48
49// conversion to final string
50Message::operator std::string() const
51{
52 // do resource lookup
53 std::string str = GetFormatString(name);
54
55 LocalLookup lookup(variables, varLookup);
56
57 // now insert stuff into curly braces
58
59 size_t pos = 0;
60 while ((pos = str.find('{', pos)) < std::string::npos) {
61 size_t end = str.find('}', pos);
62 if (end == std::string::npos)
63 break;
64
65 std::string varname = str.substr(pos + 1, end - pos - 1);
66 std::string vartext = lookup(varname);
67
68 str.replace(pos, end - pos + 1, vartext);
69 }
70 // TODO: complain about too many or too few numbered replacement variables
71 return str;
72}
73
74/*
75Resource manager lookup
76*/
77class ResourceLookup : public StringLookup
78{
79public:
80 virtual std::string operator()(const std::string& name) const
81 {
Ethan Yonkerab7955d2015-12-21 10:17:34 -060082 std::string resname;
83 std::string default_value;
84
85 size_t pos = name.find('=');
86 if (pos == std::string::npos) {
87 resname = name;
88 } else {
89 resname = name.substr(0, pos);
90 default_value = name.substr(pos + 1);
91 }
Ethan Yonker472f5062016-02-25 13:47:30 -060092#ifndef BUILD_TWRPTAR_MAIN
Ethan Yonker74db1572015-10-28 12:44:49 -050093 const ResourceManager* res = PageManager::GetResources();
Ethan Yonkerab7955d2015-12-21 10:17:34 -060094 if (res) {
95 if (default_value.empty())
96 return res->FindString(resname);
97 else
98 return res->FindString(resname, default_value);
Ethan Yonker472f5062016-02-25 13:47:30 -060099 }
100#endif
101 if (!default_value.empty()) {
Ethan Yonkerab7955d2015-12-21 10:17:34 -0600102 return default_value;
103 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500104 return name;
105 }
106};
107ResourceLookup resourceLookup;
108
109
110/*
111DataManager lookup
112*/
113class DataLookup : public StringLookup
114{
115public:
116 virtual std::string operator()(const std::string& name) const
117 {
Ethan Yonker472f5062016-02-25 13:47:30 -0600118#ifndef BUILD_TWRPTAR_MAIN
Ethan Yonker74db1572015-10-28 12:44:49 -0500119 std::string value;
120 if (DataManager::GetValue(name, value) == 0)
121 return value;
122 else
Ethan Yonker472f5062016-02-25 13:47:30 -0600123#endif
Ethan Yonker74db1572015-10-28 12:44:49 -0500124 return "";
125 }
126};
127DataLookup dataLookup;
128
129
130// Utility functions to create messages. Short names to make usage convenient.
131Message Msg(const char* name)
132{
133 return Message(msg::kNormal, name, resourceLookup, dataLookup);
134}
135
136Message Msg(msg::Kind kind, const char* name)
137{
138 return Message(kind, name, resourceLookup, dataLookup);
139}