From: Marcus Fritzsch Date: Tue, 1 Aug 2017 14:05:10 +0000 (+0200) Subject: app: simplify config loading code, handle errors too X-Git-Tag: 4.99.1~199 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=staging%2Fwindowmanager.git;a=commitdiff_plain;h=75524b385a2b3adaa5feabf547b80d412f3a1c56 app: simplify config loading code, handle errors too Signed-off-by: Marcus Fritzsch --- diff --git a/src/app.cpp b/src/app.cpp index 8c929fe..353557e 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -59,12 +59,24 @@ result layout_from_json(json const &j) { return Ok(l); } +result file_to_json(char const *filename) { + std::ifstream i(filename); + if (i.fail()) { + return Err("Could not open config file"); + } + json j; + i >> j; + return Ok(j); +} + struct result load_layout(char const *filename) { DB("loading layout from " << filename); - json jlayouts; - std::ifstream i(filename); - i >> jlayouts; + auto j = file_to_json(filename); + if (j.is_err()) { + return Err(j.unwrap_err()); + } + json jlayouts = j.unwrap(); auto layouts = layouts_type(); layouts.reserve(jlayouts.size()); @@ -78,9 +90,11 @@ struct result load_layer_ids(char const *filename) { DB("loading IDs from " << filename); - json jids; - std::ifstream i(filename); - i >> jids; + auto j = file_to_json(filename); + if (j.is_err()) { + return Err(j.unwrap_err()); + } + json jids = j.unwrap(); return to_surface_id_to_layer_map(jids); }