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>
37 #include <boost/thread/once.hpp>
42 #include <boost/algorithm/string/replace.hpp>
44 namespace caspar { namespace env {
49 std::wstring ftemplate;
52 std::wstring thumbnail;
53 boost::property_tree::wptree pt;
55 void check_is_configured()
58 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
61 std::wstring clean_path(std::wstring path)
63 boost::replace_all(path, L"\\\\", L"/");
64 boost::replace_all(path, L"\\", L"/");
69 std::wstring ensure_trailing_slash(std::wstring folder)
71 if (folder.at(folder.length() - 1) != L'/')
77 std::wstring resolve_or_create(const std::wstring& folder)
79 auto found_path = find_case_insensitive(folder);
85 boost::system::error_code ec;
86 boost::filesystem::create_directories(folder, ec);
89 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Failed to create directory " + u8(folder) + " (" + ec.message() + ")"));
95 void ensure_writable(const std::wstring& folder)
97 static const std::wstring CREATE_FILE_TEST = L"casparcg_test_writable.empty";
99 boost::system::error_code ec;
100 boost::filesystem::path test_file(folder + L"/" + CREATE_FILE_TEST);
101 boost::filesystem::ofstream out(folder + L"/" + CREATE_FILE_TEST);
105 boost::filesystem::remove(test_file, ec);
106 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Directory " + folder + L" is not writable."));
110 boost::filesystem::remove(test_file, ec);
113 void configure(const std::wstring& filename)
117 initial = clean_path(boost::filesystem::initial_path().wstring());
119 boost::filesystem::wifstream file(initial + L"/" + filename);
120 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
122 auto paths = pt.get_child(L"configuration.paths");
123 media = clean_path(paths.get(L"media-path", initial + L"/media/"));
124 log = clean_path(paths.get(L"log-path", initial + L"/log/"));
125 ftemplate = clean_path(boost::filesystem::complete(paths.get(L"template-path", initial + L"/template/")).wstring());
126 data = clean_path(paths.get(L"data-path", initial + L"/data/"));
127 font = clean_path(paths.get(L"font-path", initial + L"/font/"));
128 thumbnail = clean_path(paths.get(L"thumbnail-path", paths.get(L"thumbnails-path", initial + L"/thumbnail/")));
132 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
136 media = ensure_trailing_slash(resolve_or_create(media));
137 log = ensure_trailing_slash(resolve_or_create(log));
138 ftemplate = ensure_trailing_slash(resolve_or_create(ftemplate));
139 data = ensure_trailing_slash(resolve_or_create(data));
140 font = ensure_trailing_slash(resolve_or_create(font));
141 thumbnail = ensure_trailing_slash(resolve_or_create(thumbnail));
143 ensure_writable(log);
144 ensure_writable(ftemplate);
145 ensure_writable(data);
146 ensure_writable(thumbnail);
149 const std::wstring& initial_folder()
151 check_is_configured();
155 const std::wstring& media_folder()
157 check_is_configured();
161 const std::wstring& log_folder()
163 check_is_configured();
167 const std::wstring& template_folder()
169 check_is_configured();
173 const std::wstring& data_folder()
175 check_is_configured();
179 const std::wstring& font_folder()
181 check_is_configured();
185 const std::wstring& thumbnail_folder()
187 check_is_configured();
191 #define QUOTE(str) #str
192 #define EXPAND_AND_QUOTE(str) QUOTE(str)
194 const std::wstring& version()
196 static std::wstring ver = u16(
197 EXPAND_AND_QUOTE(CASPAR_GEN) "."
198 EXPAND_AND_QUOTE(CASPAR_MAYOR) "."
199 EXPAND_AND_QUOTE(CASPAR_MINOR) "."
200 EXPAND_AND_QUOTE(CASPAR_REV) " "
206 const boost::property_tree::wptree& properties()
208 check_is_configured();
212 void log_configuration_warnings()
217 if (pt.get_optional<std::wstring>(L"configuration.paths.thumbnails-path"))
218 CASPAR_LOG(warning) << L"Element thumbnails-path in casparcg.config has been deprecated. Use thumbnail-path instead.";