Supporters update.
[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 <dcp/filesystem.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         for (auto i: config_versions) {
64                 auto full = config_path_or_override(i) / file;
65                 if (dcp::filesystem::exists(full)) {
66                         return full;
67                 }
68         }
69
70         return config_path_or_override({}) / file;
71 }
72
73
74 /** @param file State filename
75  *  @return Full path to write @file to.
76  */
77 boost::filesystem::path
78 State::write_path (string file)
79 {
80         auto p = config_path_or_override(config_versions.front());
81         dcp::filesystem::create_directories(p);
82         p /= file;
83         return p;
84 }
85