2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Niklas P Andersson, niklas.p.andersson@svt.se
22 #include "descriptor.h"
25 #include <common/log.h>
27 #include <boost/property_tree/ptree.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
32 namespace caspar { namespace psd {
34 std::wstring debug_ptree(const boost::property_tree::wptree& tree)
36 std::wstringstream str;
37 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
38 boost::property_tree::write_xml(str, tree, w);
43 class descriptor::context::scoped_holder
45 descriptor::context::ptr_type ctx_;
47 explicit scoped_holder(const std::wstring& key, descriptor::context::ptr_type ctx) : ctx_(ctx)
49 Ptree *parent = ctx_->stack.back();
50 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
51 ctx_->stack.push_back(child);
55 ctx_->stack.pop_back();
59 descriptor::descriptor(const std::wstring& debug_name) : context_(std::make_shared<context>(debug_name))
61 context_->stack.push_back(&context_->root);
63 descriptor::descriptor(const std::wstring& key, context::ptr_type context) : context_(context)
65 Ptree *parent = context_->stack.back();
66 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
67 context->stack.push_back(child);
69 descriptor::~descriptor()
71 context_->stack.pop_back();
74 void descriptor::populate(bigendian_file_input_stream& stream)
76 stream.read_unicode_string();
77 stream.read_id_string();
78 unsigned long element_count = stream.read_long();
79 for (unsigned long element_index = 0; element_index < element_count; ++element_index)
81 std::wstring key = stream.read_id_string();
82 read_value(key, stream);
85 if (context_->stack.size() == 1)
86 CASPAR_LOG(trace) << context_->debug_name << L":\n\n" << debug_ptree(context_->root) << L"\n";
89 void descriptor::read_value(const std::wstring& key, bigendian_file_input_stream& stream)
91 unsigned int type = stream.read_long();
97 descriptor desc(key, context_);
98 desc.populate(stream);
104 context_->stack.back()->put(key, stream.read_double());
110 context_->stack.back()->put(key, stream.read_unicode_string());
116 context_->stack.back()->put(stream.read_id_string(), stream.read_id_string());
122 context_->stack.back()->put(key, static_cast<long>(stream.read_long()));
128 context_->stack.back()->put(key, stream.read_byte());
134 context::scoped_holder list(key, context_);
135 auto count = stream.read_long();
136 for(unsigned long i = 0; i < count; ++i)
138 read_value(L"li", stream);
145 unsigned long rawdata_length = stream.read_long();
146 std::vector<char> rawdata(rawdata_length);
147 stream.read(rawdata.data(), rawdata_length);
149 std::wstring data_str(rawdata.begin(), rawdata.end());
150 context_->stack.back()->put(key, data_str);
156 context::scoped_holder list(key, context_);
157 unsigned long unit = stream.read_long();
158 std::string type(reinterpret_cast<char*>(&unit), 4);
159 std::wstring wtype(type.rbegin(), type.rend());
160 context_->stack.back()->put(wtype, stream.read_double());
170 //descriptor type not supported yet
171 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor type not supported yet"));