Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1 | /* |
| 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" |
that | d62d886 | 2015-12-20 20:40:15 +0100 | [diff] [blame] | 24 | #include <cctype> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 25 | |
| 26 | std::string Message::GetFormatString(const std::string& name) const |
| 27 | { |
Ethan Yonker | ab7955d | 2015-12-21 10:17:34 -0600 | [diff] [blame] | 28 | return resourceLookup(name); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // Look up in local replacement vars first, if not found then use outer lookup object |
| 32 | class 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 |
| 50 | Message::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 | /* |
| 75 | Resource manager lookup |
| 76 | */ |
| 77 | class ResourceLookup : public StringLookup |
| 78 | { |
| 79 | public: |
| 80 | virtual std::string operator()(const std::string& name) const |
| 81 | { |
Ethan Yonker | ab7955d | 2015-12-21 10:17:34 -0600 | [diff] [blame] | 82 | 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 Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 92 | #ifndef BUILD_TWRPTAR_MAIN |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 93 | const ResourceManager* res = PageManager::GetResources(); |
Ethan Yonker | ab7955d | 2015-12-21 10:17:34 -0600 | [diff] [blame] | 94 | if (res) { |
| 95 | if (default_value.empty()) |
| 96 | return res->FindString(resname); |
| 97 | else |
| 98 | return res->FindString(resname, default_value); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 99 | } |
| 100 | #endif |
| 101 | if (!default_value.empty()) { |
Ethan Yonker | ab7955d | 2015-12-21 10:17:34 -0600 | [diff] [blame] | 102 | return default_value; |
| 103 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 104 | return name; |
| 105 | } |
| 106 | }; |
| 107 | ResourceLookup resourceLookup; |
| 108 | |
| 109 | |
| 110 | /* |
| 111 | DataManager lookup |
| 112 | */ |
| 113 | class DataLookup : public StringLookup |
| 114 | { |
| 115 | public: |
| 116 | virtual std::string operator()(const std::string& name) const |
| 117 | { |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 118 | #ifndef BUILD_TWRPTAR_MAIN |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 119 | std::string value; |
| 120 | if (DataManager::GetValue(name, value) == 0) |
| 121 | return value; |
| 122 | else |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 123 | #endif |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 124 | return ""; |
| 125 | } |
| 126 | }; |
| 127 | DataLookup dataLookup; |
| 128 | |
| 129 | |
| 130 | // Utility functions to create messages. Short names to make usage convenient. |
| 131 | Message Msg(const char* name) |
| 132 | { |
| 133 | return Message(msg::kNormal, name, resourceLookup, dataLookup); |
| 134 | } |
| 135 | |
| 136 | Message Msg(msg::Kind kind, const char* name) |
| 137 | { |
| 138 | return Message(kind, name, resourceLookup, dataLookup); |
| 139 | } |