]> git.sesse.net Git - casparcg/blob - common/env.cpp
2.1.0: -Fixed ffmpeg input crash. -Log throwing call-stack.
[casparcg] / common / env.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "stdafx.h"\r
23 \r
24 #include "env.h"\r
25 \r
26 #include "../version.h"\r
27 \r
28 #include "except.h"\r
29 #include "log.h"\r
30 #include "string.h"\r
31 \r
32 #include <boost/property_tree/ptree.hpp>\r
33 #include <boost/property_tree/xml_parser.hpp>\r
34 #include <boost/filesystem.hpp>\r
35 #include <boost/thread/once.hpp>\r
36 \r
37 #include <functional>\r
38 #include <iostream>\r
39 \r
40 namespace caspar { namespace env {\r
41         \r
42 std::wstring media;\r
43 std::wstring log;\r
44 std::wstring ftemplate;\r
45 std::wstring data;\r
46 boost::property_tree::wptree pt;\r
47 \r
48 void check_is_configured()\r
49 {\r
50         if(pt.empty())\r
51                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));\r
52 }\r
53 \r
54 void configure(const std::wstring& filename)\r
55 {\r
56         try\r
57         {\r
58                 auto initialPath = boost::filesystem3::initial_path().wstring();\r
59         \r
60                 std::wifstream file(initialPath + L"\\" + filename);\r
61                 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);\r
62 \r
63                 auto paths      = pt.get_child(L"configuration.paths");\r
64                 media           = paths.get(L"media-path", initialPath + L"\\media\\");\r
65                 log                     = paths.get(L"log-path", initialPath + L"\\log\\");\r
66                 ftemplate       = boost::filesystem3::complete(paths.get(L"template-path", initialPath + L"\\template\\")).wstring();           \r
67                 data            = paths.get(L"data-path", initialPath + L"\\data\\");\r
68 \r
69                 try\r
70                 {\r
71                         for(auto it = boost::filesystem::directory_iterator(initialPath); it != boost::filesystem::directory_iterator(); ++it)\r
72                         {\r
73                                 if(it->path().wstring().find(L".fth") != std::wstring::npos)                    \r
74                                 {\r
75                                         auto from_path = *it;\r
76                                         auto to_path = boost::filesystem::path(ftemplate + L"/" + it->path().wstring());\r
77                                 \r
78                                         if(boost::filesystem::exists(to_path))\r
79                                                 boost::filesystem::remove(to_path);\r
80 \r
81                                         boost::filesystem::copy_file(from_path, to_path);\r
82                                 }       \r
83                         }\r
84                 }\r
85                 catch(...)\r
86                 {\r
87                         CASPAR_LOG_CURRENT_EXCEPTION();\r
88                         CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";\r
89                 }\r
90         }\r
91         catch(...)\r
92         {\r
93                 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";\r
94                 throw;\r
95         }\r
96 \r
97         try\r
98         {\r
99                 try\r
100                 {\r
101                         auto log_path = boost::filesystem::path(log);\r
102                         if(!boost::filesystem::exists(log_path))\r
103                                 boost::filesystem::create_directories(log_path);\r
104                 }\r
105                 catch(...)\r
106                 {\r
107                         log = L"./";\r
108                 }\r
109 \r
110                 auto media_path = boost::filesystem::path(media);\r
111                 if(!boost::filesystem::exists(media_path))\r
112                         boost::filesystem::create_directories(media_path);\r
113                                 \r
114                 auto template_path = boost::filesystem::path(ftemplate);\r
115                 if(!boost::filesystem::exists(template_path))\r
116                         boost::filesystem::create_directories(template_path);\r
117                 \r
118                 auto data_path = boost::filesystem::path(data);\r
119                 if(!boost::filesystem::exists(data_path))\r
120                         boost::filesystem::create_directories(data_path);\r
121         }\r
122         catch(...)\r
123         {\r
124                 CASPAR_LOG_CURRENT_EXCEPTION();\r
125                 CASPAR_LOG(error) << L"Failed to create configured directories.";\r
126         }\r
127 }\r
128         \r
129 const std::wstring& media_folder()\r
130 {\r
131         check_is_configured();\r
132         return media;\r
133 }\r
134 \r
135 const std::wstring& log_folder()\r
136 {\r
137         check_is_configured();\r
138         return log;\r
139 }\r
140 \r
141 const std::wstring& template_folder()\r
142 {\r
143         check_is_configured();\r
144         return ftemplate;\r
145 }\r
146 \r
147 const std::wstring& data_folder()\r
148 {\r
149         check_is_configured();\r
150         return data;\r
151 }\r
152 \r
153 #define QUOTE(str) #str\r
154 #define EXPAND_AND_QUOTE(str) QUOTE(str)\r
155 \r
156 const std::wstring& version()\r
157 {\r
158         static std::wstring ver = u16(\r
159                         EXPAND_AND_QUOTE(CASPAR_GEN)    "." \r
160                         EXPAND_AND_QUOTE(CASPAR_MAYOR)  "." \r
161                         EXPAND_AND_QUOTE(CASPAR_MINOR)  "." \r
162                         EXPAND_AND_QUOTE(CASPAR_REV)    " " \r
163                         CASPAR_TAG);\r
164         return ver;\r
165 }\r
166 \r
167 const boost::property_tree::wptree& properties()\r
168 {\r
169         check_is_configured();\r
170         return pt;\r
171 }\r
172 \r
173 }}