4 * Copyright (C) 2008 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
27 #include "bcsignals.h"
28 #include "arraylist.h"
31 #include "mainerror.h"
33 // messes up cutads link
35 #define eprintf printf
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);
41 XMLBuffer::XMLBuffer(long buf_size, long max_size, int del)
44 bfr = new unsigned char[bsz];
51 XMLBuffer::XMLBuffer(const char *buf, long buf_size, int del)
53 bfr = (unsigned char *)buf;
61 XMLBuffer::XMLBuffer(long buf_size, char *buf, int del)
63 bfr = (unsigned char *)buf;
71 XMLBuffer::~XMLBuffer()
73 if( destroy ) delete [] bfr;
76 int XMLBuffer::demand(long len)
79 if( !destroy ) return 0;
81 len += sz/2 + BCTEXTLEN;
82 unsigned char *np = new unsigned char[len];
83 if( sz > 0 ) memcpy(np,bfr,sz);
85 outp = np + (outp-bfr);
86 lmt = np + len; bsz = len;
87 delete [] bfr; bfr = np;
92 int XMLBuffer::write(const char *bp, int len)
94 if( len <= 0 ) return 0;
95 if( !destroy && lmt-inp < len ) len = lmt-inp;
102 int XMLBuffer::read(char *bp, int len)
104 long size = inp - outp;
105 if( size <= 0 && len > 0 ) return -1;
106 if( len > size ) len = size;
107 memmove(bp,outp,len);
113 // Precision in base 10
114 // for float is 6 significant figures
115 // for double is 16 significant figures
117 XMLTag::Property::Property(const char *pp, const char *vp)
119 //printf("Property %s = %s\n",pp, vp);
124 XMLTag::Property::~Property()
139 properties.remove_all_objects();
143 const char *XMLTag::get_property(const char *property, char *value)
145 int i = properties.size();
146 while( --i >= 0 && strcasecmp(properties[i]->prop, property) );
148 strcpy(value, properties[i]->value);
155 const char *XMLTag::get_property_text(int i) {
156 return i < properties.size() ? properties[i]->prop : "";
158 int XMLTag::get_property_int(int i) {
159 return i < properties.size() ? atol(properties[i]->value) : 0;
161 float XMLTag::get_property_float(int i) {
162 return i < properties.size() ? atof(properties[i]->value) : 0.;
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;
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);
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);
179 float XMLTag::get_property(const char *prop, float dflt) {
180 const char *cp = get_property(prop);
181 return !cp ? dflt : atof(cp);
183 double XMLTag::get_property(const char *prop, double dflt) {
184 const char *cp = get_property(prop);
185 return !cp ? dflt : atof(cp);
189 int XMLTag::set_title(const char *text) {
193 int XMLTag::set_property(const char *text, const char *value) {
194 properties.append(new XMLTag::Property(text, value));
197 int XMLTag::set_property(const char *text, int32_t value)
199 char text_value[BCSTRLEN];
200 sprintf(text_value, "%d", value);
201 set_property(text, text_value);
204 int XMLTag::set_property(const char *text, int64_t value)
206 char text_value[BCSTRLEN];
207 sprintf(text_value, "%jd", value);
208 set_property(text, text_value);
211 int XMLTag::set_property(const char *text, float value)
213 char text_value[BCSTRLEN];
214 if (value - (float)((int64_t)value) == 0)
215 sprintf(text_value, "%jd", (int64_t)value);
217 sprintf(text_value, "%.6e", value);
218 set_property(text, text_value);
221 int XMLTag::set_property(const char *text, double value)
223 char text_value[BCSTRLEN];
224 if (value - (double)((int64_t)value) == 0)
225 sprintf(text_value, "%jd", (int64_t)value);
227 sprintf(text_value, "%.16e", value);
228 set_property(text, text_value);
233 int XMLTag::reset_tag()
236 properties.remove_all_objects();
240 int XMLTag::write_tag(FileXML *xml)
242 XMLBuffer *buf = xml->buffer;
244 buf->next(left_delm);
245 buf->write(title, strlen(title));
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,' ');
254 xml->append_text(prop, plen);
256 if( need_quotes ) buf->next('\"');
257 xml->append_text(value, vlen);
258 if( need_quotes ) buf->next('\"');
261 buf->next(right_delm);
265 #define ERETURN(s) do { \
266 printf("XMLTag::read_tag:%d tag \"%s\"%s\n",__LINE__,title,s);\
268 #define EOB_RETURN(s) ERETURN(", end of buffer");
270 int XMLTag::read_tag(FileXML *xml)
272 XMLBuffer *buf = xml->buffer;
274 long prop_start, prop_end;
275 long value_start, value_end;
277 int ch = buf->next();
280 while( ch>=0 && ws(ch) ) ch = buf->next();
281 if( ch < 0 ) EOB_RETURN();
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;
288 if( ch < 0 ) EOB_RETURN();
289 len = buf->itell()-1 - ttl;
290 if( len >= MAX_TITLE ) ERETURN(", title too long");
293 memmove(title, buf->pos(ttl), len);
296 // no title but first property.
303 while( ch >= 0 && ch != right_delm ) {
304 // find tag start, skip header leadin
305 while( ch >= 0 && (ch==left_delm || ws(ch)) )
307 // find end of property name
308 prop_start = buf->itell()-1;
309 while( ch >= 0 && (ch!=right_delm && ch!='=' && !ws(ch)) )
311 if( ch < 0 ) EOB_RETURN();
312 prop_end = buf->itell()-1;
314 while( ch >= 0 && ws(ch) )
316 if( ch == '=' ) ch = buf->next();
317 while( ch >= 0 && ws(ch) )
319 if( ch < 0 ) EOB_RETURN();
320 // set terminating char
327 value_start = buf->itell()-1;
329 // old edl bug work-around, allow nl in quoted string
330 if( ch==term || ch==right_delm ) break;
331 if( ch=='\n' && term!='\"' ) break;
334 if( ch < 0 ) EOB_RETURN();
335 value_end = buf->itell()-1;
337 int plen = prop_end-prop_start;
338 if( !plen ) continue;
339 int vlen = value_end-value_start;
340 char prop[plen+1], value[vlen+1];
341 const char *coded_prop = (const char *)buf->pos(prop_start);
342 const char *coded_value = (const char *)buf->pos(value_start);
343 // props should not have coded text
344 memcpy(prop, coded_prop, plen);
346 xml->decode(value, coded_value, vlen);
347 if( prop_end > prop_start ) {
348 Property *property = new Property(prop, value);
349 properties.append(property);
351 // skip the terminating char
352 if( ch != right_delm ) ch = buf->next();
354 if( !properties.size() && !title[0] ) ERETURN(", emtpy");
360 FileXML::FileXML(int coded)
364 buffer = new XMLBuffer();
375 int FileXML::terminate_string()
381 int FileXML::rewind()
389 int FileXML::append_newline()
391 append_data("\n", 1);
395 int FileXML::append_tag()
398 append_text(tag.string, tag.used);
403 int FileXML::append_text(const char *text)
406 append_text(text, strlen(text));
410 int FileXML::append_data(const char *text)
413 append_data(text, strlen(text));
417 int FileXML::append_data(const char *text, long len)
419 if( text != 0 && len > 0 )
420 buffer->write(text, len);
424 int FileXML::append_text(const char *text, long len)
426 if( text != 0 && len > 0 ) {
427 int size = coded_length(text, len);
428 char coded_text[size+1];
429 encode(coded_text, text, len);
430 buffer->write(coded_text, size);
436 char* FileXML::get_data()
438 long ofs = buffer->itell();
439 return (char *)buffer->pos(ofs);
441 char* FileXML::string()
443 return (char *)buffer->str();
446 long FileXML::length()
448 return buffer->otell();
451 char* FileXML::read_text()
453 int ch = buffer->next();
454 // filter out first char is new line
455 if( ch == '\n' ) ch = buffer->next();
456 long ipos = buffer->itell()-1;
457 // scan for delimiter
458 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
459 long pos = buffer->itell()-1;
460 if( ch >= 0 ) buffer->iseek(pos);
461 long len = pos - ipos;
462 if( len >= output_length ) {
464 output_length = len+1;
465 output = new char[output_length];
467 decode(output,(const char *)buffer->pos(ipos), len);
471 int FileXML::read_tag()
473 int ch = buffer->next();
475 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
476 if( ch < 0 ) return 1;
478 return tag.read_tag(this);
481 int FileXML::skip_tag()
483 char tag_title[sizeof(tag.title)];
484 strcpy(tag_title, tag.title);
486 while( !read_tag() ) {
487 if( tag.title[0] == tag_title[0] ) {
488 if( !strcasecmp(&tag_title[1], &tag.title[1]) ) ++n;
490 else if( tag.title[0] != '/' ) continue;
491 else if( strcasecmp(&tag_title[0], &tag.title[1]) ) continue;
492 else if( --n <= 0 ) return 0;
497 int FileXML::read_data_until(const char *tag_end, char *out, int len, int skip)
499 long ipos = buffer->itell();
500 int opos = 0, pos = -1;
501 int ch = buffer->next();
502 for( int olen=len-1; ch>=0 && opos<olen; ch=buffer->next() ) {
503 if( pos < 0 ) { // looking for next tag
504 if( ch == left_delm ) {
505 ipos = buffer->itell()-1;
512 // check for end of match
513 if( !tag_end[pos] && ch == right_delm ) break;
514 // if mismatched, copy prefix to out
515 if( tag_end[pos] != ch ) {
516 out[opos++] = left_delm;
517 for( int i=0; i<pos && opos<olen; ++i )
518 out[opos++] = tag_end[i];
519 if( opos < olen ) out[opos++] = ch;
525 // if end tag is reached, pos is left on the < of the end tag
526 if( !skip && pos >= 0 && !tag_end[pos] )
531 int FileXML::read_text_until(const char *tag_end, char *out, int len, int skip)
534 int opos = read_data_until(tag_end, data, len, skip);
535 decode(out, data, opos);
539 int FileXML::write_to_file(const char *filename)
541 FILE *out = fopen(filename, "wb");
543 eprintf("write_to_file %d \"%s\": %m\n", __LINE__, filename);
546 int ret = write_to_file(out, filename);
551 int FileXML::write_to_file(FILE *file, const char *filename)
553 strcpy(this->filename, filename);
554 const char *str = string();
555 long len = strlen(str);
556 // Position may have been rewound after storing
557 if( !fwrite(xml_header, xml_header_size, 1, file) ||
558 ( len > 0 && !fwrite(str, len, 1, file) ) ) {
559 eprintf("\"%s\": %m\n", filename);
565 int FileXML::read_from_file(const char *filename, int ignore_error)
568 strcpy(this->filename, filename);
569 FILE *in = fopen(filename, "rb");
572 eprintf("\"%s\" %m\n", filename);
575 fseek(in, 0, SEEK_END);
576 long length = ftell(in);
577 fseek(in, 0, SEEK_SET);
578 char *fbfr = new char[length+1];
580 (void)fread(fbfr, length, 1, in);
582 buffer = new XMLBuffer(fbfr, length, 1);
587 int FileXML::read_from_string(char *string)
589 strcpy(this->filename, "");
590 long length = strlen(string);
591 char *sbfr = new char[length+1];
592 strcpy(sbfr, string);
594 buffer = new XMLBuffer(sbfr, length, 1);
598 void FileXML::set_coding(int coding)
602 decode = XMLBuffer::decode_data;
603 encode = XMLBuffer::encode_data;
604 coded_length = XMLBuffer::encoded_length;
607 decode = XMLBuffer::copy_data;
608 encode = XMLBuffer::copy_data;
609 coded_length = XMLBuffer::copy_length;
613 int FileXML::get_coding()
618 int FileXML::set_shared_input(char *shared_string, long avail, int coded)
620 strcpy(this->filename, "");
622 buffer = new XMLBuffer(shared_string, avail, 0);
627 int FileXML::set_shared_output(char *shared_string, long avail, int coded)
629 strcpy(this->filename, "");
631 buffer = new XMLBuffer(avail, shared_string, 0);
638 // ================================ XML tag
642 int XMLTag::title_is(const char *tp)
644 return !strcasecmp(title, tp) ? 1 : 0;
647 char* XMLTag::get_title()
652 int XMLTag::get_title(char *value)
654 if( title[0] != 0 ) strcpy(value, title);
658 int XMLTag::test_property(char *property, char *value)
660 for( int i=0; i<properties.size(); ++i ) {
661 if( !strcasecmp(properties[i]->prop, property) &&
662 !strcasecmp(properties[i]->value, value) )
668 static inline int xml_cmp(const char *np, const char *sp)
670 const char *tp = ++np;
671 while( *np ) { if( *np != *sp ) return 0; ++np; ++sp; }
675 char *XMLBuffer::decode_data(char *bp, const char *sp, int n)
678 if( n < 0 ) n = strlen(sp);
679 const char *ep = sp + n;
681 if( (n=*sp++) != '&' ) { *bp++ = n; continue; }
682 switch( (n=*sp++) ) {
684 if( (n=xml_cmp("amp;", sp)) ) { *bp++ = '&'; sp += n; continue; }
687 if( (n=xml_cmp("gt;", sp)) ) { *bp++ = '>'; sp += n; continue; }
690 if( (n=xml_cmp("lt;", sp)) ) { *bp++ = '<'; sp += n; continue; }
693 if( (n=xml_cmp("quot;", sp)) ) { *bp++ = '"'; sp += n; continue; }
695 case '#': { // &#<num>;
696 char *cp = 0; int v = strtoul(sp,&cp,10);
697 if( *cp == ';' ) { *bp++ = (char)v; sp = cp+1; continue; }
706 while( --n >= 0 ) *bp++ = *sp++;
713 char *XMLBuffer::encode_data(char *bp, const char *sp, int n)
716 if( n < 0 ) n = strlen(sp);
717 const char *cp, *ep = sp + n;
721 case '<': cp = "<"; break;
722 case '>': cp = ">"; break;
723 case '&': cp = "&"; break;
724 case '"': cp = """; break;
725 default: *bp++ = ch; continue;
727 while( *cp != 0 ) *bp++ = *cp++;
733 long XMLBuffer::encoded_length(const char *sp, int n)
736 if( n < 0 ) n = strlen(sp);
737 const char *ep = sp + n;
741 case '<': len += 4; break;
742 case '>': len += 4; break;
743 case '&': len += 5; break;
744 case '"': len += 6; break;
745 default: ++len; break;
751 char *XMLBuffer::copy_data(char *bp, const char *sp, int n)
753 int len = n < 0 ? strlen(sp) : n;
759 long XMLBuffer::copy_length(const char *sp, int n)
761 int len = n < 0 ? strlen(sp) : n;