Skip to content

Commit 07f69f1

Browse files
committed
Refactor ParseGuestConfigTxt
- Read the `android-info.txt` file once instead of for every field - Declare less local variables Bug: 472539943
1 parent 415f008 commit 07f69f1

File tree

1 file changed

+52
-69
lines changed

1 file changed

+52
-69
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/guest_config.cc

Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -137,101 +137,84 @@ Result<void> ParseGuestConfigTextProto(const std::string& guest_config_path,
137137
return {};
138138
}
139139

140-
Result<std::string> GetAndroidInfoConfig(
141-
const std::string& android_info_file_path, const std::string& key) {
142-
CF_EXPECT(FileExists(android_info_file_path));
143-
144-
std::string android_info_contents = ReadFile(android_info_file_path);
145-
auto android_info_map = CF_EXPECT(ParseKeyEqualsValue(android_info_contents));
146-
CF_EXPECT(android_info_map.find(key) != android_info_map.end());
147-
return android_info_map[key];
140+
Result<std::string> MapGetResult(
141+
const std::map<std::string, std::string> android_info,
142+
const std::string& key) {
143+
auto it = android_info.find(key);
144+
CF_EXPECT(it != android_info.end());
145+
return it->second;
146+
}
147+
148+
bool MapHasValue(const std::map<std::string, std::string> android_info,
149+
const std::string& key, const std::string& expected_value) {
150+
auto it = android_info.find(key);
151+
return it == android_info.end() ? false : it->second == expected_value;
148152
}
149153

150154
Result<void> ParseGuestConfigTxt(const std::string& guest_config_path,
151155
GuestConfig& guest_config) {
152-
auto res_device_type = GetAndroidInfoConfig(guest_config_path, "device_type");
156+
CF_EXPECT(FileExists(guest_config_path));
157+
const std::string android_info_contents = ReadFile(guest_config_path);
158+
const std::map<std::string, std::string> info =
159+
CF_EXPECT(ParseKeyEqualsValue(android_info_contents));
160+
153161
// If that "device_type" is not explicitly set, fall back to parse "config".
154-
if (!res_device_type.ok()) {
155-
res_device_type = GetAndroidInfoConfig(guest_config_path, "config");
156-
}
157-
guest_config.device_type = ParseDeviceType(res_device_type.value_or(""));
162+
guest_config.device_type =
163+
ParseDeviceType(MapGetResult(info, "device_type")
164+
.value_or(MapGetResult(info, "config").value_or("")));
158165

159-
auto res = GetAndroidInfoConfig(guest_config_path, "gfxstream");
160-
guest_config.gfxstream_supported = res.ok() && res.value() == "supported";
166+
guest_config.gfxstream_supported =
167+
MapHasValue(info, "gfxstream", "supported");
161168

162-
res = GetAndroidInfoConfig(guest_config_path,
163-
"gfxstream_gl_program_binary_link_status");
164169
guest_config.gfxstream_gl_program_binary_link_status_supported =
165-
res.ok() && res.value() == "supported";
170+
MapHasValue(info, "gfxstream_gl_program_binary_link_status", "supported");
166171

167-
auto res_mouse_support = GetAndroidInfoConfig(guest_config_path, "mouse");
168-
guest_config.mouse_supported =
169-
res_mouse_support.ok() && res_mouse_support.value() == "supported";
172+
guest_config.mouse_supported = MapHasValue(info, "mouse", "supported");
170173

171-
auto res_gamepad_support = GetAndroidInfoConfig(guest_config_path, "gamepad");
172-
guest_config.gamepad_supported =
173-
res_gamepad_support.ok() && res_gamepad_support.value() == "supported";
174+
guest_config.gamepad_supported = MapHasValue(info, "gamepad", "supported");
174175

175-
auto res_custom_keyboard_config =
176-
GetAndroidInfoConfig(guest_config_path, "custom_keyboard");
177-
if (res_custom_keyboard_config.ok()) {
178-
guest_config.custom_keyboard_config =
179-
DefaultHostArtifactsPath(res_custom_keyboard_config.value());
176+
if (const Result<std::string> res = MapGetResult(info, "custom_keyboard");
177+
res.ok()) {
178+
guest_config.custom_keyboard_config = DefaultHostArtifactsPath(*res);
180179
}
181180

182-
auto res_domkey_mapping_config =
183-
GetAndroidInfoConfig(guest_config_path, "domkey_mapping");
184-
if (res_domkey_mapping_config.ok()) {
185-
guest_config.domkey_mapping_config =
186-
DefaultHostArtifactsPath(res_domkey_mapping_config.value());
181+
if (const Result<std::string> res = MapGetResult(info, "domkey_mapping");
182+
res.ok()) {
183+
guest_config.domkey_mapping_config = DefaultHostArtifactsPath(*res);
187184
}
188185

189-
auto res_bgra_support =
190-
GetAndroidInfoConfig(guest_config_path, "supports_bgra_framebuffers");
191186
guest_config.supports_bgra_framebuffers =
192-
res_bgra_support.value_or("") == "true";
187+
MapHasValue(info, "supports_bgra_framebuffers", "true");
193188

194-
auto res_vhost_user_vsock =
195-
GetAndroidInfoConfig(guest_config_path, "vhost_user_vsock");
196-
guest_config.vhost_user_vsock = res_vhost_user_vsock.value_or("") == "true";
189+
guest_config.vhost_user_vsock = MapHasValue(info, "vhost_user_vsock", "true");
197190

198-
auto res_prefer_drm_virgl_when_supported = GetAndroidInfoConfig(
199-
guest_config_path, "prefer_drm_virgl_when_supported");
200191
guest_config.prefer_drm_virgl_when_supported =
201-
res_prefer_drm_virgl_when_supported.value_or("") == "true";
202-
203-
auto res_ti50_emulator =
204-
GetAndroidInfoConfig(guest_config_path, "ti50_emulator");
205-
guest_config.ti50_emulator = res_ti50_emulator.value_or("");
206-
auto res_output_audio_streams_count =
207-
GetAndroidInfoConfig(guest_config_path, "output_audio_streams_count");
208-
if (res_output_audio_streams_count.ok()) {
209-
std::string output_audio_streams_count_str =
210-
res_output_audio_streams_count.value();
211-
CF_EXPECT(android::base::ParseInt(output_audio_streams_count_str,
212-
&guest_config.output_audio_streams_count),
213-
"Failed to parse value \"" << output_audio_streams_count_str
214-
<< "\" for output audio stream count");
192+
MapHasValue(info, "prefer_drm_virgl_when_supported", "true");
193+
194+
guest_config.ti50_emulator = MapGetResult(info, "ti50_emulator").value_or("");
195+
196+
if (const Result<std::string> res =
197+
MapGetResult(info, "output_audio_streams_count");
198+
res.ok()) {
199+
CF_EXPECTF(
200+
android::base::ParseInt(*res, &guest_config.output_audio_streams_count),
201+
"Failed to parse value '{}' for output audio stream count", *res);
215202
}
216203

217-
Result<std::string> enforce_mac80211_hwsim =
218-
GetAndroidInfoConfig(guest_config_path, "enforce_mac80211_hwsim");
219-
if (enforce_mac80211_hwsim.ok()) {
220-
if (*enforce_mac80211_hwsim == "true") {
204+
if (const Result<std::string> res =
205+
MapGetResult(info, "enforce_mac80211_hwsim");
206+
res.ok()) {
207+
if (*res == "true") {
221208
guest_config.enforce_mac80211_hwsim = true;
222-
} else if (*enforce_mac80211_hwsim == "false") {
209+
} else if (*res == "false") {
223210
guest_config.enforce_mac80211_hwsim = false;
224211
}
225212
}
226213

227-
auto res_blank_data_image_mb =
228-
GetAndroidInfoConfig(guest_config_path, "blank_data_image_mb");
229-
if (res_blank_data_image_mb.ok()) {
230-
std::string res_blank_data_image_mb_str = res_blank_data_image_mb.value();
231-
CF_EXPECT(android::base::ParseInt(res_blank_data_image_mb_str,
232-
&guest_config.blank_data_image_mb),
233-
"Failed to parse value \"" << res_blank_data_image_mb_str
234-
<< "\" for blank data image size");
214+
if (const Result<std::string> res = MapGetResult(info, "blank_data_image_mb");
215+
res.ok()) {
216+
CF_EXPECTF(android::base::ParseInt(*res, &guest_config.blank_data_image_mb),
217+
"Failed to parse value '{}' for blank data image size", *res);
235218
}
236219

237220
return {};

0 commit comments

Comments
 (0)