diff options
author | Marcus Fritzsch <marcus_fritzsch@mentor.com> | 2017-08-01 16:05:10 +0200 |
---|---|---|
committer | Marcus Fritzsch <marcus_fritzsch@mentor.com> | 2017-08-08 17:24:00 +0200 |
commit | 75524b385a2b3adaa5feabf547b80d412f3a1c56 (patch) | |
tree | 42becf73dc29152aac4effde11cb108f91f6f715 /src | |
parent | 6d5be0fb38f93a758127491f6c81de829554bf1d (diff) |
app: simplify config loading code, handle errors too
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/app.cpp | 26 |
1 files changed, 20 insertions, 6 deletions
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<struct layout> layout_from_json(json const &j) { return Ok(l); } +result<json> file_to_json(char const *filename) { + std::ifstream i(filename); + if (i.fail()) { + return Err<json>("Could not open config file"); + } + json j; + i >> j; + return Ok(j); +} + struct result<layouts_type> 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<layouts_type>(j.unwrap_err()); + } + json jlayouts = j.unwrap(); auto layouts = layouts_type(); layouts.reserve(jlayouts.size()); @@ -78,9 +90,11 @@ struct result<surface_id_to_layer_map> 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<surface_id_to_layer_map>(j.unwrap_err()); + } + json jids = j.unwrap(); return to_surface_id_to_layer_map(jids); } |