]> git.sesse.net Git - casparcg/blob - common/env.cpp
Made the code more portable.
[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/filesystem/fstream.hpp>
36 #include <boost/thread/once.hpp>
37
38 #include <functional>
39 #include <iostream>
40 #include <fstream>
41
42 namespace caspar { namespace env {
43         
44 std::wstring media;
45 std::wstring log;
46 std::wstring ftemplate;
47 std::wstring data;
48 std::wstring font;
49 std::wstring thumbnails;
50 boost::property_tree::wptree pt;
51
52 void check_is_configured()
53 {
54         if(pt.empty())
55                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
56 }
57
58 void configure(const std::wstring& filename)
59 {
60         try
61         {
62                 auto initialPath = boost::filesystem::initial_path().wstring();
63         
64                 boost::filesystem::wifstream file(initialPath + L"\\" + filename);
65                 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
66
67                 auto paths      = pt.get_child(L"configuration.paths");
68                 media           = paths.get(L"media-path", initialPath + L"\\media\\");
69                 log                     = paths.get(L"log-path", initialPath + L"\\log\\");
70                 ftemplate       = boost::filesystem::complete(paths.get(L"template-path", initialPath + L"\\template\\")).wstring();            
71                 data            = paths.get(L"data-path", initialPath + L"\\data\\");
72                 font            = paths.get(L"font-path", initialPath + L"\\fonts\\");
73                 thumbnails      = paths.get(L"thumbnails-path", initialPath + L"\\data\\");
74
75                 //Make sure that all paths have a trailing backslash
76                 if(media.at(media.length()-1) != L'\\')
77                         media.append(L"\\");
78                 if(log.at(log.length()-1) != L'\\')
79                         log.append(L"\\");
80                 if(ftemplate.at(ftemplate.length()-1) != L'\\')
81                         ftemplate.append(L"\\");
82                 if(data.at(data.length()-1) != L'\\')
83                         data.append(L"\\");
84                 if(font.at(font.length()-1) != L'\\')
85                         font.append(L"\\");
86                 if(thumbnails.at(thumbnails.length()-1) != L'\\')
87                         thumbnails.append(L"\\");
88
89                 try
90                 {
91                         for(auto it = boost::filesystem::directory_iterator(initialPath); it != boost::filesystem::directory_iterator(); ++it)
92                         {
93                                 if(it->path().wstring().find(L".fth") != std::wstring::npos)                    
94                                 {
95                                         auto from_path = *it;
96                                         auto to_path = boost::filesystem::path(ftemplate + L"/" + it->path().wstring());
97                                 
98                                         if(boost::filesystem::exists(to_path))
99                                                 boost::filesystem::remove(to_path);
100
101                                         boost::filesystem::copy_file(from_path, to_path);
102                                 }       
103                         }
104                 }
105                 catch(...)
106                 {
107                         CASPAR_LOG_CURRENT_EXCEPTION();
108                         CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";
109                 }
110         }
111         catch(...)
112         {
113                 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
114                 throw;
115         }
116
117         try
118         {
119                 try
120                 {
121                         auto log_path = boost::filesystem::path(log);
122                         if(!boost::filesystem::exists(log_path))
123                                 boost::filesystem::create_directories(log_path);
124                 }
125                 catch(...)
126                 {
127                         log = L"./";
128                 }
129
130                 auto media_path = boost::filesystem::path(media);
131                 if(!boost::filesystem::exists(media_path))
132                         boost::filesystem::create_directories(media_path);
133                                 
134                 auto template_path = boost::filesystem::path(ftemplate);
135                 if(!boost::filesystem::exists(template_path))
136                         boost::filesystem::create_directories(template_path);
137                 
138                 auto data_path = boost::filesystem::path(data);
139                 if(!boost::filesystem::exists(data_path))
140                         boost::filesystem::create_directories(data_path);
141
142                 auto font_path = boost::filesystem::path(font);
143                 if(!boost::filesystem::exists(font_path))
144                         boost::filesystem::create_directories(font_path);
145
146                 auto thumbnails_path = boost::filesystem::path(thumbnails);
147                 if(!boost::filesystem::exists(thumbnails_path))
148                         boost::filesystem::create_directories(thumbnails_path);
149         }
150         catch(...)
151         {
152                 CASPAR_LOG_CURRENT_EXCEPTION();
153                 CASPAR_LOG(error) << L"Failed to create configured directories.";
154         }
155 }
156         
157 const std::wstring& media_folder()
158 {
159         check_is_configured();
160         return media;
161 }
162
163 const std::wstring& log_folder()
164 {
165         check_is_configured();
166         return log;
167 }
168
169 const std::wstring& template_folder()
170 {
171         check_is_configured();
172         return ftemplate;
173 }
174
175 const std::wstring& data_folder()
176 {
177         check_is_configured();
178         return data;
179 }
180
181 const std::wstring& font_folder()
182 {
183         check_is_configured();
184         return font;
185 }
186
187 const std::wstring& thumbnails_folder()
188 {
189         check_is_configured();
190         return thumbnails;
191 }
192
193 #define QUOTE(str) #str
194 #define EXPAND_AND_QUOTE(str) QUOTE(str)
195
196 const std::wstring& version()
197 {
198         static std::wstring ver = u16(
199                         EXPAND_AND_QUOTE(CASPAR_GEN)    "." 
200                         EXPAND_AND_QUOTE(CASPAR_MAYOR)  "." 
201                         EXPAND_AND_QUOTE(CASPAR_MINOR)  "." 
202                         CASPAR_REV      " " 
203                         CASPAR_TAG);
204         return ver;
205 }
206
207 const boost::property_tree::wptree& properties()
208 {
209         check_is_configured();
210         return pt;
211 }
212
213 std::wstring system_font_folder()
214 {
215         return L"C:\\windows\\Fonts\\";
216 }
217
218 }}