]> git.sesse.net Git - casparcg/blob - common/env.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / env.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "stdafx.h"\r
21 \r
22 #include "env.h"\r
23 \r
24 #include "../version.h"\r
25 \r
26 #include "exception\exceptions.h"\r
27 #include "utility/string.h"\r
28 \r
29 #include <boost/property_tree/ptree.hpp>\r
30 #include <boost/property_tree/xml_parser.hpp>\r
31 #include <boost/filesystem.hpp>\r
32 #include <boost/thread/once.hpp>\r
33 \r
34 #include <functional>\r
35 #include <iostream>\r
36 \r
37 namespace caspar { namespace env {\r
38 \r
39 using namespace boost::filesystem2;\r
40 \r
41 std::wstring media;\r
42 std::wstring log;\r
43 std::wstring ftemplate;\r
44 std::wstring ftemplate_host;\r
45 std::wstring data;\r
46 boost::property_tree::ptree pt;\r
47 \r
48 void check_is_configured()\r
49 {\r
50         if(pt.empty())\r
51                 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Enviroment properties has not been configured"));\r
52 }\r
53 \r
54 void configure(const std::string& filename)\r
55 {\r
56         try\r
57         {\r
58                 std::string initialPath = boost::filesystem::initial_path().file_string();\r
59         \r
60                 boost::property_tree::read_xml(initialPath + "\\" + filename, pt);\r
61 \r
62                 auto paths = pt.get_child("configuration.paths");\r
63                 media = widen(paths.get("media-path", initialPath + "\\media\\"));\r
64                 log = widen(paths.get("log-path", initialPath + "\\log\\"));\r
65                 ftemplate = complete(wpath(widen(paths.get("template-path", initialPath + "\\template\\")))).string();\r
66 \r
67                 ftemplate_host = widen(paths.get("template-host", "cg.fth"));\r
68 \r
69                 data = widen(paths.get("data-path", initialPath + "\\data\\"));\r
70         }\r
71         catch(...)\r
72         {\r
73                 std::wcout << L" ### Invalid configuration file. ###";\r
74                 throw;\r
75         }\r
76 }\r
77         \r
78 const std::wstring& media_folder()\r
79 {\r
80         check_is_configured();\r
81         return media;\r
82 }\r
83 \r
84 const std::wstring& log_folder()\r
85 {\r
86         check_is_configured();\r
87         return log;\r
88 }\r
89 \r
90 const std::wstring& template_folder()\r
91 {\r
92         check_is_configured();\r
93         return ftemplate;\r
94 }\r
95 \r
96 const std::wstring& template_host()\r
97 {\r
98         check_is_configured();\r
99         return ftemplate_host;\r
100 }\r
101 \r
102 const std::wstring& data_folder()\r
103 {\r
104         check_is_configured();\r
105         return data;\r
106 }\r
107 \r
108 const std::wstring& version()\r
109 {\r
110         static std::wstring ver = std::wstring(L"") + CASPAR_GEN + L"." + CASPAR_MAYOR + L"." + CASPAR_MINOR + L"." + CASPAR_REV + L" ALPHA";\r
111         return ver;\r
112 }\r
113 \r
114 const boost::property_tree::ptree& properties()\r
115 {\r
116         check_is_configured();\r
117         return pt;\r
118 }\r
119 \r
120 }}