X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fstate.cc;h=e22f9e0b98bd71d37f4e6947d1563b1b6afd01c2;hb=0f9f004c50a8bea6b87d5a1636f95f67066e5187;hp=dd48516f43fc05a384089c543a8b60a12896ea5f;hpb=a1f7bf2d9e5610075fbd898cdf52f4f8373741f2;p=dcpomatic.git diff --git a/src/lib/state.cc b/src/lib/state.cc index dd48516f4..e22f9e0b9 100644 --- a/src/lib/state.cc +++ b/src/lib/state.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2018-2020 Carl Hetherington + Copyright (C) 2018-2021 Carl Hetherington This file is part of DCP-o-matic. @@ -18,29 +18,71 @@ */ -#include "state.h" + #include "cross.h" +#include "state.h" +#include "util.h" #include + using std::string; +using boost::optional; + boost::optional State::override_path; + +/* List of config versions to look for in descending order of preference; + * i.e. look at the first one, and if that doesn't exist, try the second, etc. + */ +static std::vector config_versions = { "2.16" }; + + +static +boost::filesystem::path +config_path_or_override (optional version) +{ + if (State::override_path) { + auto p = *State::override_path; + if (version) { + p /= *version; + } + return p; + } + + return config_path (version); +} + + /** @param file State filename - * @return Full path to write @file to */ + * @return Full path to read @file from. + */ boost::filesystem::path -State::path (string file, bool create_directories) +State::read_path (string file) { - boost::filesystem::path p; - if (override_path) { - p = *override_path; - } else { - p = config_path (); + using namespace boost::filesystem; + + for (auto i: config_versions) { + auto full = config_path_or_override(i) / file; + if (exists(full)) { + return full; + } } + + return config_path_or_override({}) / file; +} + + +/** @param file State filename + * @return Full path to write @file to. + */ +boost::filesystem::path +State::write_path (string file) +{ + boost::filesystem::path p = config_path_or_override(config_versions.front()); boost::system::error_code ec; - if (create_directories) { - boost::filesystem::create_directories (p, ec); - } + boost::filesystem::create_directories (p, ec); p /= file; return p; } +