Missed update to private test repo version.
[dcpomatic.git] / src / lib / state.cc
1 /*
2     Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "cross.h"
23 #include "state.h"
24 #include "util.h"
25 #include <glib.h>
26
27
28 using std::string;
29 using boost::optional;
30
31
32 boost::optional<boost::filesystem::path> State::override_path;
33
34
35 /* List of config versions to look for in descending order of preference;
36  * i.e. look at the first one, and if that doesn't exist, try the second, etc.
37  */
38 static std::vector<std::string> config_versions = { "2.16" };
39
40
41 static
42 boost::filesystem::path
43 config_path_or_override (optional<string> version)
44 {
45         if (State::override_path) {
46                 auto p = *State::override_path;
47                 if (version) {
48                         p /= *version;
49                 }
50                 return p;
51         }
52
53         return config_path (version);
54 }
55
56
57 /** @param file State filename
58  *  @return Full path to read @file from.
59  */
60 boost::filesystem::path
61 State::read_path (string file)
62 {
63         using namespace boost::filesystem;
64
65         for (auto i: config_versions) {
66                 auto full = config_path_or_override(i) / file;
67                 if (exists(full)) {
68                         return full;
69                 }
70         }
71
72         return config_path_or_override({}) / file;
73 }
74
75
76 /** @param file State filename
77  *  @return Full path to write @file to.
78  */
79 boost::filesystem::path
80 State::write_path (string file)
81 {
82         boost::filesystem::path p = config_path_or_override(config_versions.front());
83         boost::system::error_code ec;
84         boost::filesystem::create_directories (p, ec);
85         p /= file;
86         return p;
87 }
88