2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG 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 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG 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.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
26 #include "../version.h"
31 #include "os/filesystem.h"
33 #include <boost/property_tree/ptree.hpp>
34 #include <boost/property_tree/xml_parser.hpp>
35 #include <boost/filesystem.hpp>
36 #include <boost/filesystem/fstream.hpp>
41 #include <boost/algorithm/string/replace.hpp>
43 namespace caspar { namespace env {
48 std::wstring ftemplate;
51 std::wstring thumbnail;
52 boost::property_tree::wptree pt;
54 void check_is_configured()
57 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
60 std::wstring clean_path(std::wstring path)
62 boost::replace_all(path, L"\\\\", L"/");
63 boost::replace_all(path, L"\\", L"/");
68 std::wstring ensure_trailing_slash(std::wstring folder)
70 if (folder.at(folder.length() - 1) != L'/')
76 std::wstring resolve_or_create(const std::wstring& folder)
78 auto found_path = find_case_insensitive(folder);
84 boost::system::error_code ec;
85 boost::filesystem::create_directories(folder, ec);
88 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Failed to create directory " + u8(folder) + " (" + ec.message() + ")"));
94 void ensure_writable(const std::wstring& folder)
96 static const std::wstring CREATE_FILE_TEST = L"casparcg_test_writable.empty";
98 boost::system::error_code ec;
99 boost::filesystem::path test_file(folder + L"/" + CREATE_FILE_TEST);
100 boost::filesystem::ofstream out(folder + L"/" + CREATE_FILE_TEST);
104 boost::filesystem::remove(test_file, ec);
105 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Directory " + folder + L" is not writable."));
109 boost::filesystem::remove(test_file, ec);
112 void configure(const std::wstring& filename)
116 initial = clean_path(boost::filesystem::initial_path().wstring());
118 boost::filesystem::wifstream file(initial + L"/" + filename);
119 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
121 auto paths = pt.get_child(L"configuration.paths");
122 media = clean_path(paths.get(L"media-path", initial + L"/media/"));
123 log = clean_path(paths.get(L"log-path", initial + L"/log/"));
124 ftemplate = clean_path(boost::filesystem::complete(paths.get(L"template-path", initial + L"/template/")).wstring());
125 data = clean_path(paths.get(L"data-path", initial + L"/data/"));
126 font = clean_path(paths.get(L"font-path", initial + L"/font/"));
127 thumbnail = clean_path(paths.get(L"thumbnail-path", paths.get(L"thumbnails-path", initial + L"/thumbnail/")));
131 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
135 media = ensure_trailing_slash(resolve_or_create(media));
136 log = ensure_trailing_slash(resolve_or_create(log));
137 ftemplate = ensure_trailing_slash(resolve_or_create(ftemplate));
138 data = ensure_trailing_slash(resolve_or_create(data));
139 font = ensure_trailing_slash(resolve_or_create(font));
140 thumbnail = ensure_trailing_slash(resolve_or_create(thumbnail));
142 ensure_writable(log);
143 ensure_writable(ftemplate);
144 ensure_writable(data);
145 ensure_writable(thumbnail);
148 const std::wstring& initial_folder()
150 check_is_configured();
154 const std::wstring& media_folder()
156 check_is_configured();
160 const std::wstring& log_folder()
162 check_is_configured();
166 const std::wstring& template_folder()
168 check_is_configured();
172 const std::wstring& data_folder()
174 check_is_configured();
178 const std::wstring& font_folder()
180 check_is_configured();
184 const std::wstring& thumbnail_folder()
186 check_is_configured();
190 #define QUOTE(str) #str
191 #define EXPAND_AND_QUOTE(str) QUOTE(str)
193 const std::wstring& version()
195 static std::wstring ver = u16(
196 EXPAND_AND_QUOTE(CASPAR_GEN) "."
197 EXPAND_AND_QUOTE(CASPAR_MAYOR) "."
198 EXPAND_AND_QUOTE(CASPAR_MINOR) "."
199 EXPAND_AND_QUOTE(CASPAR_REV) " "
205 const boost::property_tree::wptree& properties()
207 check_is_configured();
211 void log_configuration_warnings()
216 if (pt.get_optional<std::wstring>(L"configuration.paths.thumbnails-path"))
217 CASPAR_LOG(warning) << L"Element thumbnails-path in casparcg.config has been deprecated. Use thumbnail-path instead.";