]> git.sesse.net Git - casparcg/blob - common/env.cpp
[psd] Fixed wrong animation pace when interlaced video formats are used.
[casparcg] / common / env.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "stdafx.h"
23
24 #include "env.h"
25
26 #include "../version.h"
27
28 #include "except.h"
29 #include "log.h"
30 #include "string.h"
31 #include "os/filesystem.h"
32
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>
38
39 #include <functional>
40 #include <iostream>
41 #include <fstream>
42 #include <boost/algorithm/string/replace.hpp>
43
44 namespace caspar { namespace env {
45
46 std::wstring initial;
47 std::wstring media;
48 std::wstring log;
49 std::wstring ftemplate;
50 std::wstring data;
51 std::wstring font;
52 std::wstring thumbnail;
53 boost::property_tree::wptree pt;
54
55 void check_is_configured()
56 {
57         if(pt.empty())
58                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
59 }
60
61 std::wstring clean_path(std::wstring path)
62 {
63         boost::replace_all(path, L"\\\\", L"/");
64         boost::replace_all(path, L"\\", L"/");
65
66         return path;
67 }
68
69 std::wstring ensure_trailing_slash(std::wstring folder)
70 {
71         if (folder.at(folder.length() - 1) != L'/')
72                 folder.append(L"/");
73
74         return folder;
75 }
76
77 std::wstring resolve_or_create(const std::wstring& folder)
78 {
79         auto found_path = find_case_insensitive(folder);
80
81         if (found_path)
82                 return *found_path;
83         else
84         {
85                 boost::system::error_code ec;
86                 boost::filesystem::create_directories(folder, ec);
87
88                 if (ec)
89                         CASPAR_THROW_EXCEPTION(user_error() << msg_info("Failed to create directory " + u8(folder) + " (" + ec.message() + ")"));
90
91                 return folder;
92         }
93 }
94
95 void ensure_writable(const std::wstring& folder)
96 {
97         static const std::wstring CREATE_FILE_TEST = L"casparcg_test_writable.empty";
98
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);
102
103         if (out.fail())
104         {
105                 boost::filesystem::remove(test_file, ec);
106                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Directory " + folder + L" is not writable."));
107         }
108
109         out.close();
110         boost::filesystem::remove(test_file, ec);
111 }
112
113 void configure(const std::wstring& filename)
114 {
115         try
116         {
117                 initial = clean_path(boost::filesystem::initial_path().wstring());
118
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);
121
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/")));
129         }
130         catch (...)
131         {
132                 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
133                 throw;
134         }
135
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));
142
143         ensure_writable(log);
144         ensure_writable(ftemplate);
145         ensure_writable(data);
146         ensure_writable(thumbnail);
147 }
148
149 const std::wstring& initial_folder()
150 {
151         check_is_configured();
152         return initial;
153 }
154
155 const std::wstring& media_folder()
156 {
157         check_is_configured();
158         return media;
159 }
160
161 const std::wstring& log_folder()
162 {
163         check_is_configured();
164         return log;
165 }
166
167 const std::wstring& template_folder()
168 {
169         check_is_configured();
170         return ftemplate;
171 }
172
173 const std::wstring& data_folder()
174 {
175         check_is_configured();
176         return data;
177 }
178
179 const std::wstring& font_folder()
180 {
181         check_is_configured();
182         return font;
183 }
184
185 const std::wstring& thumbnail_folder()
186 {
187         check_is_configured();
188         return thumbnail;
189 }
190
191 #define QUOTE(str) #str
192 #define EXPAND_AND_QUOTE(str) QUOTE(str)
193
194 const std::wstring& version()
195 {
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)    " "
201                         CASPAR_HASH                                             " "
202                         CASPAR_TAG);
203         return ver;
204 }
205
206 const boost::property_tree::wptree& properties()
207 {
208         check_is_configured();
209         return pt;
210 }
211
212 void log_configuration_warnings()
213 {
214         if (pt.empty())
215                 return;
216
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.";
219 }
220
221 }}