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