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, const 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 unsigned char *np = new unsigned char[len];
81 if( inp > bfr ) memcpy(np,bfr,inp-bfr);
83 outp = np + (outp-bfr);
84 lmt = np + len; bsz = len;
85 delete [] bfr; bfr = np;
90 int XMLBuffer::write(const char *bp, int len)
92 if( !destroy && lmt-inp < len ) len = lmt-inp;
93 if( len <= 0 ) return 0;
100 int XMLBuffer::read(char *bp, int len)
102 long size = inp - outp;
103 if( size <= 0 && len > 0 ) return -1;
104 if( len > size ) len = size;
105 memmove(bp,outp,len);
111 // Precision in base 10
112 // for float is 6 significant figures
113 // for double is 16 significant figures
115 XMLTag::Property::Property(const char *pp, const char *vp)
117 //printf("Property %s = %s\n",pp, vp);
122 XMLTag::Property::~Property()
137 properties.remove_all_objects();
141 const char *XMLTag::get_property(const char *property, char *value)
143 int i = properties.size();
144 while( --i >= 0 && strcasecmp(properties[i]->prop, property) );
146 strcpy(value, properties[i]->value);
153 const char *XMLTag::get_property_text(int i) {
154 return i < properties.size() ? properties[i]->prop : "";
156 int XMLTag::get_property_int(int i) {
157 return i < properties.size() ? atol(properties[i]->value) : 0;
159 float XMLTag::get_property_float(int i) {
160 return i < properties.size() ? atof(properties[i]->value) : 0.;
162 const char* XMLTag::get_property(const char *prop) {
163 for( int i=0; i<properties.size(); ++i ) {
164 if( !strcasecmp(properties[i]->prop, prop) )
165 return properties[i]->value;
169 int32_t XMLTag::get_property(const char *prop, int32_t dflt) {
170 const char *cp = get_property(prop);
171 return !cp ? dflt : atol(cp);
173 int64_t XMLTag::get_property(const char *prop, int64_t dflt) {
174 const char *cp = get_property(prop);
175 return !cp ? dflt : strtoll(cp,0,0);
177 float XMLTag::get_property(const char *prop, float dflt) {
178 const char *cp = get_property(prop);
179 return !cp ? dflt : atof(cp);
181 double XMLTag::get_property(const char *prop, double dflt) {
182 const char *cp = get_property(prop);
183 return !cp ? dflt : atof(cp);
187 int XMLTag::set_title(const char *text) {
191 int XMLTag::set_property(const char *text, const char *value) {
192 properties.append(new XMLTag::Property(text, value));
195 int XMLTag::set_property(const char *text, int32_t value)
197 char text_value[BCSTRLEN];
198 sprintf(text_value, "%d", value);
199 set_property(text, text_value);
202 int XMLTag::set_property(const char *text, int64_t value)
204 char text_value[BCSTRLEN];
205 sprintf(text_value, "%jd", value);
206 set_property(text, text_value);
209 int XMLTag::set_property(const char *text, float value)
211 char text_value[BCSTRLEN];
212 if (value - (float)((int64_t)value) == 0)
213 sprintf(text_value, "%jd", (int64_t)value);
215 sprintf(text_value, "%.6e", value);
216 set_property(text, text_value);
219 int XMLTag::set_property(const char *text, double value)
221 char text_value[BCSTRLEN];
222 if (value - (double)((int64_t)value) == 0)
223 sprintf(text_value, "%jd", (int64_t)value);
225 sprintf(text_value, "%.16e", value);
226 set_property(text, text_value);
231 int XMLTag::reset_tag()
234 properties.remove_all_objects();
238 int XMLTag::write_tag(FileXML *xml)
240 XMLBuffer *buf = xml->buffer;
242 buf->next(left_delm);
243 buf->write(title, strlen(title));
246 for( int i=0; i<properties.size(); ++i ) {
247 const char *prop = properties[i]->prop;
248 const char *value = properties[i]->value;
249 int plen = strlen(prop), vlen = strlen(value);
250 bool need_quotes = !vlen || strchr(value,' ');
252 xml->append_text(prop, plen);
254 if( need_quotes ) buf->next('\"');
255 xml->append_text(value, vlen);
256 if( need_quotes ) buf->next('\"');
259 buf->next(right_delm);
263 #define ERETURN(s) do { \
264 printf("XMLTag::read_tag:%d tag \"%s\"%s\n",__LINE__,title,s);\
266 #define EOB_RETURN(s) ERETURN(", end of buffer");
268 int XMLTag::read_tag(FileXML *xml)
270 XMLBuffer *buf = xml->buffer;
272 long prop_start, prop_end;
273 long value_start, value_end;
275 int ch = buf->next();
278 while( ch>=0 && ws(ch) ) ch = buf->next();
279 if( ch < 0 ) EOB_RETURN();
282 ttl = buf->itell() - 1;
283 for( int i=0; i<MAX_TITLE && ch>=0; ++i, ch=buf->next() ) {
284 if( ch == right_delm || ch == '=' || ws(ch) ) break;
286 if( ch < 0 ) EOB_RETURN();
287 len = buf->itell()-1 - ttl;
288 if( len >= MAX_TITLE ) ERETURN(", title too long");
291 memmove(title, buf->pos(ttl), len);
294 // no title but first property.
301 while( ch >= 0 && ch != right_delm ) {
302 // find tag start, skip header leadin
303 while( ch >= 0 && (ch==left_delm || ws(ch)) )
305 // find end of property name
306 prop_start = buf->itell()-1;
307 while( ch >= 0 && (ch!=right_delm && ch!='=' && !ws(ch)) )
309 if( ch < 0 ) EOB_RETURN();
310 prop_end = buf->itell()-1;
312 while( ch >= 0 && ws(ch) )
314 if( ch == '=' ) ch = buf->next();
315 while( ch >= 0 && ws(ch) )
317 if( ch < 0 ) EOB_RETURN();
318 // set terminating char
325 value_start = buf->itell()-1;
326 while( ch >= 0 && (ch!=term && ch!=right_delm && ch!='\n') )
328 if( ch < 0 ) EOB_RETURN();
329 value_end = buf->itell()-1;
331 int plen = prop_end-prop_start;
332 if( !plen ) continue;
333 int vlen = value_end-value_start;
334 char prop[plen+1], value[vlen+1];
335 const char *coded_prop = (const char *)buf->pos(prop_start);
336 const char *coded_value = (const char *)buf->pos(value_start);
337 // props should not have coded text
338 memcpy(prop, coded_prop, plen);
340 xml->decode(value, coded_value, vlen);
341 if( prop_end > prop_start ) {
342 Property *property = new Property(prop, value);
343 properties.append(property);
345 // skip the terminating char
346 if( ch != right_delm ) ch = buf->next();
348 if( !properties.size() && !title[0] ) ERETURN(", emtpy");
354 FileXML::FileXML(int coded)
358 buffer = new XMLBuffer();
369 int FileXML::terminate_string()
375 int FileXML::rewind()
383 int FileXML::append_newline()
385 append_data("\n", 1);
389 int FileXML::append_tag()
392 append_text(tag.string, tag.used);
397 int FileXML::append_text(const char *text)
400 append_text(text, strlen(text));
404 int FileXML::append_data(const char *text)
407 append_data(text, strlen(text));
411 int FileXML::append_data(const char *text, long len)
413 if( text != 0 && len > 0 )
414 buffer->write(text, len);
418 int FileXML::append_text(const char *text, long len)
420 if( text != 0 && len > 0 ) {
421 int size = coded_length(text, len);
422 char coded_text[size+1];
423 encode(coded_text, text, len);
424 buffer->write(coded_text, size);
430 char* FileXML::get_data()
432 long ofs = buffer->itell();
433 return (char *)buffer->pos(ofs);
435 char* FileXML::string()
437 return (char *)buffer->str();
440 long FileXML::length()
442 return buffer->otell();
445 char* FileXML::read_text()
447 int ch = buffer->next();
448 // filter out first char is new line
449 if( ch == '\n' ) ch = buffer->next();
450 long ipos = buffer->itell()-1;
451 // scan for delimiter
452 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
453 long pos = buffer->itell()-1;
454 if( ch >= 0 ) buffer->iseek(pos);
455 long len = pos - ipos;
456 if( len >= output_length ) {
458 output_length = len+1;
459 output = new char[output_length];
461 decode(output,(const char *)buffer->pos(ipos), len);
465 int FileXML::read_tag()
467 int ch = buffer->next();
469 while( ch >= 0 && ch != left_delm ) ch = buffer->next();
470 if( ch < 0 ) return 1;
472 return tag.read_tag(this);
475 int FileXML::read_data_until(const char *tag_end, char *out, int len)
477 long ipos = buffer->itell();
478 int opos = 0, pos = -1;
479 int ch = buffer->next();
480 for( int olen=len-1; ch>=0 && opos<olen; ch=buffer->next() ) {
481 if( pos < 0 ) { // looking for next tag
482 if( ch == left_delm ) {
483 ipos = buffer->itell()-1;
490 // check for end of match
491 if( !tag_end[pos] && ch == right_delm ) break;
492 // if mismatched, copy prefix to out
493 if( tag_end[pos] != ch ) {
494 out[opos++] = left_delm;
495 for( int i=0; i<pos && opos<olen; ++i )
496 out[opos++] = tag_end[i];
497 if( opos < olen ) out[opos++] = ch;
503 // if end tag is reached, pos is left on the < of the end tag
504 if( pos >= 0 && !tag_end[pos] )
509 int FileXML::read_text_until(const char *tag_end, char *out, int len)
512 int opos = read_data_until(tag_end, data, len);
513 decode(out, data, opos);
517 int FileXML::write_to_file(const char *filename)
519 FILE *out = fopen(filename, "wb");
521 eprintf("write_to_file %d \"%s\": %m\n", __LINE__, filename);
524 int ret = write_to_file(out, filename);
529 int FileXML::write_to_file(FILE *file, const char *filename)
531 strcpy(this->filename, filename);
532 const char *str = string();
533 long len = strlen(str);
534 // Position may have been rewound after storing
535 if( !fwrite(xml_header, xml_header_size, 1, file) ||
536 ( len > 0 && !fwrite(str, len, 1, file) ) ) {
537 eprintf("\"%s\": %m\n", filename);
543 int FileXML::read_from_file(const char *filename, int ignore_error)
546 strcpy(this->filename, filename);
547 FILE *in = fopen(filename, "rb");
550 eprintf("\"%s\" %m\n", filename);
553 fseek(in, 0, SEEK_END);
554 long length = ftell(in);
555 fseek(in, 0, SEEK_SET);
556 char *fbfr = new char[length+1];
558 (void)fread(fbfr, length, 1, in);
560 buffer = new XMLBuffer(fbfr, length, 1);
565 int FileXML::read_from_string(char *string)
567 strcpy(this->filename, "");
568 long length = strlen(string);
569 char *sbfr = new char[length+1];
570 strcpy(sbfr, string);
572 buffer = new XMLBuffer(sbfr, length, 1);
576 void FileXML::set_coding(int coding)
580 decode = XMLBuffer::decode_data;
581 encode = XMLBuffer::encode_data;
582 coded_length = XMLBuffer::encoded_length;
585 decode = XMLBuffer::copy_data;
586 encode = XMLBuffer::copy_data;
587 coded_length = XMLBuffer::copy_length;
591 int FileXML::get_coding()
596 int FileXML::set_shared_input(char *shared_string, long avail, int coded)
598 strcpy(this->filename, "");
600 buffer = new XMLBuffer(shared_string, avail, 0);
605 int FileXML::set_shared_output(char *shared_string, long avail, int coded)
607 strcpy(this->filename, "");
609 buffer = new XMLBuffer(avail, shared_string, 0);
616 // ================================ XML tag
620 int XMLTag::title_is(const char *tp)
622 return !strcasecmp(title, tp) ? 1 : 0;
625 char* XMLTag::get_title()
630 int XMLTag::get_title(char *value)
632 if( title[0] != 0 ) strcpy(value, title);
636 int XMLTag::test_property(char *property, char *value)
638 for( int i=0; i<properties.size(); ++i ) {
639 if( !strcasecmp(properties[i]->prop, property) &&
640 !strcasecmp(properties[i]->value, value) )
646 static inline int xml_cmp(const char *np, const char *sp)
648 const char *tp = ++np;
649 while( *np ) { if( *np != *sp ) return 0; ++np; ++sp; }
653 char *XMLBuffer::decode_data(char *bp, const char *sp, int n)
656 if( n < 0 ) n = strlen(sp);
657 const char *ep = sp + n;
659 if( (n=*sp++) != '&' ) { *bp++ = n; continue; }
660 switch( (n=*sp++) ) {
662 if( (n=xml_cmp("amp;", sp)) ) { *bp++ = '&'; sp += n; continue; }
665 if( (n=xml_cmp("gt;", sp)) ) { *bp++ = '>'; sp += n; continue; }
668 if( (n=xml_cmp("lt;", sp)) ) { *bp++ = '<'; sp += n; continue; }
671 if( (n=xml_cmp("quot;", sp)) ) { *bp++ = '"'; sp += n; continue; }
673 case '#': { // &#<num>;
674 char *cp = 0; int v = strtoul(sp,&cp,10);
675 if( *cp == ';' ) { *bp++ = (char)v; sp = cp+1; continue; }
684 while( --n >= 0 ) *bp++ = *sp++;
691 char *XMLBuffer::encode_data(char *bp, const char *sp, int n)
694 if( n < 0 ) n = strlen(sp);
695 const char *cp, *ep = sp + n;
699 case '<': cp = "<"; break;
700 case '>': cp = ">"; break;
701 case '&': cp = "&"; break;
702 case '"': cp = """; break;
703 default: *bp++ = ch; continue;
705 while( *cp != 0 ) *bp++ = *cp++;
711 long XMLBuffer::encoded_length(const char *sp, int n)
714 if( n < 0 ) n = strlen(sp);
715 const char *ep = sp + n;
719 case '<': len += 4; break;
720 case '>': len += 4; break;
721 case '&': len += 5; break;
722 case '"': len += 6; break;
723 default: ++len; break;
729 char *XMLBuffer::copy_data(char *bp, const char *sp, int n)
731 int len = n < 0 ? strlen(sp) : n;
737 long XMLBuffer::copy_length(const char *sp, int n)
739 int len = n < 0 ? strlen(sp) : n;