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