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);
112 void XMLBuffer::copy_from(XMLBuffer *xbuf)
114 if( bsz != xbuf->bsz ) { delete [] bfr; bfr = 0; }
115 if( !bfr ) bfr = new unsigned char[bsz = xbuf->bsz];
117 long ilen = xbuf->otell(), olen = xbuf->itell();
121 memmove(bfr, xbuf->bfr, ilen);
122 destroy = xbuf->destroy;
126 // Precision in base 10
127 // for float is 6 significant figures
128 // for double is 16 significant figures
130 XMLTag::Property::Property(const char *pp, const char *vp)
132 //printf("Property %s = %s\n",pp, vp);
137 XMLTag::Property::~Property()
152 properties.remove_all_objects();
156 const char *XMLTag::get_property(const char *property, char *value)
158 int i = properties.size();
159 while( --i >= 0 && strcasecmp(properties[i]->prop, property) );
161 strcpy(value, properties[i]->value);
168 const char *XMLTag::get_property_text(int i) {
169 return i < properties.size() ? properties[i]->prop : "";
171 int XMLTag::get_property_int(int i) {
172 return i < properties.size() ? atol(properties[i]->value) : 0;
174 float XMLTag::get_property_float(int i) {
175 return i < properties.size() ? atof(properties[i]->value) : 0.;
177 const char* XMLTag::get_property(const char *prop) {
178 for( int i=0; i<properties.size(); ++i ) {
179 if( !strcasecmp(properties[i]->prop, prop) )
180 return properties[i]->value;
184 int32_t XMLTag::get_property(const char *prop, int32_t dflt) {
185 const char *cp = get_property(prop);
186 return !cp ? dflt : atol(cp);
188 int64_t XMLTag::get_property(const char *prop, int64_t dflt) {
189 const char *cp = get_property(prop);
190 return !cp ? dflt : strtoll(cp,0,0);
192 float XMLTag::get_property(const char *prop, float dflt) {
193 const char *cp = get_property(prop);
194 return !cp ? dflt : atof(cp);
196 double XMLTag::get_property(const char *prop, double dflt) {
197 const char *cp = get_property(prop);
198 return !cp ? dflt : atof(cp);
202 int XMLTag::set_title(const char *text) {
206 int XMLTag::set_property(const char *text, const char *value) {
207 properties.append(new XMLTag::Property(text, value));
210 int XMLTag::set_property(const char *text, int32_t value)
212 char text_value[BCSTRLEN];
213 sprintf(text_value, "%d", value);
214 set_property(text, text_value);
217 int XMLTag::set_property(const char *text, int64_t value)
219 char text_value[BCSTRLEN];
220 sprintf(text_value, "%jd", value);
221 set_property(text, text_value);
224 int XMLTag::set_property(const char *text, float value)
226 char text_value[BCSTRLEN];
227 if (value - (float)((int64_t)value) == 0)
228 sprintf(text_value, "%jd", (int64_t)value);
230 sprintf(text_value, "%.6e", value);
231 set_property(text, text_value);
234 int XMLTag::set_property(const char *text, double value)
236 char text_value[BCSTRLEN];
237 if (value - (double)((int64_t)value) == 0)
238 sprintf(text_value, "%jd", (int64_t)value);
240 sprintf(text_value, "%.16e", value);
241 set_property(text, text_value);
246 int XMLTag::reset_tag()
249 properties.remove_all_objects();
253 int XMLTag::write_tag(FileXML *xml)
255 XMLBuffer *buf = xml->buffer;
257 buf->next(left_delm);
258 buf->write(title, strlen(title));
261 for( int i=0; i<properties.size(); ++i ) {
262 const char *prop = properties[i]->prop;
263 const char *value = properties[i]->value;
264 int plen = strlen(prop), vlen = strlen(value);
265 bool need_quotes = !vlen || strchr(value,' ');
267 xml->append_text(prop, plen);
269 if( need_quotes ) buf->next('\"');
270 xml->append_text(value, vlen);
271 if( need_quotes ) buf->next('\"');
274 buf->next(right_delm);
278 #define ERETURN(s) do { \
279 printf("XMLTag::read_tag:%d tag \"%s\"%s\n",__LINE__,title,s);\
281 #define EOB_RETURN(s) ERETURN(", end of buffer");
283 int XMLTag::read_tag(FileXML *xml)
285 XMLBuffer *buf = xml->buffer;
287 long prop_start, prop_end;
288 long value_start, value_end;
290 int ch = buf->next();
293 while( ch>=0 && ws(ch) ) ch = buf->next();
294 if( ch < 0 ) EOB_RETURN();
297 ttl = buf->itell() - 1;
298 for( int i=0; i<MAX_TITLE && ch>=0; ++i, ch=buf->next() ) {
299 if( ch == right_delm || ch == '=' || ws(ch) ) break;
301 if( ch < 0 ) EOB_RETURN();
302 len = buf->itell()-1 - ttl;
303 if( len >= MAX_TITLE ) ERETURN(", title too long");
306 memmove(title, buf->pos(ttl), len);
309 // no title but first property.
316 while( ch >= 0 && ch != right_delm ) {
317 // find tag start, skip header leadin
318 while( ch >= 0 && (ch==left_delm || ws(ch)) )
320 // find end of property name
321 prop_start = buf->itell()-1;
322 while( ch >= 0 && (ch!=right_delm && ch!='=' && !ws(ch)) )
324 if( ch < 0 ) EOB_RETURN();
325 prop_end = buf->itell()-1;
327 while( ch >= 0 && ws(ch) )
329 if( ch == '=' ) ch = buf->next();
330 while( ch >= 0 && ws(ch) )
332 if( ch < 0 ) EOB_RETURN();
333 // set terminating char
340 value_start = buf->itell()-1;
342 // old edl bug work-around, allow nl in quoted string
343 if( ch==term || ch==right_delm ) break;
344 if( ch=='\n' && term!='\"' ) break;
347 if( ch < 0 ) EOB_RETURN();
348 value_end = buf->itell()-1;
350 int plen = prop_end-prop_start;
351 if( !plen ) continue;
352 int vlen = value_end-value_start;
353 char prop[plen+1], value[vlen+1];
354 const char *coded_prop = (const char *)buf->pos(prop_start);
355 const char *coded_value = (const char *)buf->pos(value_start);
356 // props should not have coded text
357 memcpy(prop, coded_prop, plen);
359 xml->decode(value, coded_value, vlen);
360 if( prop_end > prop_start ) {
361 Property *property = new Property(prop, value);
362 properties.append(property);
364 // skip the terminating char
365 if( ch != right_delm ) ch = buf->next();
367 if( !properties.size() && !title[0] ) ERETURN(", emtpy");
373 FileXML::FileXML(int coded)
377 buffer = new XMLBuffer();
384 if( !shared ) delete buffer;
389 int FileXML::terminate_string()
395 int FileXML::rewind()
403 int FileXML::append_newline()
405 append_data("\n", 1);
409 int FileXML::append_tag()
412 append_text(tag.string, tag.used);
417 int FileXML::append_text(const char *text)
420 append_text(text, strlen(text));
424 int FileXML::append_data(const char *text)
427 append_data(text, strlen(text));
431 int FileXML::append_data(const char *text, long len)
433 if( text != 0 && len > 0 )
434 buffer->write(text, len);
438 int FileXML::append_text(const char *text, long len)
440 if( text != 0 && len > 0 ) {
441 int size = coded_length(text, len);
442 char coded_text[size+1];
443 encode(coded_text, text, len);
444 buffer->write(coded_text, size);
450 char* FileXML::get_data()
452 long ofs = buffer->itell();
453 return (char *)buffer->pos(ofs);
455 char* FileXML::string()
457 return (char *)buffer->cstr();
460 long FileXML::length()
462 return buffer->otell();
465 char* FileXML::read_text()
467 int ch = buffer->next();
468 // filter out first char is new line
469 if( ch == '\n' ) ch = buffer->next();
470 long ipos = buffer->itell()-1;
471 // scan for delimiter
472 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
473 long pos = buffer->itell()-1;
474 if( ch >= 0 ) buffer->iseek(pos);
475 long len = pos - ipos;
476 if( len >= output_length ) {
478 output_length = len+1;
479 output = new char[output_length];
481 decode(output,(const char *)buffer->pos(ipos), len);
485 int FileXML::read_tag()
487 int ch = buffer->next();
489 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
490 if( ch < 0 ) return 1;
492 return tag.read_tag(this);
495 int FileXML::skip_tag()
497 char tag_title[sizeof(tag.title)];
498 strcpy(tag_title, tag.title);
500 while( !read_tag() ) {
501 if( tag.title[0] == tag_title[0] ) {
502 if( !strcasecmp(&tag_title[1], &tag.title[1]) ) ++n;
504 else if( tag.title[0] != '/' ) continue;
505 else if( strcasecmp(&tag_title[0], &tag.title[1]) ) continue;
506 else if( --n <= 0 ) return 0;
511 int FileXML::read_data_until(const char *tag_end, XMLBuffer *xbuf, int skip)
513 long ipos = buffer->itell();
515 for( int ch=buffer->next(); ch>=0; ch=buffer->next() ) {
516 if( pos < 0 ) { // looking for next tag
517 if( ch == left_delm ) {
518 ipos = buffer->itell()-1;
525 // check for end of match
526 if( !tag_end[pos] && ch == right_delm ) break;
527 // if mismatched, copy prefix to out
528 if( tag_end[pos] != ch ) {
529 xbuf->next(left_delm);
530 for( int i=0; i<pos; ++i )
531 xbuf->next(tag_end[i]);
538 // if end tag is reached, pos is left on the < of the end tag
539 if( !skip && pos >= 0 && !tag_end[pos] )
541 return xbuf->otell();
544 int FileXML::read_text_until(const char *tag_end, XMLBuffer *xbuf, int skip)
546 int len = read_data_until(tag_end, xbuf, skip);
547 char *cp = xbuf->cstr();
552 int FileXML::write_to_file(const char *filename)
554 FILE *out = fopen(filename, "wb");
556 eprintf("write_to_file %d \"%s\": %m\n", __LINE__, filename);
559 int ret = write_to_file(out, filename);
564 int FileXML::write_to_file(FILE *file, const char *filename)
566 strcpy(this->filename, filename);
567 const char *str = string();
568 long len = strlen(str);
569 // Position may have been rewound after storing
570 if( !fwrite(xml_header, xml_header_size, 1, file) ||
571 ( len > 0 && !fwrite(str, len, 1, file) ) ) {
572 eprintf("\"%s\": %m\n", filename);
578 int FileXML::read_from_file(const char *filename, int ignore_error)
581 strcpy(this->filename, filename);
582 FILE *in = fopen(filename, "rb");
585 eprintf("\"%s\" %m\n", filename);
588 fseek(in, 0, SEEK_END);
589 long length = ftell(in);
590 fseek(in, 0, SEEK_SET);
591 char *fbfr = new char[length+1];
593 (void)fread(fbfr, length, 1, in);
595 buffer = new XMLBuffer(fbfr, length, 1);
600 int FileXML::read_from_string(char *string)
602 strcpy(this->filename, "");
603 long length = strlen(string);
604 char *sbfr = new char[length+1];
605 strcpy(sbfr, string);
607 buffer = new XMLBuffer(sbfr, length, 1);
611 void FileXML::set_coding(int coding)
615 decode = XMLBuffer::decode_data;
616 encode = XMLBuffer::encode_data;
617 coded_length = XMLBuffer::encoded_length;
620 decode = XMLBuffer::copy_data;
621 encode = XMLBuffer::copy_data;
622 coded_length = XMLBuffer::copy_length;
626 int FileXML::get_coding()
631 int FileXML::set_shared_input(XMLBuffer *xbuf)
633 strcpy(this->filename, "");
642 int FileXML::set_shared_output(XMLBuffer *xbuf)
644 strcpy(this->filename, "");
654 // ================================ XML tag
656 int XMLTag::title_is(const char *tp)
658 return !strcasecmp(title, tp) ? 1 : 0;
661 char* XMLTag::get_title()
666 int XMLTag::get_title(char *value)
668 if( title[0] != 0 ) strcpy(value, title);
672 int XMLTag::test_property(char *property, char *value)
674 for( int i=0; i<properties.size(); ++i ) {
675 if( !strcasecmp(properties[i]->prop, property) &&
676 !strcasecmp(properties[i]->value, value) )
682 static inline int xml_cmp(const char *np, const char *sp)
684 const char *tp = ++np;
685 while( *np ) { if( *np != *sp ) return 0; ++np; ++sp; }
689 char *XMLBuffer::decode_data(char *bp, const char *sp, int n)
692 if( n < 0 ) n = strlen(sp);
693 const char *ep = sp + n;
695 if( (n=*sp++) != '&' ) { *bp++ = n; continue; }
696 switch( (n=*sp++) ) {
698 if( (n=xml_cmp("amp;", sp)) ) { *bp++ = '&'; sp += n; continue; }
701 if( (n=xml_cmp("gt;", sp)) ) { *bp++ = '>'; sp += n; continue; }
704 if( (n=xml_cmp("lt;", sp)) ) { *bp++ = '<'; sp += n; continue; }
707 if( (n=xml_cmp("quot;", sp)) ) { *bp++ = '"'; sp += n; continue; }
709 case '#': { // &#<num>;
710 char *cp = 0; int v = strtoul(sp,&cp,10);
711 if( *cp == ';' ) { *bp++ = (char)v; sp = cp+1; continue; }
720 while( --n >= 0 ) *bp++ = *sp++;
727 char *XMLBuffer::encode_data(char *bp, const char *sp, int n)
730 if( n < 0 ) n = strlen(sp);
731 const char *cp, *ep = sp + n;
735 case '<': cp = "<"; break;
736 case '>': cp = ">"; break;
737 case '&': cp = "&"; break;
738 case '"': cp = """; break;
739 default: *bp++ = ch; continue;
741 while( *cp != 0 ) *bp++ = *cp++;
747 long XMLBuffer::encoded_length(const char *sp, int n)
750 if( n < 0 ) n = strlen(sp);
751 const char *ep = sp + n;
755 case '<': len += 4; break;
756 case '>': len += 4; break;
757 case '&': len += 5; break;
758 case '"': len += 6; break;
759 default: ++len; break;
765 char *XMLBuffer::copy_data(char *bp, const char *sp, int n)
767 int len = n < 0 ? strlen(sp) : n;
774 long XMLBuffer::copy_length(const char *sp, int n)
776 int len = n < 0 ? strlen(sp) : n;