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 unsigned char *&XMLBuffer::demand(long len)
80 len += sz/2 + BCTEXTLEN;
81 unsigned char *np = new unsigned char[len];
82 if( sz > 0 ) memcpy(np,bfr,sz);
84 outp = np + (outp-bfr);
85 lmt = np + len; bsz = len;
86 delete [] bfr; bfr = np;
91 int XMLBuffer::write(const char *bp, int len)
93 if( !destroy && lmt-inp < len ) len = lmt-inp;
94 if( len <= 0 ) return 0;
101 int XMLBuffer::read(char *bp, int len)
103 long size = inp - outp;
104 if( size <= 0 && len > 0 ) return -1;
105 if( len > size ) len = size;
106 memmove(bp,outp,len);
112 // Precision in base 10
113 // for float is 6 significant figures
114 // for double is 16 significant figures
116 XMLTag::Property::Property(const char *pp, const char *vp)
118 //printf("Property %s = %s\n",pp, vp);
123 XMLTag::Property::~Property()
138 properties.remove_all_objects();
142 const char *XMLTag::get_property(const char *property, char *value)
144 int i = properties.size();
145 while( --i >= 0 && strcasecmp(properties[i]->prop, property) );
147 strcpy(value, properties[i]->value);
154 const char *XMLTag::get_property_text(int i) {
155 return i < properties.size() ? properties[i]->prop : "";
157 int XMLTag::get_property_int(int i) {
158 return i < properties.size() ? atol(properties[i]->value) : 0;
160 float XMLTag::get_property_float(int i) {
161 return i < properties.size() ? atof(properties[i]->value) : 0.;
163 const char* XMLTag::get_property(const char *prop) {
164 for( int i=0; i<properties.size(); ++i ) {
165 if( !strcasecmp(properties[i]->prop, prop) )
166 return properties[i]->value;
170 int32_t XMLTag::get_property(const char *prop, int32_t dflt) {
171 const char *cp = get_property(prop);
172 return !cp ? dflt : atol(cp);
174 int64_t XMLTag::get_property(const char *prop, int64_t dflt) {
175 const char *cp = get_property(prop);
176 return !cp ? dflt : strtoll(cp,0,0);
178 float XMLTag::get_property(const char *prop, float dflt) {
179 const char *cp = get_property(prop);
180 return !cp ? dflt : atof(cp);
182 double XMLTag::get_property(const char *prop, double dflt) {
183 const char *cp = get_property(prop);
184 return !cp ? dflt : atof(cp);
188 int XMLTag::set_title(const char *text) {
192 int XMLTag::set_property(const char *text, const char *value) {
193 properties.append(new XMLTag::Property(text, value));
196 int XMLTag::set_property(const char *text, int32_t value)
198 char text_value[BCSTRLEN];
199 sprintf(text_value, "%d", value);
200 set_property(text, text_value);
203 int XMLTag::set_property(const char *text, int64_t value)
205 char text_value[BCSTRLEN];
206 sprintf(text_value, "%jd", value);
207 set_property(text, text_value);
210 int XMLTag::set_property(const char *text, float value)
212 char text_value[BCSTRLEN];
213 if (value - (float)((int64_t)value) == 0)
214 sprintf(text_value, "%jd", (int64_t)value);
216 sprintf(text_value, "%.6e", value);
217 set_property(text, text_value);
220 int XMLTag::set_property(const char *text, double value)
222 char text_value[BCSTRLEN];
223 if (value - (double)((int64_t)value) == 0)
224 sprintf(text_value, "%jd", (int64_t)value);
226 sprintf(text_value, "%.16e", value);
227 set_property(text, text_value);
232 int XMLTag::reset_tag()
235 properties.remove_all_objects();
239 int XMLTag::write_tag(FileXML *xml)
241 XMLBuffer *buf = xml->buffer;
243 buf->next(left_delm);
244 buf->write(title, strlen(title));
247 for( int i=0; i<properties.size(); ++i ) {
248 const char *prop = properties[i]->prop;
249 const char *value = properties[i]->value;
250 int plen = strlen(prop), vlen = strlen(value);
251 bool need_quotes = !vlen || strchr(value,' ');
253 xml->append_text(prop, plen);
255 if( need_quotes ) buf->next('\"');
256 xml->append_text(value, vlen);
257 if( need_quotes ) buf->next('\"');
260 buf->next(right_delm);
264 #define ERETURN(s) do { \
265 printf("XMLTag::read_tag:%d tag \"%s\"%s\n",__LINE__,title,s);\
267 #define EOB_RETURN(s) ERETURN(", end of buffer");
269 int XMLTag::read_tag(FileXML *xml)
271 XMLBuffer *buf = xml->buffer;
273 long prop_start, prop_end;
274 long value_start, value_end;
276 int ch = buf->next();
279 while( ch>=0 && ws(ch) ) ch = buf->next();
280 if( ch < 0 ) EOB_RETURN();
283 ttl = buf->itell() - 1;
284 for( int i=0; i<MAX_TITLE && ch>=0; ++i, ch=buf->next() ) {
285 if( ch == right_delm || ch == '=' || ws(ch) ) break;
287 if( ch < 0 ) EOB_RETURN();
288 len = buf->itell()-1 - ttl;
289 if( len >= MAX_TITLE ) ERETURN(", title too long");
292 memmove(title, buf->pos(ttl), len);
295 // no title but first property.
302 while( ch >= 0 && ch != right_delm ) {
303 // find tag start, skip header leadin
304 while( ch >= 0 && (ch==left_delm || ws(ch)) )
306 // find end of property name
307 prop_start = buf->itell()-1;
308 while( ch >= 0 && (ch!=right_delm && ch!='=' && !ws(ch)) )
310 if( ch < 0 ) EOB_RETURN();
311 prop_end = buf->itell()-1;
313 while( ch >= 0 && ws(ch) )
315 if( ch == '=' ) ch = buf->next();
316 while( ch >= 0 && ws(ch) )
318 if( ch < 0 ) EOB_RETURN();
319 // set terminating char
326 value_start = buf->itell()-1;
327 while( ch >= 0 && (ch!=term && ch!=right_delm && ch!='\n') )
329 if( ch < 0 ) EOB_RETURN();
330 value_end = buf->itell()-1;
332 int plen = prop_end-prop_start;
333 if( !plen ) continue;
334 int vlen = value_end-value_start;
335 char prop[plen+1], value[vlen+1];
336 const char *coded_prop = (const char *)buf->pos(prop_start);
337 const char *coded_value = (const char *)buf->pos(value_start);
338 // props should not have coded text
339 memcpy(prop, coded_prop, plen);
341 xml->decode(value, coded_value, vlen);
342 if( prop_end > prop_start ) {
343 Property *property = new Property(prop, value);
344 properties.append(property);
346 // skip the terminating char
347 if( ch != right_delm ) ch = buf->next();
349 if( !properties.size() && !title[0] ) ERETURN(", emtpy");
355 FileXML::FileXML(int coded)
359 buffer = new XMLBuffer();
370 int FileXML::terminate_string()
376 int FileXML::rewind()
384 int FileXML::append_newline()
386 append_data("\n", 1);
390 int FileXML::append_tag()
393 append_text(tag.string, tag.used);
398 int FileXML::append_text(const char *text)
401 append_text(text, strlen(text));
405 int FileXML::append_data(const char *text)
408 append_data(text, strlen(text));
412 int FileXML::append_data(const char *text, long len)
414 if( text != 0 && len > 0 )
415 buffer->write(text, len);
419 int FileXML::append_text(const char *text, long len)
421 if( text != 0 && len > 0 ) {
422 int size = coded_length(text, len);
423 char coded_text[size+1];
424 encode(coded_text, text, len);
425 buffer->write(coded_text, size);
431 char* FileXML::get_data()
433 long ofs = buffer->itell();
434 return (char *)buffer->pos(ofs);
436 char* FileXML::string()
438 return (char *)buffer->str();
441 long FileXML::length()
443 return buffer->otell();
446 char* FileXML::read_text()
448 int ch = buffer->next();
449 // filter out first char is new line
450 if( ch == '\n' ) ch = buffer->next();
451 long ipos = buffer->itell()-1;
452 // scan for delimiter
453 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
454 long pos = buffer->itell()-1;
455 if( ch >= 0 ) buffer->iseek(pos);
456 long len = pos - ipos;
457 if( len >= output_length ) {
459 output_length = len+1;
460 output = new char[output_length];
462 decode(output,(const char *)buffer->pos(ipos), len);
466 int FileXML::read_tag()
468 int ch = buffer->next();
470 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
471 if( ch < 0 ) return 1;
473 return tag.read_tag(this);
476 int FileXML::skip_tag()
478 char tag_title[sizeof(tag.title)];
479 strcpy(tag_title, tag.title);
481 while( !read_tag() ) {
482 if( tag.title[0] == tag_title[0] ) {
483 if( !strcasecmp(&tag_title[1], &tag.title[1]) ) ++n;
485 else if( tag.title[0] != '/' ) continue;
486 else if( strcasecmp(&tag_title[0], &tag.title[1]) ) continue;
487 else if( --n <= 0 ) return 0;
492 int FileXML::read_data_until(const char *tag_end, char *out, int len, int skip)
494 long ipos = buffer->itell();
495 int opos = 0, pos = -1;
496 int ch = buffer->next();
497 for( int olen=len-1; ch>=0 && opos<olen; ch=buffer->next() ) {
498 if( pos < 0 ) { // looking for next tag
499 if( ch == left_delm ) {
500 ipos = buffer->itell()-1;
507 // check for end of match
508 if( !tag_end[pos] && ch == right_delm ) break;
509 // if mismatched, copy prefix to out
510 if( tag_end[pos] != ch ) {
511 out[opos++] = left_delm;
512 for( int i=0; i<pos && opos<olen; ++i )
513 out[opos++] = tag_end[i];
514 if( opos < olen ) out[opos++] = ch;
520 // if end tag is reached, pos is left on the < of the end tag
521 if( !skip && pos >= 0 && !tag_end[pos] )
526 int FileXML::read_text_until(const char *tag_end, char *out, int len, int skip)
529 int opos = read_data_until(tag_end, data, len, skip);
530 decode(out, data, opos);
534 int FileXML::write_to_file(const char *filename)
536 FILE *out = fopen(filename, "wb");
538 eprintf("write_to_file %d \"%s\": %m\n", __LINE__, filename);
541 int ret = write_to_file(out, filename);
546 int FileXML::write_to_file(FILE *file, const char *filename)
548 strcpy(this->filename, filename);
549 const char *str = string();
550 long len = strlen(str);
551 // Position may have been rewound after storing
552 if( !fwrite(xml_header, xml_header_size, 1, file) ||
553 ( len > 0 && !fwrite(str, len, 1, file) ) ) {
554 eprintf("\"%s\": %m\n", filename);
560 int FileXML::read_from_file(const char *filename, int ignore_error)
563 strcpy(this->filename, filename);
564 FILE *in = fopen(filename, "rb");
567 eprintf("\"%s\" %m\n", filename);
570 fseek(in, 0, SEEK_END);
571 long length = ftell(in);
572 fseek(in, 0, SEEK_SET);
573 char *fbfr = new char[length+1];
575 (void)fread(fbfr, length, 1, in);
577 buffer = new XMLBuffer(fbfr, length, 1);
582 int FileXML::read_from_string(char *string)
584 strcpy(this->filename, "");
585 long length = strlen(string);
586 char *sbfr = new char[length+1];
587 strcpy(sbfr, string);
589 buffer = new XMLBuffer(sbfr, length, 1);
593 void FileXML::set_coding(int coding)
597 decode = XMLBuffer::decode_data;
598 encode = XMLBuffer::encode_data;
599 coded_length = XMLBuffer::encoded_length;
602 decode = XMLBuffer::copy_data;
603 encode = XMLBuffer::copy_data;
604 coded_length = XMLBuffer::copy_length;
608 int FileXML::get_coding()
613 int FileXML::set_shared_input(char *shared_string, long avail, int coded)
615 strcpy(this->filename, "");
617 buffer = new XMLBuffer(shared_string, avail, 0);
622 int FileXML::set_shared_output(char *shared_string, long avail, int coded)
624 strcpy(this->filename, "");
626 buffer = new XMLBuffer(avail, shared_string, 0);
633 // ================================ XML tag
637 int XMLTag::title_is(const char *tp)
639 return !strcasecmp(title, tp) ? 1 : 0;
642 char* XMLTag::get_title()
647 int XMLTag::get_title(char *value)
649 if( title[0] != 0 ) strcpy(value, title);
653 int XMLTag::test_property(char *property, char *value)
655 for( int i=0; i<properties.size(); ++i ) {
656 if( !strcasecmp(properties[i]->prop, property) &&
657 !strcasecmp(properties[i]->value, value) )
663 static inline int xml_cmp(const char *np, const char *sp)
665 const char *tp = ++np;
666 while( *np ) { if( *np != *sp ) return 0; ++np; ++sp; }
670 char *XMLBuffer::decode_data(char *bp, const char *sp, int n)
673 if( n < 0 ) n = strlen(sp);
674 const char *ep = sp + n;
676 if( (n=*sp++) != '&' ) { *bp++ = n; continue; }
677 switch( (n=*sp++) ) {
679 if( (n=xml_cmp("amp;", sp)) ) { *bp++ = '&'; sp += n; continue; }
682 if( (n=xml_cmp("gt;", sp)) ) { *bp++ = '>'; sp += n; continue; }
685 if( (n=xml_cmp("lt;", sp)) ) { *bp++ = '<'; sp += n; continue; }
688 if( (n=xml_cmp("quot;", sp)) ) { *bp++ = '"'; sp += n; continue; }
690 case '#': { // &#<num>;
691 char *cp = 0; int v = strtoul(sp,&cp,10);
692 if( *cp == ';' ) { *bp++ = (char)v; sp = cp+1; continue; }
701 while( --n >= 0 ) *bp++ = *sp++;
708 char *XMLBuffer::encode_data(char *bp, const char *sp, int n)
711 if( n < 0 ) n = strlen(sp);
712 const char *cp, *ep = sp + n;
716 case '<': cp = "<"; break;
717 case '>': cp = ">"; break;
718 case '&': cp = "&"; break;
719 case '"': cp = """; break;
720 default: *bp++ = ch; continue;
722 while( *cp != 0 ) *bp++ = *cp++;
728 long XMLBuffer::encoded_length(const char *sp, int n)
731 if( n < 0 ) n = strlen(sp);
732 const char *ep = sp + n;
736 case '<': len += 4; break;
737 case '>': len += 4; break;
738 case '&': len += 5; break;
739 case '"': len += 6; break;
740 default: ++len; break;
746 char *XMLBuffer::copy_data(char *bp, const char *sp, int n)
748 int len = n < 0 ? strlen(sp) : n;
754 long XMLBuffer::copy_length(const char *sp, int n)
756 int len = n < 0 ? strlen(sp) : n;