]> git.sesse.net Git - casparcg/blob - modules/psd/descriptor.cpp
* Fixed code standard issues in PSD producer.
[casparcg] / modules / psd / descriptor.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: Niklas P Andersson, niklas.p.andersson@svt.se
20 */
21
22 #include "descriptor.h"
23 #include "misc.h"
24
25 #include <boost/property_tree/ptree.hpp>
26
27 #include <memory>
28
29 namespace caspar { namespace psd {
30
31         class descriptor::context::scoped_holder
32         {
33                 descriptor::context::ptr_type ctx_;
34         public:
35                 explicit scoped_holder(const std::wstring& key, descriptor::context::ptr_type ctx) : ctx_(ctx) 
36                 {
37                         Ptree *parent = ctx_->stack.back();
38                         Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
39                         ctx_->stack.push_back(child);
40                 }
41                 ~scoped_holder()
42                 {
43                         ctx_->stack.pop_back();
44                 }
45         };
46
47         descriptor::descriptor() : context_(std::make_shared<context>())
48         {
49                 context_->stack.push_back(&context_->root);
50         }
51         descriptor::descriptor(const std::wstring& key, context::ptr_type context) : context_(context)
52         {
53                 Ptree *parent = context_->stack.back();
54                 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
55                 context->stack.push_back(child);
56         }
57         descriptor::~descriptor()
58         {
59                 context_->stack.pop_back();
60         }
61
62 void descriptor::populate(bigendian_file_input_stream& stream)
63 {
64         stream.read_unicode_string();
65         stream.read_id_string();
66         unsigned long element_count = stream.read_long();
67         for (unsigned long element_index = 0; element_index < element_count; ++element_index)
68         {
69                 std::wstring key = stream.read_id_string();
70                 read_value(key, stream);
71         }
72 }
73
74 void descriptor::read_value(const std::wstring& key, bigendian_file_input_stream& stream)
75 {
76         unsigned int type = stream.read_long();
77
78         switch(type)
79         {
80         case 'Objc': 
81                 {
82                         descriptor desc(key, context_);
83                         desc.populate(stream);
84                 }
85                 break;
86
87         case 'doub': 
88                 {
89                         context_->stack.back()->put(key, stream.read_double());
90                 }
91                 break;
92
93         case 'TEXT':
94                 {
95                         context_->stack.back()->put(key, stream.read_unicode_string());
96                 }
97                 break;
98
99         case 'enum':
100                 {
101                         context_->stack.back()->put(stream.read_id_string(), stream.read_id_string());
102                 }
103                 break;
104
105         case 'long':
106                 {
107                         context_->stack.back()->put(key, static_cast<long>(stream.read_long()));
108                 }
109                 break;
110
111         case 'bool': 
112                 {
113                         context_->stack.back()->put(key, stream.read_byte());
114                 }
115                 break;
116
117         case 'VlLs': 
118                 {
119                         context::scoped_holder list(key, context_);
120                         auto count = stream.read_long();
121                         for(unsigned long i = 0; i < count; ++i)
122                         {
123                                 read_value(L"li", stream);
124                         }
125                 }
126                 break;
127
128         case 'tdta':
129                 {
130                         unsigned long rawdata_length = stream.read_long();
131                         std::vector<char> rawdata(rawdata_length);
132                         stream.read(rawdata.data(), rawdata_length);
133
134                         std::wstring data_str(rawdata.begin(), rawdata.end());
135                         context_->stack.back()->put(key, data_str);
136                 }
137                 break;
138
139         case 'UntF': 
140                 {
141                         context::scoped_holder list(key, context_);
142                         unsigned long unit = stream.read_long();
143                         std::string type(reinterpret_cast<char*>(&unit), 4);
144                         std::wstring wtype(type.rbegin(), type.rend());
145                         context_->stack.back()->put(wtype, stream.read_double());
146                 }
147                 break;
148
149         case 'obj ': 
150         case 'GlbO': 
151         case 'type':
152         case 'GlbC':
153         case 'alis':
154         default:
155                 //descriptor type not supported yet
156                 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor type not supported yet"));
157         }
158 }
159
160 }       //namespace psd
161 }       //namespace caspar