4 * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcsignals.h"
37 xbuf = new XMLBuffer();
40 KeyFrame::KeyFrame(EDL *edl, KeyFrames *autos)
41 : Auto(edl, (Autos*)autos)
43 xbuf = new XMLBuffer();
51 void KeyFrame::load(FileXML *file)
53 //printf("KeyFrame::load 1\n");
54 // Shouldn't be necessary
55 // position = file->tag.get_property((char*)"POSITION", position);
56 //printf("KeyFrame::load 1\n");
57 xbuf->iseek(0); xbuf->oseek(0);
58 file->read_data_until((char*)"/KEYFRAME", xbuf, 1);
61 void KeyFrame::copy(int64_t start, int64_t end, FileXML *file, int default_auto)
63 file->tag.set_title((char*)"KEYFRAME");
65 file->tag.set_property((char*)"POSITION", 0);
67 file->tag.set_property((char*)"POSITION", position - start);
68 // default_auto converts a default auto to a normal auto
69 if(is_default && !default_auto)
70 file->tag.set_property((char*)"DEFAULT", 1);
72 // Can't put newlines in because these would be reimported and resaved along
74 char *data = (char*)xbuf->cstr();
75 file->append_data(data, strlen(data));
77 file->tag.set_title((char*)"/KEYFRAME");
79 file->append_newline();
83 void KeyFrame::copy_from(Auto *that)
85 copy_from((KeyFrame*)that);
88 void KeyFrame::copy_data(KeyFrame *src)
90 xbuf->copy_from(src->xbuf);
93 void KeyFrame::copy_from(KeyFrame *that)
95 Auto::copy_from(that);
97 position = that->position;
100 int KeyFrame::identical(KeyFrame *src)
102 return !strcasecmp(xbuf->cstr(), src->xbuf->cstr());
105 void KeyFrame::get_contents(BC_Hash *ptr, char **text, char **extra)
108 input.set_shared_input(xbuf);
109 char *this_text = 0, *this_extra = 0;
110 if( !input.read_tag() ) {
111 for( int i=0; i<input.tag.properties.size(); ++i ) {
112 const char *key = input.tag.get_property_text(i);
113 const char *value = input.tag.get_property(key);
114 ptr->update(key, value);
117 // Read any text after tag
118 this_text = input.read_text();
119 (*text) = cstrdup(this_text);
121 // Read remaining data
122 this_extra = input.get_data();
123 (*extra) = cstrdup(this_extra);
127 void KeyFrame::update_parameter(BC_Hash *params,
128 const char *text, const char *extra)
131 char *this_text = 0, *this_extra = 0;
132 get_contents(&this_params, &this_text, &this_extra);
134 FileXML input, output;
135 input.set_shared_input(xbuf);
136 if( !input.read_tag() ) {
138 output.tag.set_title(input.tag.get_title());
140 // Get each parameter from this keyframe
141 for( int i=0; i<this_params.size(); ++i ) {
142 const char *key = this_params.get_key(i);
143 const char *value = this_params.get_value(i);
144 // Get new value from the params argument
146 for( int j=0; j<params->size(); ++j ) {
147 if( !strcmp(params->get_key(j), key) ) {
148 value = params->get_value(j);
154 output.tag.set_property(key, value);
157 // Get each parameter from params argument
159 for( int i=0; i<params->size(); ++i ) {
160 const char *key = params->get_key(i);
162 for( int j=0; j<this_params.size(); ++j ) {
163 if( !strcmp(this_params.get_key(j), key) ) {
169 // If it wasn't found in output, set new parameter in output.
171 output.tag.set_property(key, params->get_value(i));
177 // Write anonymous text & duplicate the rest
178 output.append_text(text ? text : this_text);
179 output.append_data(extra ? extra : this_extra);
180 output.terminate_string();
181 // Move output to input
183 xbuf->write(output.string(), output.length());
187 delete [] this_extra;
191 void KeyFrame::get_diff(KeyFrame *src,
192 BC_Hash **params, char **text, char **extra)
194 BC_Hash this_params; char *this_text = 0, *this_extra = 0;
195 BC_Hash src_params; char *src_text = 0, *src_extra = 0;
197 get_contents(&this_params, &this_text, &this_extra);
198 src->get_contents(&src_params, &src_text, &src_extra);
200 // Capture changed parameters
201 char this_value[BCTEXTLEN];
202 int m = MIN(this_params.size(), src_params.size());
203 for( int i=0; i<m; ++i ) {
204 const char *src_key = src_params.get_key(i);
205 const char *src_value = src_params.get_value(i);
207 this_params.get(src_key, this_value);
208 // Capture values which differ
209 if( strcmp(src_value, this_value) ) {
210 if(!(*params)) (*params) = new BC_Hash;
211 (*params)->update(src_key, src_value);
216 // Capture text which differs
217 if( !this_text || strcmp(this_text, src_text) )
218 (*text) = cstrdup(src_text);
220 if( !this_extra || strcmp(this_extra, src_extra) )
221 (*extra) = cstrdup(src_extra);
224 delete [] this_extra;
229 int KeyFrame::operator==(Auto &that)
231 return identical((KeyFrame*)&that);
234 int KeyFrame::operator==(KeyFrame &that)
236 return identical(&that);
239 char *KeyFrame::get_data(int64_t sz)
241 if( sz >= 0 ) xbuf->demand(sz);
245 void KeyFrame::set_data(char *data)
248 xbuf->write(data, strlen(data));
251 void KeyFrame::dump(FILE *fp)
253 fprintf(fp," position: %jd\n", position);
254 fprintf(fp," data: %s\n", xbuf->cstr());