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