]> git.sesse.net Git - casparcg/blob - common/env.cpp
2.0.2: Updated file info headers.
[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 "utility/string.h"\r
30 \r
31 #include <boost/property_tree/ptree.hpp>\r
32 #include <boost/property_tree/xml_parser.hpp>\r
33 #include <boost/filesystem.hpp>\r
34 #include <boost/thread/once.hpp>\r
35 \r
36 #include <functional>\r
37 #include <iostream>\r
38 \r
39 namespace caspar { namespace env {\r
40 \r
41 using namespace boost::filesystem2;\r
42 \r
43 std::wstring media;\r
44 std::wstring log;\r
45 std::wstring ftemplate;\r
46 std::wstring data;\r
47 boost::property_tree::ptree pt;\r
48 \r
49 void check_is_configured()\r
50 {\r
51         if(pt.empty())\r
52                 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Enviroment properties has not been configured"));\r
53 }\r
54 \r
55 void configure(const std::string& filename)\r
56 {\r
57         try\r
58         {\r
59                 std::string initialPath = boost::filesystem::initial_path().file_string();\r
60         \r
61                 boost::property_tree::read_xml(initialPath + "\\" + filename, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);\r
62 \r
63                 auto paths = pt.get_child("configuration.paths");\r
64                 media = widen(paths.get("media-path", initialPath + "\\media\\"));\r
65                 log = widen(paths.get("log-path", initialPath + "\\log\\"));\r
66                 ftemplate = complete(wpath(widen(paths.get("template-path", initialPath + "\\template\\")))).string();          \r
67                 data = widen(paths.get("data-path", initialPath + "\\data\\"));\r
68         }\r
69         catch(...)\r
70         {\r
71                 std::wcout << L" ### Invalid configuration file. ###";\r
72                 throw;\r
73         }\r
74 }\r
75         \r
76 const std::wstring& media_folder()\r
77 {\r
78         check_is_configured();\r
79         return media;\r
80 }\r
81 \r
82 const std::wstring& log_folder()\r
83 {\r
84         check_is_configured();\r
85         return log;\r
86 }\r
87 \r
88 const std::wstring& template_folder()\r
89 {\r
90         check_is_configured();\r
91         return ftemplate;\r
92 }\r
93 \r
94 const std::wstring& data_folder()\r
95 {\r
96         check_is_configured();\r
97         return data;\r
98 }\r
99 \r
100 const std::wstring& version()\r
101 {\r
102         static std::wstring ver = std::wstring(L"") + CASPAR_GEN + L"." + CASPAR_MAYOR + L"." + CASPAR_MINOR + L"." + CASPAR_REV + L" " + CASPAR_TAG;\r
103         return ver;\r
104 }\r
105 \r
106 const boost::property_tree::ptree& properties()\r
107 {\r
108         check_is_configured();\r
109         return pt;\r
110 }\r
111 \r
112 }}