ibeam column select tweaks, fix title alpha fader value popup, title bar border color...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / filexml.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 <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "bcsignals.h"
28 #include "arraylist.h"
29 #include "cstrdup.h"
30 #include "filexml.h"
31 #include "mainerror.h"
32
33 // messes up cutads link
34 #undef eprintf
35 #define eprintf printf
36
37 static const char left_delm = '<', right_delm = '>';
38 const char *FileXML::xml_header = "<?xml version=\"1.0\"?>\n";
39 const int FileXML::xml_header_size = strlen(xml_header);
40
41 XMLBuffer::XMLBuffer(long buf_size, long max_size, int del)
42 {
43         bsz = buf_size;
44         bfr = new unsigned char[bsz];
45         inp = outp = bfr;
46         lmt = bfr + bsz;
47         isz = max_size;
48         destroy = del;
49         share_lock = new Mutex("XMLBuffer::share_lock", 1);
50 }
51
52 XMLBuffer::XMLBuffer(const char *buf, long buf_size, int del)
53 {       // reading
54         bfr = (unsigned char *)buf;
55         bsz = buf_size;
56         outp = bfr;
57         lmt = inp = bfr+bsz;
58         isz = bsz;
59         destroy = del;
60         share_lock = new Mutex("XMLBuffer::share_lock");
61 }
62
63 XMLBuffer::XMLBuffer(long buf_size, char *buf, int del)
64 {       // writing
65         bfr = (unsigned char *)buf;
66         bsz = buf_size;
67         inp = bfr;
68         lmt = outp = bfr+bsz;
69         isz = bsz;
70         destroy = del;
71         share_lock = new Mutex("XMLBuffer::share_lock");
72 }
73
74 XMLBuffer::~XMLBuffer()
75 {
76         if( destroy ) delete [] bfr;
77         delete share_lock;
78 }
79
80 int XMLBuffer::demand(long len)
81 {
82         if( len > bsz ) {
83                 if( !destroy ) return 0;
84                 long sz = inp-bfr;
85                 len += sz/2 + BCTEXTLEN;
86                 unsigned char *np = new unsigned char[len];
87                 if( sz > 0 ) memcpy(np,bfr,sz);
88                 inp = np + (inp-bfr);
89                 outp = np + (outp-bfr);
90                 lmt = np + len;  bsz = len;
91                 delete [] bfr;   bfr = np;
92         }
93         return 1;
94 }
95
96 int XMLBuffer::write(const char *bp, int len)
97 {
98         if( len <= 0 ) return 0;
99         if( !destroy && lmt-inp < len ) len = lmt-inp;
100         demand(otell()+len);
101         memmove(inp,bp,len);
102         inp += len;
103         return len;
104 }
105
106 int XMLBuffer::read(char *bp, int len)
107 {
108         long size = inp - outp;
109         if( size <= 0 && len > 0 ) return -1;
110         if( len > size ) len = size;
111         memmove(bp,outp,len);
112         outp += len;
113         return len;
114 }
115
116 void XMLBuffer::copy_from(XMLBuffer *xbuf)
117 {
118         xbuf->share_lock->lock("XMLBuffer::copy_from");
119         share_lock->lock("XMLBuffer::copy_from");
120         oseek(0);
121         write((const char*)xbuf->pos(0), xbuf->otell());
122         iseek(xbuf->itell());
123         xbuf->share_lock->unlock();
124         share_lock->unlock();
125 }
126
127
128 // Precision in base 10
129 // for float is 6 significant figures
130 // for double is 16 significant figures
131
132 XMLTag::Property::Property(const char *pp, const char *vp)
133 {
134 //printf("Property %s = %s\n",pp, vp);
135         prop = cstrdup(pp);
136         value = cstrdup(vp);
137 }
138
139 XMLTag::Property::~Property()
140 {
141         delete [] prop;
142         delete [] value;
143 }
144
145
146 XMLTag::XMLTag()
147 {
148         string = 0;
149         avail = used = 0;
150 }
151
152 XMLTag::~XMLTag()
153 {
154         properties.remove_all_objects();
155         delete [] string;
156 }
157
158 const char *XMLTag::get_property(const char *property, char *value)
159 {
160         int i = properties.size();
161         while( --i >= 0 && strcasecmp(properties[i]->prop, property) );
162         if( i >= 0 )
163                 strcpy(value, properties[i]->value);
164         else
165                 *value = 0;
166         return value;
167 }
168
169 //getters
170 const char *XMLTag::get_property_text(int i) {
171         return i < properties.size() ? properties[i]->prop : "";
172 }
173 int XMLTag::get_property_int(int i) {
174         return i < properties.size() ? atol(properties[i]->value) : 0;
175 }
176 float XMLTag::get_property_float(int i) {
177         return i < properties.size() ? atof(properties[i]->value) : 0.;
178 }
179 const char* XMLTag::get_property(const char *prop) {
180         for( int i=0; i<properties.size(); ++i ) {
181                 if( !strcasecmp(properties[i]->prop, prop) )
182                         return properties[i]->value;
183         }
184         return 0;
185 }
186 int32_t XMLTag::get_property(const char *prop, int32_t dflt) {
187         const char *cp = get_property(prop);
188         return !cp ? dflt : atol(cp);
189 }
190 int64_t XMLTag::get_property(const char *prop, int64_t dflt) {
191         const char *cp = get_property(prop);
192         return !cp ? dflt : strtoll(cp,0,0);
193 }
194 float XMLTag::get_property(const char *prop, float dflt) {
195         const char *cp = get_property(prop);
196         return !cp ? dflt : atof(cp);
197 }
198 double XMLTag::get_property(const char *prop, double dflt) {
199         const char *cp = get_property(prop);
200         return !cp ? dflt : atof(cp);
201 }
202
203 //setters
204 int XMLTag::set_title(const char *text) {
205         strcpy(title, text);
206         return 0;
207 }
208 int XMLTag::set_property(const char *text, const char *value) {
209         properties.append(new XMLTag::Property(text, value));
210         return 0;
211 }
212 int XMLTag::set_property(const char *text, int32_t value)
213 {
214         char text_value[BCSTRLEN];
215         sprintf(text_value, "%d", value);
216         set_property(text, text_value);
217         return 0;
218 }
219 int XMLTag::set_property(const char *text, int64_t value)
220 {
221         char text_value[BCSTRLEN];
222         sprintf(text_value, "%jd", value);
223         set_property(text, text_value);
224         return 0;
225 }
226 int XMLTag::set_property(const char *text, float value)
227 {
228         char text_value[BCSTRLEN];
229         if (value - (float)((int64_t)value) == 0)
230                 sprintf(text_value, "%jd", (int64_t)value);
231         else
232                 sprintf(text_value, "%.6e", value);
233         set_property(text, text_value);
234         return 0;
235 }
236 int XMLTag::set_property(const char *text, double value)
237 {
238         char text_value[BCSTRLEN];
239         if (value - (double)((int64_t)value) == 0)
240                 sprintf(text_value, "%jd", (int64_t)value);
241         else
242                 sprintf(text_value, "%.16e", value);
243         set_property(text, text_value);
244         return 0;
245 }
246
247
248 int XMLTag::reset_tag()
249 {
250         used = 0;
251         properties.remove_all_objects();
252         return 0;
253 }
254
255 int XMLTag::write_tag(FileXML *xml)
256 {
257         XMLBuffer *buf = xml->buffer;
258 // title header
259         buf->next(left_delm);
260         buf->write(title, strlen(title));
261
262 // properties
263         for( int i=0; i<properties.size(); ++i ) {
264                 const char *prop = properties[i]->prop;
265                 const char *value = properties[i]->value;
266                 int plen = strlen(prop), vlen = strlen(value);
267                 bool need_quotes = !vlen || strchr(value,' ');
268                 buf->next(' ');
269                 xml->append_text(prop, plen);
270                 buf->next('=');
271                 if( need_quotes ) buf->next('\"');
272                 xml->append_text(value, vlen);
273                 if( need_quotes ) buf->next('\"');
274         }
275
276         buf->next(right_delm);
277         return 0;
278 }
279
280 #define ERETURN(s) do { \
281   printf("XMLTag::read_tag:%d tag \"%s\"%s\n",__LINE__,title,s);\
282   return 1; } while(0)
283 #define EOB_RETURN(s) ERETURN(", end of buffer");
284
285 int XMLTag::read_tag(FileXML *xml)
286 {
287         XMLBuffer *buf = xml->buffer;
288         int len, term;
289         long prop_start, prop_end;
290         long value_start, value_end;
291         long ttl;
292         int ch = buf->next();
293         title[0] = 0;
294 // skip ws
295         while( ch>=0 && ws(ch) ) ch = buf->next();
296         if( ch < 0 ) EOB_RETURN();
297
298 // read title
299         ttl = buf->itell() - 1;
300         for( int i=0; i<MAX_TITLE && ch>=0; ++i, ch=buf->next() ) {
301                 if( ch == right_delm || ch == '=' || ws(ch) ) break;
302         }
303         if( ch < 0 ) EOB_RETURN();
304         len = buf->itell()-1 - ttl;
305         if( len >= MAX_TITLE ) ERETURN(", title too long");
306 // if title
307         if( ch != '=' ) {
308                 memmove(title, buf->pos(ttl), len);
309                 title[len] = 0;
310         }
311 // no title but first property.
312         else {
313                 title[0] = 0;
314                 buf->iseek(ttl);
315                 ch = buf->next();
316         }
317 // read properties
318         while( ch >= 0 && ch != right_delm ) {
319 // find tag start, skip header leadin
320                 while( ch >= 0 && (ch==left_delm || ws(ch)) )
321                         ch = buf->next();
322 // find end of property name
323                 prop_start = buf->itell()-1;
324                 while( ch >= 0 && (ch!=right_delm && ch!='=' && !ws(ch)) )
325                         ch = buf->next();
326                 if( ch < 0 ) EOB_RETURN();
327                 prop_end = buf->itell()-1;
328 // skip ws = ws
329                 while( ch >= 0 && ws(ch) )
330                         ch = buf->next();
331                 if( ch == '=' ) ch = buf->next();
332                 while( ch >= 0 && ws(ch) )
333                         ch = buf->next();
334                 if( ch < 0 ) EOB_RETURN();
335 // set terminating char
336                 if( ch == '\"' ) {
337                         term = ch;
338                         ch = buf->next();
339                 }
340                 else
341                         term = ' ';
342                 value_start = buf->itell()-1;
343                 while( ch >= 0 ) {
344 // old edl bug work-around, allow nl in quoted string
345                         if( ch==term || ch==right_delm ) break;
346                         if( ch=='\n' && term!='\"' ) break;
347                         ch = buf->next();
348                 }
349                 if( ch < 0 ) EOB_RETURN();
350                 value_end = buf->itell()-1;
351 // add property
352                 int plen = prop_end-prop_start;
353                 if( !plen ) continue;
354                 int vlen = value_end-value_start;
355                 char prop[plen+1], value[vlen+1];
356                 const char *coded_prop = (const char *)buf->pos(prop_start);
357                 const char *coded_value = (const char *)buf->pos(value_start);
358 // props should not have coded text
359                 memcpy(prop, coded_prop, plen);
360                 prop[plen] = 0;
361                 xml->decode(value, coded_value, vlen);
362                 if( prop_end > prop_start ) {
363                         Property *property = new Property(prop, value);
364                         properties.append(property);
365                 }
366 // skip the terminating char
367                 if( ch != right_delm ) ch = buf->next();
368         }
369         if( !properties.size() && !title[0] ) ERETURN(", emtpy");
370         return 0;
371 }
372
373
374
375 FileXML::FileXML(int coded)
376 {
377         output = 0;
378         output_length = 0;
379         buffer = new XMLBuffer();
380         set_coding(coded);
381         shared = 0;
382 }
383
384 FileXML::~FileXML()
385 {
386         if( !shared ) delete buffer;
387         else buffer->share_lock->unlock();
388         delete [] output;
389 }
390
391
392 int FileXML::terminate_string()
393 {
394         append_data("", 1);
395         return 0;
396 }
397
398 int FileXML::rewind()
399 {
400         terminate_string();
401         buffer->iseek(0);
402         return 0;
403 }
404
405
406 int FileXML::append_newline()
407 {
408         append_data("\n", 1);
409         return 0;
410 }
411
412 int FileXML::append_tag()
413 {
414         tag.write_tag(this);
415         append_text(tag.string, tag.used);
416         tag.reset_tag();
417         return 0;
418 }
419
420 int FileXML::append_text(const char *text)
421 {
422         if( text != 0 )
423                 append_text(text, strlen(text));
424         return 0;
425 }
426
427 int FileXML::append_data(const char *text)
428 {
429         if( text != 0 )
430                 append_data(text, strlen(text));
431         return 0;
432 }
433
434 int FileXML::append_data(const char *text, long len)
435 {
436         if( text != 0 && len > 0 )
437                 buffer->write(text, len);
438         return 0;
439 }
440
441 int FileXML::append_text(const char *text, long len)
442 {
443         if( text != 0 && len > 0 ) {
444                 int size = coded_length(text, len);
445                 char coded_text[size+1];
446                 encode(coded_text, text, len);
447                 buffer->write(coded_text, size);
448         }
449         return 0;
450 }
451
452
453 char* FileXML::get_data()
454 {
455         long ofs = buffer->itell();
456         return (char *)buffer->pos(ofs);
457 }
458 char* FileXML::string()
459 {
460         return (char *)buffer->cstr();
461 }
462
463 long FileXML::length()
464 {
465         return buffer->otell();
466 }
467
468 char* FileXML::read_text()
469 {
470         int ch = buffer->next();
471 // filter out first char is new line
472         if( ch == '\n' ) ch = buffer->next();
473         long ipos = buffer->itell()-1;
474 // scan for delimiter
475         while( ch >= 0 && ch != left_delm ) ch = buffer->next();
476         long pos = buffer->itell()-1;
477         if( ch >= 0 ) buffer->iseek(pos);
478         long len = pos - ipos;
479         if( len >= output_length ) {
480                 delete [] output;
481                 output_length = len+1;
482                 output = new char[output_length];
483         }
484         decode(output,(const char *)buffer->pos(ipos), len);
485         return output;
486 }
487
488 int FileXML::read_tag()
489 {
490         int ch = buffer->next();
491 // scan to next tag
492         while( ch >= 0 && ch != left_delm ) ch = buffer->next();
493         if( ch < 0 ) return 1;
494         tag.reset_tag();
495         return tag.read_tag(this);
496 }
497
498 int FileXML::skip_tag()
499 {
500         char tag_title[sizeof(tag.title)];
501         strcpy(tag_title, tag.title);
502         int n = 1;
503         while( !read_tag() ) {
504                 if( tag.title[0] == tag_title[0] ) {
505                         if( !strcasecmp(&tag_title[1], &tag.title[1]) ) ++n;
506                 }
507                 else if( tag.title[0] != '/' ) continue;
508                 else if( strcasecmp(&tag_title[0], &tag.title[1]) ) continue;
509                 else if( --n <= 0 ) return 0;
510         }
511         return 1;
512 }
513
514 int FileXML::read_data_until(const char *tag_end, XMLBuffer *xbuf, int skip)
515 {
516         long ipos = buffer->itell();
517         int pos = -1;
518         for( int ch=buffer->next(); ch>=0; ch=buffer->next() ) {
519                 if( pos < 0 ) { // looking for next tag
520                         if( ch == left_delm ) {
521                                 ipos = buffer->itell()-1;
522                                 pos = 0;
523                         }
524                         else
525                                 xbuf->next(ch);
526                         continue;
527                 }
528                 // check for end of match
529                 if( !tag_end[pos] && ch == right_delm ) break;
530                 // if mismatched, copy prefix to out
531                 if( tag_end[pos] != ch ) {
532                         xbuf->next(left_delm);
533                         for( int i=0; i<pos; ++i )
534                                 xbuf->next(tag_end[i]);
535                         pos = -1;
536                         xbuf->next(ch);
537                         continue;
538                 }
539                 ++pos;
540         }
541 // if end tag is reached, pos is left on the < of the end tag
542         if( !skip && pos >= 0 && !tag_end[pos] )
543                 buffer->iseek(ipos);
544         return xbuf->otell();
545 }
546
547 int FileXML::read_text_until(const char *tag_end, XMLBuffer *xbuf, int skip)
548 {
549         int len = read_data_until(tag_end, xbuf, skip);
550         char *cp = xbuf->cstr();
551         decode(cp, cp, len);
552         return 0;
553 }
554
555 int FileXML::write_to_file(const char *filename)
556 {
557         FILE *out = fopen(filename, "wb");
558         if( !out ) {
559                 eprintf("write_to_file %d \"%s\": %m\n", __LINE__, filename);
560                 return 1;
561         }
562         int ret = write_to_file(out, filename);
563         fclose(out);
564         return ret;
565 }
566
567 int FileXML::write_to_file(FILE *file, const char *filename)
568 {
569         strcpy(this->filename, filename);
570         const char *str = string();
571         long len = strlen(str);
572 // Position may have been rewound after storing
573         if( !fwrite(xml_header, xml_header_size, 1, file) ||
574             ( len > 0 && !fwrite(str, len, 1, file) ) ) {
575                 eprintf("\"%s\": %m\n", filename);
576                 return 1;
577         }
578         return 0;
579 }
580
581 int FileXML::read_from_file(const char *filename, int ignore_error)
582 {
583
584         strcpy(this->filename, filename);
585         FILE *in = fopen(filename, "rb");
586         if( !in ) {
587                 if(!ignore_error)
588                         eprintf("\"%s\" %m\n", filename);
589                 return 1;
590         }
591         fseek(in, 0, SEEK_END);
592         long length = ftell(in);
593         fseek(in, 0, SEEK_SET);
594         char *fbfr = new char[length+1];
595         delete buffer;
596         (void)fread(fbfr, length, 1, in);
597         fbfr[length] = 0;
598         buffer = new XMLBuffer(fbfr, length, 1);
599         fclose(in);
600         return 0;
601 }
602
603 int FileXML::read_from_string(char *string)
604 {
605         strcpy(this->filename, "");
606         long length = strlen(string);
607         char *sbfr = new char[length+1];
608         strcpy(sbfr, string);
609         delete buffer;
610         buffer = new XMLBuffer(sbfr, length, 1);
611         return 0;
612 }
613
614 void FileXML::set_coding(int coding)
615 {
616         coded = coding;
617         if( coded ) {
618                 decode = XMLBuffer::decode_data;
619                 encode = XMLBuffer::encode_data;
620                 coded_length = XMLBuffer::encoded_length;
621         }
622         else {
623                 decode = XMLBuffer::copy_data;
624                 encode = XMLBuffer::copy_data;
625                 coded_length = XMLBuffer::copy_length;
626         }
627 }
628
629 int FileXML::get_coding()
630 {
631         return coded;
632 }
633
634 int FileXML::set_shared_input(XMLBuffer *xbuf)
635 {
636         strcpy(this->filename, "");
637         delete buffer;
638         buffer = xbuf;
639         xbuf->share_lock->lock("FileXML::set_shared_input");
640         xbuf->iseek(0);
641         shared = 1;
642         set_coding(coded);
643         return 0;
644 }
645
646 int FileXML::set_shared_output(XMLBuffer *xbuf)
647 {
648         strcpy(this->filename, "");
649         delete buffer;
650         buffer = xbuf;
651         xbuf->share_lock->lock("FileXML::set_shared_output");
652         xbuf->oseek(0);
653         shared = 1;
654         set_coding(coded);
655         return 0;
656 }
657
658
659 // ================================ XML tag
660
661 int XMLTag::title_is(const char *tp)
662 {
663         return !strcasecmp(title, tp) ? 1 : 0;
664 }
665
666 char* XMLTag::get_title()
667 {
668         return title;
669 }
670
671 int XMLTag::get_title(char *value)
672 {
673         if( title[0] != 0 ) strcpy(value, title);
674         return 0;
675 }
676
677 int XMLTag::test_property(char *property, char *value)
678 {
679         for( int i=0; i<properties.size(); ++i ) {
680                 if( !strcasecmp(properties[i]->prop, property) &&
681                     !strcasecmp(properties[i]->value, value) )
682                         return 1;
683         }
684         return 0;
685 }
686
687 static inline int xml_cmp(const char *np, const char *sp)
688 {
689    const char *tp = ++np;
690    while( *np ) { if( *np != *sp ) return 0;  ++np; ++sp; }
691    return np - tp;
692 }
693
694 char *XMLBuffer::decode_data(char *bp, const char *sp, int n)
695 {
696    char *ret = bp;
697    if( n < 0 ) n = strlen(sp);
698    const char *ep = sp + n;
699    while( sp < ep ) {
700       if( (n=*sp++) != '&' ) { *bp++ = n; continue; }
701       switch( (n=*sp++) ) {
702       case 'a': // &amp;
703          if( (n=xml_cmp("amp;", sp)) ) { *bp++ = '&';  sp += n;  continue; }
704          break;
705       case 'g': // &gt;
706          if( (n=xml_cmp("gt;", sp)) ) { *bp++ = '>';  sp += n;  continue; }
707          break;
708       case 'l': // &lt;
709          if( (n=xml_cmp("lt;", sp)) ) { *bp++ = '<';  sp += n;  continue; }
710          break;
711       case 'q': // &quot;
712          if( (n=xml_cmp("quot;", sp)) ) { *bp++ = '"';  sp += n;  continue; }
713          break;
714       case '#': { // &#<num>;
715          char *cp = 0;  int v = strtoul(sp,&cp,10);
716          if( *cp == ';' ) { *bp++ = (char)v;  sp = cp+1;  continue; }
717          n = cp - sp; }
718          break;
719       default:
720          *bp++ = '&';
721          *bp++ = (char)n;
722          continue;
723       }
724       sp -= 2;  n += 2;
725       while( --n >= 0 ) *bp++ = *sp++;
726    }
727    *bp = 0;
728    return ret;
729 }
730
731
732 char *XMLBuffer::encode_data(char *bp, const char *sp, int n)
733 {
734         char *ret = bp;
735         if( n < 0 ) n = strlen(sp);
736         const char *cp, *ep = sp + n;
737         while( sp < ep ) {
738                 int ch = *sp++;
739                 switch( ch ) {
740                 case '<':  cp = "&lt;";    break;
741                 case '>':  cp = "&gt;";    break;
742                 case '&':  cp = "&amp;";   break;
743                 case '"':  cp = "&quot;";  break;
744                 default:  *bp++ = ch;      continue;
745                 }
746                 while( *cp != 0 ) *bp++ = *cp++;
747         }
748         *bp = 0;
749         return ret;
750 }
751
752 long XMLBuffer::encoded_length(const char *sp, int n)
753 {
754         long len = 0;
755         if( n < 0 ) n = strlen(sp);
756         const char *ep = sp + n;
757         while( sp < ep ) {
758                 int ch = *sp++;
759                 switch( ch ) {
760                 case '<':  len += 4;  break;
761                 case '>':  len += 4;  break;
762                 case '&':  len += 5;  break;
763                 case '"':  len += 6;  break;
764                 default:   ++len;     break;
765                 }
766         }
767         return len;
768 }
769
770 char *XMLBuffer::copy_data(char *bp, const char *sp, int n)
771 {
772         int len = n < 0 ? strlen(sp) : n;
773         if( bp != sp )
774                 memmove(bp,sp,len);
775         bp[len] = 0;
776         return bp;
777 }
778
779 long XMLBuffer::copy_length(const char *sp, int n)
780 {
781         int len = n < 0 ? strlen(sp) : n;
782         return len;
783 }
784