X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=common%2Fenv.cpp;h=4dde3c7823b0ad46cbd522d995c7f6d2a173cf6e;hb=f02cacc5b01c75f1837d82fd9fd0988b02d7c9e4;hp=b19c146e4bd9b8e0b29a9225227db381b002f5a0;hpb=dfecc075b136ca43df22061fdd32a5c7ad2e397d;p=casparcg diff --git a/common/env.cpp b/common/env.cpp index b19c146e4..4dde3c782 100644 --- a/common/env.cpp +++ b/common/env.cpp @@ -1,10 +1,33 @@ +/* +* Copyright (c) 2011 Sveriges Television AB +* +* This file is part of CasparCG (www.casparcg.com). +* +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . +* +* Author: Robert Nagy, ronag89@gmail.com +*/ + #include "stdafx.h" #include "env.h" #include "../version.h" -#include "utility/string_convert.h" +#include "exception/exceptions.h" +#include "log/log.h" +#include "utility/string.h" #include #include @@ -12,84 +35,104 @@ #include #include +#include -namespace caspar -{ +namespace caspar { namespace env { + +using namespace boost::filesystem2; std::wstring media; std::wstring log; std::wstring ftemplate; -std::wstring ftemplate_host; std::wstring data; -boost::property_tree::ptree pt; +boost::property_tree::wptree pt; -void do_setup() +void check_is_configured() { - std::string initialPath = boost::filesystem::initial_path().file_string(); - - boost::property_tree::read_xml(initialPath + "\\caspar.config", pt); - - auto paths = pt.get_child("configuration.paths"); - media = widen(paths.get("media-path", initialPath + "\\media\\")); - log = widen(paths.get("log-path", initialPath + "\\log\\")); - ftemplate = widen(paths.get("template-path", initialPath + "\\template\\")); - ftemplate_host = widen(paths.get("template-host-path", initialPath + "\\template\\cg.fth")); - data = widen(paths.get("data-path", initialPath + "\\data\\")); + if(pt.empty()) + BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Enviroment properties has not been configured")); } -void setup() +void configure(const std::wstring& filename) { - static boost::once_flag setup_flag = BOOST_ONCE_INIT; - boost::call_once(do_setup, setup_flag); + try + { + auto initialPath = boost::filesystem::initial_path().file_string(); + + std::wifstream file(initialPath + L"\\" + filename); + boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments); + + auto paths = pt.get_child(L"configuration.paths"); + media = widen(paths.get(L"media-path", initialPath + L"\\media\\")); + log = widen(paths.get(L"log-path", initialPath + L"\\log\\")); + ftemplate = complete(wpath(widen(paths.get(L"template-path", initialPath + L"\\template\\")))).string(); + data = widen(paths.get(L"data-path", initialPath + L"\\data\\")); + } + catch(...) + { + std::wcout << L" ### Invalid configuration file. ###"; + throw; + } + + try + { + auto media_path = boost::filesystem::wpath(media); + if(!boost::filesystem::exists(media_path)) + boost::filesystem::create_directory(media_path); + + auto log_path = boost::filesystem::wpath(log); + if(!boost::filesystem::exists(log_path)) + boost::filesystem::create_directory(log_path); + + auto template_path = boost::filesystem::wpath(ftemplate); + if(!boost::filesystem::exists(template_path)) + boost::filesystem::create_directory(template_path); + + auto data_path = boost::filesystem::wpath(data); + if(!boost::filesystem::exists(data_path)) + boost::filesystem::create_directory(data_path); + } + catch(...) + { + CASPAR_LOG_CURRENT_EXCEPTION(); + CASPAR_LOG(error) << L"Failed to create configured directories."; + } } -const std::wstring& env::media_folder() +const std::wstring& media_folder() { - setup(); + check_is_configured(); return media; } -const std::wstring& env::log_folder() +const std::wstring& log_folder() { - setup(); + check_is_configured(); return log; } -const std::wstring& env::template_folder() +const std::wstring& template_folder() { - setup(); + check_is_configured(); return ftemplate; } -const std::wstring& env::template_host() -{ - setup(); - return ftemplate_host; -} - - -const std::wstring& env::data_folder() +const std::wstring& data_folder() { - setup(); + check_is_configured(); return data; } -const std::wstring& env::version() +const std::wstring& version() { - static std::wstring ver = std::wstring(L"") + CASPAR_GEN + L"." + CASPAR_MAYOR + L"." + CASPAR_MINOR + L"." + CASPAR_REV; + static std::wstring ver = std::wstring(L"") + CASPAR_GEN + L"." + CASPAR_MAYOR + L"." + CASPAR_MINOR + L"." + CASPAR_REV + L" " + CASPAR_TAG; return ver; } -const std::wstring& env::version_tag() -{ - static std::wstring tag = L"Experimental"; - return tag; -} - -const boost::property_tree::ptree& env::properties() +const boost::property_tree::wptree& properties() { - setup(); + check_is_configured(); return pt; } -} \ No newline at end of file +}} \ No newline at end of file