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>
43 namespace caspar { namespace env {
47 std::wstring ftemplate;
50 std::wstring thumbnails;
51 boost::property_tree::wptree pt;
53 void check_is_configured()
56 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
59 void configure(const std::wstring& filename)
63 auto initialPath = boost::filesystem::initial_path().wstring();
65 boost::filesystem::wifstream file(initialPath + L"/" + filename);
66 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
68 auto paths = pt.get_child(L"configuration.paths");
69 media = paths.get(L"media-path", initialPath + L"/media/");
70 log = paths.get(L"log-path", initialPath + L"/log/");
71 ftemplate = boost::filesystem::complete(paths.get(L"template-path", initialPath + L"/template/")).wstring();
72 data = paths.get(L"data-path", initialPath + L"/data/");
73 font = paths.get(L"fonts-path", initialPath + L"/fonts/");
74 thumbnails = paths.get(L"thumbnails-path", initialPath + L"/data/");
78 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
84 auto found_media_path = find_case_insensitive(media);
86 media = *found_media_path;
88 boost::filesystem::create_directories(media);
90 auto found_template_path = find_case_insensitive(ftemplate);
91 if (found_template_path)
92 ftemplate = *found_template_path;
94 boost::filesystem::create_directories(ftemplate);
96 auto found_data_path = find_case_insensitive(data);
98 data = *found_data_path;
100 boost::filesystem::create_directories(data);
102 auto found_font_path = find_case_insensitive(font);
104 font = *found_font_path;
106 boost::filesystem::create_directories(font);
108 auto found_thumbnails_path = find_case_insensitive(thumbnails);
109 if (found_thumbnails_path)
110 thumbnails = *found_thumbnails_path;
112 boost::filesystem::create_directories(thumbnails);
114 auto found_log_path = find_case_insensitive(log);
116 log = *found_log_path;
117 else if (!boost::filesystem::create_directories(log))
120 //Make sure that all paths have a trailing slash
121 if(media.at(media.length()-1) != L'/')
123 if(log.at(log.length()-1) != L'/')
125 if(ftemplate.at(ftemplate.length()-1) != L'/')
126 ftemplate.append(L"/");
127 if(data.at(data.length()-1) != L'/')
129 if(font.at(font.length()-1) != L'/')
131 if(thumbnails.at(thumbnails.length()-1) != L'/')
132 thumbnails.append(L"/");
136 auto initialPath = boost::filesystem::initial_path().wstring();
138 for(auto it = boost::filesystem::directory_iterator(initialPath); it != boost::filesystem::directory_iterator(); ++it)
140 if(it->path().wstring().find(L".fth") != std::wstring::npos)
142 auto from_path = *it;
143 auto to_path = boost::filesystem::path(ftemplate + L"/" + it->path().wstring());
145 if(boost::filesystem::exists(to_path))
146 boost::filesystem::remove(to_path);
148 boost::filesystem::copy_file(from_path, to_path);
154 CASPAR_LOG_CURRENT_EXCEPTION();
155 CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";
160 CASPAR_LOG_CURRENT_EXCEPTION();
161 CASPAR_LOG(error) << L"Failed to create configured directories.";
165 const std::wstring& media_folder()
167 check_is_configured();
171 const std::wstring& log_folder()
173 check_is_configured();
177 const std::wstring& template_folder()
179 check_is_configured();
183 const std::wstring& data_folder()
185 check_is_configured();
189 const std::wstring& font_folder()
191 check_is_configured();
195 const std::wstring& thumbnails_folder()
197 check_is_configured();
201 #define QUOTE(str) #str
202 #define EXPAND_AND_QUOTE(str) QUOTE(str)
204 const std::wstring& version()
206 static std::wstring ver = u16(
207 EXPAND_AND_QUOTE(CASPAR_GEN) "."
208 EXPAND_AND_QUOTE(CASPAR_MAYOR) "."
209 EXPAND_AND_QUOTE(CASPAR_MINOR) "."
215 const boost::property_tree::wptree& properties()
217 check_is_configured();