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(const char *buf, long len)
43 xbuf = new XMLBuffer(buf, len, 0);
46 KeyFrame::KeyFrame(EDL *edl, KeyFrames *autos)
47 : Auto(edl, (Autos*)autos)
49 xbuf = new XMLBuffer();
57 void KeyFrame::load(FileXML *file)
59 //printf("KeyFrame::load 1\n");
60 // Shouldn't be necessary
61 // position = file->tag.get_property((char*)"POSITION", position);
62 //printf("KeyFrame::load 1\n");
63 xbuf->iseek(0); xbuf->oseek(0);
64 file->read_data_until((char*)"/KEYFRAME", xbuf, 1);
67 void KeyFrame::copy(int64_t start, int64_t end, FileXML *file, int default_auto)
69 file->tag.set_title((char*)"KEYFRAME");
71 file->tag.set_property((char*)"POSITION", 0);
73 file->tag.set_property((char*)"POSITION", position - start);
74 // default_auto converts a default auto to a normal auto
75 if(is_default && !default_auto)
76 file->tag.set_property((char*)"DEFAULT", 1);
78 // Can't put newlines in because these would be reimported and resaved along
80 char *data = (char*)xbuf->cstr();
81 file->append_data(data, strlen(data));
83 file->tag.set_title((char*)"/KEYFRAME");
85 file->append_newline();
89 void KeyFrame::copy_from(Auto *that)
91 copy_from((KeyFrame*)that);
94 void KeyFrame::copy_data(KeyFrame *src)
96 xbuf->copy_from(src->xbuf);
99 void KeyFrame::copy_from(KeyFrame *that)
101 Auto::copy_from(that);
103 position = that->position;
106 int KeyFrame::identical(KeyFrame *src)
108 return !strcasecmp(xbuf->cstr(), src->xbuf->cstr());
111 void KeyFrame::get_contents(BC_Hash *ptr, char **text, char **extra)
114 input.set_shared_input(xbuf);
115 char *this_text = 0, *this_extra = 0;
116 if( !input.read_tag() ) {
117 for( int i=0; i<input.tag.properties.size(); ++i ) {
118 const char *key = input.tag.get_property_text(i);
119 const char *value = input.tag.get_property(key);
120 ptr->update(key, value);
123 // Read any text after tag
124 this_text = input.read_text();
125 (*text) = cstrdup(this_text);
127 // Read remaining data
128 this_extra = input.get_data();
129 (*extra) = cstrdup(this_extra);
133 void KeyFrame::update_parameter(BC_Hash *params,
134 const char *text, const char *extra)
137 char *this_text = 0, *this_extra = 0;
138 get_contents(&this_params, &this_text, &this_extra);
140 FileXML input, output;
141 input.set_shared_input(xbuf);
142 if( !input.read_tag() ) {
144 output.tag.set_title(input.tag.get_title());
146 // Get each parameter from this keyframe
147 for( int i=0; i<this_params.size(); ++i ) {
148 const char *key = this_params.get_key(i);
149 const char *value = this_params.get_value(i);
150 // Get new value from the params argument
152 for( int j=0; j<params->size(); ++j ) {
153 if( !strcmp(params->get_key(j), key) ) {
154 value = params->get_value(j);
160 output.tag.set_property(key, value);
163 // Get each parameter from params argument
165 for( int i=0; i<params->size(); ++i ) {
166 const char *key = params->get_key(i);
168 for( int j=0; j<this_params.size(); ++j ) {
169 if( !strcmp(this_params.get_key(j), key) ) {
175 // If it wasn't found in output, set new parameter in output.
177 output.tag.set_property(key, params->get_value(i));
183 // Write anonymous text & duplicate the rest
184 output.append_text(text ? text : this_text);
185 output.append_data(extra ? extra : this_extra);
186 output.terminate_string();
187 // Move output to input
189 xbuf->write(output.string(), output.length());
193 delete [] this_extra;
197 void KeyFrame::get_diff(KeyFrame *src,
198 BC_Hash **params, char **text, char **extra)
200 BC_Hash this_params; char *this_text = 0, *this_extra = 0;
201 BC_Hash src_params; char *src_text = 0, *src_extra = 0;
203 get_contents(&this_params, &this_text, &this_extra);
204 src->get_contents(&src_params, &src_text, &src_extra);
206 // Capture changed parameters
207 char this_value[BCTEXTLEN];
208 int m = MIN(this_params.size(), src_params.size());
209 for( int i=0; i<m; ++i ) {
210 const char *src_key = src_params.get_key(i);
211 const char *src_value = src_params.get_value(i);
213 this_params.get(src_key, this_value);
214 // Capture values which differ
215 if( strcmp(src_value, this_value) ) {
216 if(!(*params)) (*params) = new BC_Hash;
217 (*params)->update(src_key, src_value);
222 // Capture text which differs
223 if( !this_text || strcmp(this_text, src_text) )
224 (*text) = cstrdup(src_text);
226 if( !this_extra || strcmp(this_extra, src_extra) )
227 (*extra) = cstrdup(src_extra);
230 delete [] this_extra;
235 int KeyFrame::operator==(Auto &that)
237 return identical((KeyFrame*)&that);
240 int KeyFrame::operator==(KeyFrame &that)
242 return identical(&that);
245 char *KeyFrame::get_data(int64_t sz)
247 if( sz >= 0 ) xbuf->demand(sz);
251 void KeyFrame::set_data(char *data)
254 xbuf->write(data, strlen(data));
257 void KeyFrame::dump(FILE *fp)
259 fprintf(fp," position: %jd\n", position);
260 fprintf(fp," data: %s\n", xbuf->cstr());