0c49981b22ff63f025deab29a4f2561de1633bad
[goodguy/history.git] / cinelerra-5.1 / cinelerra / undostack.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcsignals.h"
23 #include "bctimer.h"
24 #include "clip.h"
25 #include "cstrdup.h"
26 #include "filexml.h"
27 #include "undostack.h"
28 #include <string.h>
29
30 UndoStack::UndoStack() : List<UndoStackItem>()
31 {
32         current = 0;
33 }
34
35 UndoStack::~UndoStack()
36 {
37 }
38
39 UndoStackItem *UndoStack::get_current_undo()
40 {
41         UndoStackItem *item = current;
42         if( item &&  (number_of(item) & 1) ) item = item->previous;
43         if( item && !(number_of(item) & 1) ) item = item->previous;
44         return item;
45 }
46
47 UndoStackItem *UndoStack::get_current_redo()
48 {
49         UndoStackItem *item = current ? current : first;
50         if( item &&  (number_of(item) & 1) ) item = item->next;
51         if( item && !(number_of(item) & 1) ) item = item->next;
52         return item;
53 }
54
55
56 UndoStackItem* UndoStack::push()
57 {
58 // current is only 0 if before first undo
59         if( current )
60                 current = insert_after(current);
61         else
62                 current = insert_before(first);
63
64 // delete future undos if necessary
65         if( current && current->next ) {
66                 while( current->next ) remove(last);
67         }
68
69 // delete oldest 2 undos if necessary
70         if( total() > UNDOLEVELS ) {
71                 for( int i=0; i<2; ++i ) {
72                         UndoStackItem *second = first->next;
73                         char *temp_data = 0;
74
75
76                         if( !second->is_key() ) {
77                                 temp_data = second->get_data();
78                         }
79                         remove(first);
80
81 // Convert new first to key buffer.
82                         if( !second->is_key() ) {
83                                 second->set_data(temp_data);
84                         }
85                         delete [] temp_data;
86                 }
87         }
88
89         return current;
90 }
91
92 UndoStackItem* UndoStack::pull_next()
93 {
94 // use first entry if none
95         if( !current )
96                 current = first;
97         else
98 // use next entry if there is a next entry
99         if( current->next )
100                 current = NEXT;
101 // don't change current if there is no next entry
102         else
103                 return 0;
104
105         return current;
106 }
107
108
109 void UndoStack::dump(FILE *fp)
110 {
111         fprintf(fp,"UndoStack::dump\n");
112         UndoStackItem *current = last;
113 // Dump most recent
114         for( int i=0; current && i<32; ++i,current=PREVIOUS ) {
115                 fprintf(fp,"%c %2d %14p %-24s %8d %04jx %c\n", current->is_key() ? 'k' : ' ',
116                         i, current, current->get_description(), current->get_size(),
117                         current->get_flags(), current == this->current ? '*' : ' ');
118 //char fn[BCSTRLEN]; sprintf(fn,"/tmp/undo%d", i); FILE *fp = fopen(fn,"w");
119 //if( fp ) { char *cp=current->get_data(); fwrite(cp,strlen(cp),1,fp); fclose(fp); delete [] cp; }
120         }
121 }
122
123 // undo can be incremental or direct, from key to current
124 //#define UNDO_INCREMENTAL
125
126 class undo_record {
127 public:
128         int32_t new_ofs;
129         int32_t new_len;
130         int32_t old_len;
131 };
132
133 static unsigned char* apply_difference(unsigned char *before, int before_len,
134                 unsigned char *patch, int patch_len, int *result_len)
135 {
136         XMLBuffer xbfr((const char *)patch, patch_len, 0);
137         int32_t len = 0;
138         xbfr.read((char *)&len, sizeof(len));
139         char *result = new char[len];
140         char *rp = result, *bp = (char*)before;
141
142         while( xbfr.itell() < patch_len ) {
143                 undo_record urecd;
144                 xbfr.read((char*)&urecd, sizeof(urecd));
145                 int ofs = rp - result;
146                 if( urecd.new_ofs > ofs ) {
147                         int sz = urecd.new_ofs - ofs;
148                         memcpy(rp, bp, sz);
149                         bp += sz;  rp += sz;
150                 }
151                 if( urecd.new_len > 0 )
152                         xbfr.read(rp, urecd.new_len);
153                 bp += urecd.old_len;  rp += urecd.new_len;
154         }
155
156 // All data from before_ptr to end of result buffer is identical
157         int sz = bp - (char *)before;
158         if( (before_len-=sz) > 0 ) {
159                 memcpy(rp, bp, before_len);
160                 rp += before_len;
161         }
162
163         *result_len = rp - result;
164         return (unsigned char *)result;
165 }
166
167 UndoStackItem::UndoStackItem()
168  : ListItem<UndoStackItem>()
169 {
170         description = data = 0;
171         data_size = 0;
172         key = 0;
173         creator = 0;
174         session_filename = 0;
175 }
176
177 UndoStackItem::~UndoStackItem()
178 {
179         delete [] description;
180         delete [] data;
181         delete [] session_filename;
182 }
183
184 void UndoStackItem::set_description(char *description)
185 {
186         delete [] this->description;
187         this->description= 0;
188         this->description = new char[strlen(description) + 1];
189         strcpy(this->description, description);
190 }
191
192 void UndoStackItem::set_filename(char *filename)
193 {
194         delete [] session_filename;
195         this->session_filename = cstrdup(filename);
196 }
197
198 char* UndoStackItem::get_filename()
199 {
200         return session_filename;
201 }
202
203
204 const char* UndoStackItem::get_description()
205 {
206         if( description )
207                 return description;
208         else
209                 return "";
210 }
211
212 // undo diff
213
214 UndoHash::UndoHash(char *txt, int len, UndoHash *nxt)
215 {
216         this->txt = txt;
217         this->len = len;
218         this->nxt = nxt;
219         line[va] = line[vb] = -1;
220         occurs[va] = occurs[vb] = 0;
221 }
222 UndoHash::~UndoHash()
223 {
224 }
225
226 UndoHashTable::UndoHashTable()
227 {
228         for( int i=0; i<hash_sz; ++i ) table[i] = 0;
229         bof = new UndoHash(0, 0, 0);
230         eof = new UndoHash(0, 0, 0);
231 }
232 UndoHashTable::~UndoHashTable()
233 {
234         delete bof;
235         delete eof;
236         for( int i=0; i<hash_sz; ++i ) {
237                 for( UndoHash *nxt=0, *hp=table[i]; hp; hp=nxt ) {
238                         nxt = hp->nxt;  delete hp;
239                 }
240         }
241 }
242
243 UndoHash *UndoHashTable::add(char *txt, int len)
244 {
245         uint8_t *bp = (uint8_t *)txt;
246         int v = 0;
247         for( int i=len; --i>=0; ++bp ) {
248                 v = (v<<1) + *bp;
249                 v = (v + (v>>hash_sz2)) & hash_sz1;
250         }
251         UndoHash *hp = table[v];
252         while( hp && strncmp(hp->txt,txt,len) ) hp = hp->nxt;
253         if( !hp ) {
254                 hp = new UndoHash(txt, len, table[v]);
255                 table[v] = hp;
256         }
257         return hp;
258 }
259
260 UndoLine::UndoLine(UndoHash *hash)
261 {
262         this->txt = 0;  this->len = 0;
263         this->hash = hash;
264         hash->occurs[va] = hash->occurs[vb] = 1;
265 }
266
267 UndoLine::UndoLine(UndoHashTable *hash, char *txt, int len)
268 {
269         this->txt = txt;  this->len = len;
270         this->hash = hash->add(txt, len);
271 }
272 UndoLine::~UndoLine()
273 {
274 }
275
276 int UndoLine::eq(UndoLine *ln)
277 {
278         return hash == ln->hash ? 1 : 0;
279 }
280
281 void UndoVersion::scan_lines(UndoHashTable *hash, char *sp, char *ep)
282 {
283         append(new UndoLine(hash->bof));
284         for( int line=1; sp<ep; ++line ) {
285                 char *txt = sp;
286                 while( sp<ep && *sp++ != '\n' );
287                 UndoLine *ln = new UndoLine(hash, txt, sp-txt);
288                 ln->hash->line[ver] = line;
289                 ++ln->hash->occurs[ver];
290                 append(ln);
291         }
292         append(new UndoLine(hash->eof));
293 }
294
295
296 void UndoStackItem::set_data(char *data)
297 {
298         delete [] this->data;  this->data = 0;
299         this->data_size = 0;   this->key = 0;
300         int data_size = strlen(data)+1;
301
302         UndoStackItem *current = this;
303 // Search for key buffer within interval
304         for( int i=UNDO_KEY_INTERVAL; --i>=0 && current && !current->key; current=PREVIOUS );
305
306         int need_key = current ? 0 : 1;
307 // setting need_key = 1 forces all undo data items to key (not diffd)
308 //      int need_key = 1;
309         char *prev = 0;  int prev_size = 0;
310         if( !need_key ) {
311 #ifdef UNDO_INCREMENTAL
312                 prev = previous->get_data();
313                 prev_size = prev ? strlen(prev)+1 : 0;
314 #else
315                 prev = current->data;
316                 prev_size = current->data_size;
317 #endif
318                 int min_size = data_size/16;
319 // if the previous is a sizable incremental diff
320                 if( previous && !previous->key &&
321                     previous->data_size > min_size ) {
322 //  and this minimum diff distance is a lot different, need key
323                         int dist = abs(data_size - prev_size);
324                         if( abs(dist - previous->data_size) > min_size )
325                                 need_key = 1;
326                 }
327         }
328         if( !need_key ) {
329                 int bfr_size = BCTEXTLEN;
330                 char *bfr = new char[bfr_size];
331                 XMLBuffer xbfr(bfr_size, bfr, 1);
332                 xbfr.write((char*)&data_size, sizeof(data_size));
333                 UndoHashTable hash;
334                 UndoVersion alines(va), blines(vb);
335                 char *asp = data, *aep = asp + data_size-1;
336                 char *bsp = prev, *bep = bsp + prev_size-1;
337                 alines.scan_lines(&hash, asp, aep);
338                 blines.scan_lines(&hash, bsp, bep);
339 // trim suffix
340                 int asz = alines.size(), bsz = blines.size();
341                 while( asz > 0 && bsz > 0 && alines[asz-1]->eq(blines[bsz-1]) ) {
342                         --asz;  --bsz;
343                 }
344
345                 int ai = 0, bi = 0;
346                 while( ai < asz || bi < bsz ) {
347 // skip running match
348                         if( alines[ai]->eq(blines[bi]) ) { ++ai;  ++bi;  continue; }
349                         char *adp = alines[ai]->txt, *bdp = blines[bi]->txt;
350 // find best unique match
351                         int ma = asz, mb = bsz;
352                         int mx = ma + mb + 1;
353                         for( int ia=ai; ia<asz && ia-ai<mx; ++ia ) {
354                                 UndoHash *ah = alines[ia]->hash;
355                                 if( ah->occurs[va] != 1 || ah->occurs[vb] != 1 ) continue;
356                                 int m = (ah->line[va] - ai) + (ah->line[vb] - bi);
357                                 if( m >= mx ) continue;
358                                 ma = ah->line[va];  mb = ah->line[vb];  mx = m;
359                         }
360 // trim suffix
361                         while( ma > 0 && mb > 0 && alines[ma-1]->eq(blines[mb-1]) ) { --ma;  --mb; }
362                         char *ap = alines[ma]->txt, *bp = blines[mb]->txt;
363                         ai = ma;  bi = mb;
364                         undo_record urecd;
365                         urecd.new_ofs = adp - data;
366                         urecd.new_len = ap - adp;
367                         urecd.old_len = bp - bdp;
368                         xbfr.write((const char*)&urecd, sizeof(urecd));
369                         xbfr.write((const char*)adp, urecd.new_len);
370                 }
371
372                 int64_t pos = xbfr.otell();
373                 if( abs(data_size-prev_size) < pos/2 ) {
374                         this->data = new char[this->data_size = pos];
375                         xbfr.iseek(0);
376                         xbfr.read(this->data, pos);
377 #if 1
378                         int test_size = 0;
379                         char *test_data = (char*)apply_difference(
380                                 (unsigned char *)prev, prev_size,
381                                 (unsigned char *)this->data, this->data_size,
382                                 &test_size);
383                         if( test_size != data_size || memcmp(test_data, data, test_size) ) {
384                                 printf("UndoStackItem::set_data: *** incremental undo failed!\n");
385                                 delete [] this->data;  this->data = 0;  this->data_size = 0;
386                                 need_key = 1;
387                         }
388                         delete [] test_data;
389 #endif
390                 }
391                 else
392                         need_key = 1;
393         }
394
395 #ifdef UNDO_INCREMENTAL
396         delete [] prev;
397 #endif
398         if( need_key ) {
399                 this->key = 1;
400                 this->data = new char[this->data_size = data_size];
401                 memcpy(this->data, data, this->data_size);
402         }
403 }
404
405 char* UndoStackItem::get_data()
406 {
407 // Find latest key buffer
408         UndoStackItem *current = this;
409         while( current && !current->key )
410                 current = PREVIOUS;
411         if( !current ) {
412                 printf("UndoStackItem::get_data: no key buffer found!\n");
413                 return 0;
414         }
415
416 // This is the key buffer
417         if( current == this ) {
418                 char *result = new char[data_size];
419                 memcpy(result, data, data_size);
420                 return result;
421         }
422 #ifdef UNDO_INCREMENTAL
423 // Get key buffer
424         char *current_data = current->get_data();
425         int current_size = current->data_size;
426         current = NEXT;
427         while( current ) {
428 // Do incremental updates
429                 int new_size;
430                 char *new_data = (char*)apply_difference(
431                         (unsigned char*)current_data, current_size,
432                         (unsigned char*)current->data, current->data_size,
433                         &new_size);
434                 delete [] current_data;
435
436                 if( current == this )
437                         return new_data;
438                 current_data = new_data;
439                 current_size = new_size;
440                 current = NEXT;
441         }
442 // Never hit this object.
443         delete [] current_data;
444         printf("UndoStackItem::get_data: lost starting object!\n");
445         return 0;
446 #else
447         int new_size;
448         char *new_data = (char*)apply_difference(
449                 (unsigned char*)current->data, current->data_size,
450                 (unsigned char*)this->data, this->data_size, &new_size);
451         return new_data;
452 #endif
453 }
454
455 int UndoStackItem::is_key()
456 {
457         return key;
458 }
459
460 int UndoStackItem::get_size()
461 {
462         return data_size;
463 }
464
465 void UndoStackItem::set_flags(uint64_t flags)
466 {
467         this->load_flags = flags;
468 }
469
470 uint64_t UndoStackItem::get_flags()
471 {
472         return load_flags;
473 }
474
475 void UndoStackItem::set_creator(void *creator)
476 {
477         this->creator = creator;
478 }
479
480 void* UndoStackItem::get_creator()
481 {
482         return creator;
483 }
484