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 {
48 std::wstring ftemplate;
51 std::wstring thumbnails;
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 void configure(const std::wstring& filename)
64 initial = boost::filesystem::initial_path().wstring();
66 boost::filesystem::wifstream file(initial + L"/" + filename);
67 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
69 auto paths = pt.get_child(L"configuration.paths");
70 media = paths.get(L"media-path", initial + L"/media/");
71 log = paths.get(L"log-path", initial + L"/log/");
72 ftemplate = boost::filesystem::complete(paths.get(L"template-path", initial + L"/template/")).wstring();
73 data = paths.get(L"data-path", initial + L"/data/");
74 font = paths.get(L"font-path", initial + L"/font/");
75 thumbnails = paths.get(L"thumbnail-path", initial + L"/thumbnail/");
79 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
85 auto found_media_path = find_case_insensitive(media);
87 media = *found_media_path;
89 boost::filesystem::create_directories(media);
91 auto found_template_path = find_case_insensitive(ftemplate);
92 if (found_template_path)
93 ftemplate = *found_template_path;
95 boost::filesystem::create_directories(ftemplate);
97 auto found_data_path = find_case_insensitive(data);
99 data = *found_data_path;
101 boost::filesystem::create_directories(data);
103 auto found_font_path = find_case_insensitive(font);
105 font = *found_font_path;
107 boost::filesystem::create_directories(font);
109 auto found_thumbnails_path = find_case_insensitive(thumbnails);
110 if (found_thumbnails_path)
111 thumbnails = *found_thumbnails_path;
113 boost::filesystem::create_directories(thumbnails);
115 auto found_log_path = find_case_insensitive(log);
117 log = *found_log_path;
118 else if (!boost::filesystem::create_directories(log))
121 //Make sure that all paths have a trailing slash
122 if(media.at(media.length()-1) != L'/')
124 if(log.at(log.length()-1) != L'/')
126 if(ftemplate.at(ftemplate.length()-1) != L'/')
127 ftemplate.append(L"/");
128 if(data.at(data.length()-1) != L'/')
130 if(font.at(font.length()-1) != L'/')
132 if(thumbnails.at(thumbnails.length()-1) != L'/')
133 thumbnails.append(L"/");
137 CASPAR_LOG_CURRENT_EXCEPTION();
138 CASPAR_LOG(error) << L"Failed to create configured directories.";
142 const std::wstring& initial_folder()
144 check_is_configured();
148 const std::wstring& media_folder()
150 check_is_configured();
154 const std::wstring& log_folder()
156 check_is_configured();
160 const std::wstring& template_folder()
162 check_is_configured();
166 const std::wstring& data_folder()
168 check_is_configured();
172 const std::wstring& font_folder()
174 check_is_configured();
178 const std::wstring& thumbnails_folder()
180 check_is_configured();
184 #define QUOTE(str) #str
185 #define EXPAND_AND_QUOTE(str) QUOTE(str)
187 const std::wstring& version()
189 static std::wstring ver = u16(
190 EXPAND_AND_QUOTE(CASPAR_GEN) "."
191 EXPAND_AND_QUOTE(CASPAR_MAYOR) "."
192 EXPAND_AND_QUOTE(CASPAR_MINOR) "."
198 const boost::property_tree::wptree& properties()
200 check_is_configured();