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