]> git.sesse.net Git - casparcg/blob - common/env.cpp
21738e85486d65720b3986426daec5770d8bf307
[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 "exception/exceptions.h"\r
29 #include "log/log.h"\r
30 #include "utility/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 using namespace boost::filesystem2;\r
43 \r
44 std::wstring media;\r
45 std::wstring log;\r
46 std::wstring ftemplate;\r
47 std::wstring data;\r
48 boost::property_tree::wptree pt;\r
49 \r
50 void check_is_configured()\r
51 {\r
52         if(pt.empty())\r
53                 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Enviroment properties has not been configured"));\r
54 }\r
55 \r
56 void configure(const std::wstring& filename)\r
57 {\r
58         try\r
59         {\r
60                 auto initialPath = boost::filesystem::initial_path<boost::filesystem2::wpath>().file_string();\r
61         \r
62                 std::wifstream file(initialPath + L"\\" + filename);\r
63                 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);\r
64 \r
65                 auto paths = pt.get_child(L"configuration.paths");\r
66                 media = widen(paths.get(L"media-path", initialPath + L"\\media\\"));\r
67                 log = widen(paths.get(L"log-path", initialPath + L"\\log\\"));\r
68                 ftemplate = complete(wpath(widen(paths.get(L"template-path", initialPath + L"\\template\\")))).string();                \r
69                 data = widen(paths.get(L"data-path", initialPath + L"\\data\\"));\r
70 \r
71                 try\r
72                 {\r
73                         for(auto it = boost::filesystem2::wdirectory_iterator(initialPath); it != boost::filesystem2::wdirectory_iterator(); ++it)\r
74                         {\r
75                                 if(it->filename().find(L".fth") != std::wstring::npos)                  \r
76                                 {\r
77                                         auto from_path = *it;\r
78                                         auto to_path = boost::filesystem2::wpath(ftemplate + L"/" + it->filename());\r
79                                 \r
80                                         if(boost::filesystem2::exists(to_path))\r
81                                                 boost::filesystem2::remove(to_path);\r
82 \r
83                                         boost::filesystem2::copy_file(from_path, to_path);\r
84                                 }       \r
85                         }\r
86                 }\r
87                 catch(...)\r
88                 {\r
89                         CASPAR_LOG_CURRENT_EXCEPTION();\r
90                         CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";\r
91                 }\r
92         }\r
93         catch(...)\r
94         {\r
95                 std::wcout << L" ### Invalid configuration file. ###";\r
96                 throw;\r
97         }\r
98 \r
99         try\r
100         {\r
101                 auto media_path = boost::filesystem::wpath(media);\r
102                 if(!boost::filesystem::exists(media_path))\r
103                         boost::filesystem::create_directory(media_path);\r
104                 \r
105                 auto log_path = boost::filesystem::wpath(log);\r
106                 if(!boost::filesystem::exists(log_path))\r
107                         boost::filesystem::create_directory(log_path);\r
108                 \r
109                 auto template_path = boost::filesystem::wpath(ftemplate);\r
110                 if(!boost::filesystem::exists(template_path))\r
111                         boost::filesystem::create_directory(template_path);\r
112                 \r
113                 auto data_path = boost::filesystem::wpath(data);\r
114                 if(!boost::filesystem::exists(data_path))\r
115                         boost::filesystem::create_directory(data_path);\r
116         }\r
117         catch(...)\r
118         {\r
119                 CASPAR_LOG_CURRENT_EXCEPTION();\r
120                 CASPAR_LOG(error) << L"Failed to create configured directories.";\r
121         }\r
122 }\r
123         \r
124 const std::wstring& media_folder()\r
125 {\r
126         check_is_configured();\r
127         return media;\r
128 }\r
129 \r
130 const std::wstring& log_folder()\r
131 {\r
132         check_is_configured();\r
133         return log;\r
134 }\r
135 \r
136 const std::wstring& template_folder()\r
137 {\r
138         check_is_configured();\r
139         return ftemplate;\r
140 }\r
141 \r
142 const std::wstring& data_folder()\r
143 {\r
144         check_is_configured();\r
145         return data;\r
146 }\r
147 \r
148 const std::wstring& version()\r
149 {\r
150         static std::wstring ver = std::wstring(L"") + CASPAR_GEN + L"." + CASPAR_MAYOR + L"." + CASPAR_MINOR + L"." + CASPAR_REV + L" " + CASPAR_TAG;\r
151         return ver;\r
152 }\r
153 \r
154 const boost::property_tree::wptree& properties()\r
155 {\r
156         check_is_configured();\r
157         return pt;\r
158 }\r
159 \r
160 }}