]> git.sesse.net Git - casparcg/blob - common/env.cpp
Updated changes and updated version to stable.
[casparcg] / common / env.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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 namespace fs = boost::filesystem;\r
43 \r
44 std::wstring media;\r
45 std::wstring log;\r
46 std::wstring ftemplate;\r
47 std::wstring data;\r
48 std::wstring thumbnails;\r
49 boost::property_tree::wptree pt;\r
50 \r
51 void check_is_configured()\r
52 {\r
53         if(pt.empty())\r
54                 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Enviroment properties has not been configured"));\r
55 }\r
56 \r
57 void configure(const std::wstring& filename)\r
58 {\r
59         try\r
60         {\r
61                 auto initialPath = fs::initial_path<fs::path>().wstring();\r
62         \r
63                 std::wifstream file(initialPath + L"\\" + filename);\r
64                 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);\r
65 \r
66                 auto paths = pt.get_child(L"configuration.paths");\r
67                 media = widen(paths.get(L"media-path", initialPath + L"\\media\\"));\r
68                 log = widen(paths.get(L"log-path", initialPath + L"\\log\\"));\r
69                 ftemplate = fs::complete(fs::path(widen(paths.get(L"template-path", initialPath + L"\\template\\")))).wstring();                \r
70                 data = widen(paths.get(L"data-path", initialPath + L"\\data\\"));\r
71                 thumbnails = widen(paths.get(L"thumbnails-path", initialPath + L"\\thumbnails\\"));\r
72 \r
73                 //Make sure that all paths have a trailing backslash\r
74                 if(media.at(media.length()-1) != L'\\')\r
75                         media.append(L"\\");\r
76                 if(log.at(log.length()-1) != L'\\')\r
77                         log.append(L"\\");\r
78                 if(ftemplate.at(ftemplate.length()-1) != L'\\')\r
79                         ftemplate.append(L"\\");\r
80                 if(data.at(data.length()-1) != L'\\')\r
81                         data.append(L"\\");\r
82                 if(thumbnails.at(thumbnails.length()-1) != L'\\')\r
83                         thumbnails.append(L"\\");\r
84 \r
85                 try\r
86                 {\r
87                         for(auto it = fs::directory_iterator(initialPath); it != fs::directory_iterator(); ++it)\r
88                         {\r
89                                 if(it->path().filename().wstring().find(L".fth") != std::wstring::npos)                 \r
90                                 {\r
91                                         auto from_path = *it;\r
92                                         auto to_path = fs::path(ftemplate + L"/" + it->path().filename().wstring());\r
93                                 \r
94                                         if(fs::exists(to_path))\r
95                                                 fs::remove(to_path);\r
96 \r
97                                         fs::copy_file(from_path, to_path);\r
98                                 }       \r
99                         }\r
100                 }\r
101                 catch(...)\r
102                 {\r
103                         CASPAR_LOG_CURRENT_EXCEPTION();\r
104                         CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";\r
105                 }\r
106         }\r
107         catch(...)\r
108         {\r
109                 std::wcout << L" ### Invalid configuration file. ###";\r
110                 throw;\r
111         }\r
112 \r
113         try\r
114         {\r
115                 auto media_path = fs::path(media);\r
116                 if(!fs::exists(media_path))\r
117                         fs::create_directory(media_path);\r
118                 \r
119                 auto log_path = fs::path(log);\r
120                 if(!fs::exists(log_path))\r
121                         fs::create_directory(log_path);\r
122                 \r
123                 auto template_path = fs::path(ftemplate);\r
124                 if(!fs::exists(template_path))\r
125                         fs::create_directory(template_path);\r
126                 \r
127                 auto data_path = fs::path(data);\r
128                 if(!fs::exists(data_path))\r
129                         fs::create_directory(data_path);\r
130                 \r
131                 auto thumbnails_path = fs::path(thumbnails);\r
132                 if(!fs::exists(thumbnails_path))\r
133                         fs::create_directory(thumbnails_path);\r
134         }\r
135         catch(...)\r
136         {\r
137                 CASPAR_LOG_CURRENT_EXCEPTION();\r
138                 CASPAR_LOG(error) << L"Failed to create configured directories.";\r
139         }\r
140 }\r
141         \r
142 const std::wstring& media_folder()\r
143 {\r
144         check_is_configured();\r
145         return media;\r
146 }\r
147 \r
148 const std::wstring& log_folder()\r
149 {\r
150         check_is_configured();\r
151         return log;\r
152 }\r
153 \r
154 const std::wstring& template_folder()\r
155 {\r
156         check_is_configured();\r
157         return ftemplate;\r
158 }\r
159 \r
160 const std::wstring& data_folder()\r
161 {\r
162         check_is_configured();\r
163         return data;\r
164 }\r
165 \r
166 const std::wstring& thumbnails_folder()\r
167 {\r
168         check_is_configured();\r
169         return thumbnails;\r
170 }\r
171 \r
172 #define QUOTE(str) #str\r
173 #define EXPAND_AND_QUOTE(str) QUOTE(str)\r
174 \r
175 const std::wstring& version()\r
176 {\r
177         static std::wstring ver = widen(\r
178                         EXPAND_AND_QUOTE(CASPAR_GEN)    "." \r
179                         EXPAND_AND_QUOTE(CASPAR_MAYOR)  "." \r
180                         EXPAND_AND_QUOTE(CASPAR_MINOR)  "." \r
181                         CASPAR_REV      " " \r
182                         CASPAR_TAG);\r
183         return ver;\r
184 }\r
185 \r
186 const boost::property_tree::wptree& properties()\r
187 {\r
188         check_is_configured();\r
189         return pt;\r
190 }\r
191 \r
192 }}