Cleanup: assert result of get_font().
[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 #include <glib.h>
27
28
29 using std::string;
30 using boost::optional;
31
32
33 boost::optional<boost::filesystem::path> State::override_path;
34
35
36 /* List of config versions to look for in descending order of preference;
37  * i.e. look at the first one, and if that doesn't exist, try the second, etc.
38  */
39 static std::vector<std::string> config_versions = { "2.16" };
40
41
42 static
43 boost::filesystem::path
44 config_path_or_override (optional<string> version)
45 {
46         if (State::override_path) {
47                 auto p = *State::override_path;
48                 if (version) {
49                         p /= *version;
50                 }
51                 return p;
52         }
53
54         return config_path (version);
55 }
56
57
58 /** @param file State filename
59  *  @return Full path to read @file from.
60  */
61 boost::filesystem::path
62 State::read_path (string file)
63 {
64         for (auto i: config_versions) {
65                 auto full = config_path_or_override(i) / file;
66                 if (dcp::filesystem::exists(full)) {
67                         return full;
68                 }
69         }
70
71         return config_path_or_override({}) / file;
72 }
73
74
75 /** @param file State filename
76  *  @return Full path to write @file to.
77  */
78 boost::filesystem::path
79 State::write_path (string file)
80 {
81         auto p = config_path_or_override(config_versions.front());
82         boost::system::error_code ec;
83         dcp::filesystem::create_directories(p, ec);
84         p /= file;
85         return p;
86 }
87