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"
32 #include "mainerror.h"
34 // messes up cutads link
36 #define eprintf printf
38 static const char left_delm = '<', right_delm = '>';
39 const char *FileXML::xml_header = "<?xml version=\"1.0\"?>\n";
40 const int FileXML::xml_header_size = strlen(xml_header);
42 XMLBuffer::XMLBuffer(long buf_size, long max_size, int del)
45 bfr = new unsigned char[bsz];
52 XMLBuffer::XMLBuffer(const char *buf, long buf_size, int del)
54 bfr = (unsigned char *)buf;
62 XMLBuffer::XMLBuffer(long buf_size, const char *buf, int del)
64 bfr = (unsigned char *)buf;
72 XMLBuffer::~XMLBuffer()
74 if( destroy ) delete [] bfr;
77 unsigned char *&XMLBuffer::demand(long len)
81 unsigned char *np = new unsigned char[len];
82 if( inp > bfr ) memcpy(np,bfr,inp-bfr);
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, "" _LD "", 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, "" _LD "", (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, "" _LD "", (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::read_data_until(const char *tag_end, char *out, int len)
478 long ipos = buffer->itell();
479 int opos = 0, pos = -1;
480 int ch = buffer->next();
481 for( int olen=len-1; ch>=0 && opos<olen; ch=buffer->next() ) {
482 if( pos < 0 ) { // looking for next tag
483 if( ch == left_delm ) {
484 ipos = buffer->itell()-1;
491 // check for end of match
492 if( !tag_end[pos] && ch == right_delm ) break;
493 // if mismatched, copy prefix to out
494 if( tag_end[pos] != ch ) {
495 out[opos++] = left_delm;
496 for( int i=0; i<pos && opos<olen; ++i )
497 out[opos++] = tag_end[i];
498 if( opos < olen ) out[opos++] = ch;
504 // if end tag is reached, pos is left on the < of the end tag
505 if( pos >= 0 && !tag_end[pos] )
510 int FileXML::read_text_until(const char *tag_end, char *out, int len)
513 int opos = read_data_until(tag_end, data, len);
514 decode(out, data, opos);
518 int FileXML::write_to_file(const char *filename)
520 FILE *out = fopen(filename, "wb");
522 eprintf("write_to_file %d \"%s\": %m\n", __LINE__, filename);
525 int ret = write_to_file(out, filename);
530 int FileXML::write_to_file(FILE *file, const char *filename)
532 strcpy(this->filename, filename);
533 const char *str = string();
534 long len = strlen(str);
535 // Position may have been rewound after storing
536 if( !fwrite(xml_header, xml_header_size, 1, file) ||
537 ( len > 0 && !fwrite(str, len, 1, file) ) ) {
538 eprintf("\"%s\": %m\n", filename);
544 int FileXML::read_from_file(const char *filename, int ignore_error)
547 strcpy(this->filename, filename);
548 FILE *in = fopen(filename, "rb");
551 eprintf("\"%s\" %m\n", filename);
554 fseek(in, 0, SEEK_END);
555 long length = ftell(in);
556 fseek(in, 0, SEEK_SET);
557 char *fbfr = new char[length+1];
559 (void)fread(fbfr, length, 1, in);
561 buffer = new XMLBuffer(fbfr, length, 1);
566 int FileXML::read_from_string(char *string)
568 strcpy(this->filename, "");
569 long length = strlen(string);
570 char *sbfr = new char[length+1];
571 strcpy(sbfr, string);
573 buffer = new XMLBuffer(sbfr, length, 1);
577 void FileXML::set_coding(int coding)
581 decode = XMLBuffer::decode_data;
582 encode = XMLBuffer::encode_data;
583 coded_length = XMLBuffer::encoded_length;
586 decode = XMLBuffer::copy_data;
587 encode = XMLBuffer::copy_data;
588 coded_length = XMLBuffer::copy_length;
592 int FileXML::get_coding()
597 int FileXML::set_shared_input(char *shared_string, long avail, int coded)
599 strcpy(this->filename, "");
601 buffer = new XMLBuffer(shared_string, avail, 0);
606 int FileXML::set_shared_output(char *shared_string, long avail, int coded)
608 strcpy(this->filename, "");
610 buffer = new XMLBuffer(avail, shared_string, 0);
617 // ================================ XML tag
621 int XMLTag::title_is(const char *tp)
623 return !strcasecmp(title, tp) ? 1 : 0;
626 char* XMLTag::get_title()
631 int XMLTag::get_title(char *value)
633 if( title[0] != 0 ) strcpy(value, title);
637 int XMLTag::test_property(char *property, char *value)
639 for( int i=0; i<properties.size(); ++i ) {
640 if( !strcasecmp(properties[i]->prop, property) &&
641 !strcasecmp(properties[i]->value, value) )
647 static inline int xml_cmp(const char *np, const char *sp)
649 const char *tp = ++np;
650 while( *np ) { if( *np != *sp ) return 0; ++np; ++sp; }
654 char *XMLBuffer::decode_data(char *bp, const char *sp, int n)
657 if( n < 0 ) n = strlen(sp);
658 const char *ep = sp + n;
660 if( (n=*sp++) != '&' ) { *bp++ = n; continue; }
661 switch( (n=*sp++) ) {
663 if( (n=xml_cmp("amp;", sp)) ) { *bp++ = '&'; sp += n; continue; }
666 if( (n=xml_cmp("gt;", sp)) ) { *bp++ = '>'; sp += n; continue; }
669 if( (n=xml_cmp("lt;", sp)) ) { *bp++ = '<'; sp += n; continue; }
672 if( (n=xml_cmp("quot;", sp)) ) { *bp++ = '"'; sp += n; continue; }
674 case '#': { // &#<num>;
675 char *cp = 0; int v = strtoul(sp,&cp,10);
676 if( *cp == ';' ) { *bp++ = (char)v; sp = cp+1; continue; }
685 while( --n >= 0 ) *bp++ = *sp++;
692 char *XMLBuffer::encode_data(char *bp, const char *sp, int n)
695 if( n < 0 ) n = strlen(sp);
696 const char *cp, *ep = sp + n;
700 case '<': cp = "<"; break;
701 case '>': cp = ">"; break;
702 case '&': cp = "&"; break;
703 case '"': cp = """; break;
704 default: *bp++ = ch; continue;
706 while( *cp != 0 ) *bp++ = *cp++;
712 long XMLBuffer::encoded_length(const char *sp, int n)
715 if( n < 0 ) n = strlen(sp);
716 const char *ep = sp + n;
720 case '<': len += 4; break;
721 case '>': len += 4; break;
722 case '&': len += 5; break;
723 case '"': len += 6; break;
724 default: ++len; break;
730 char *XMLBuffer::copy_data(char *bp, const char *sp, int n)
732 int len = n < 0 ? strlen(sp) : n;
738 long XMLBuffer::copy_length(const char *sp, int n)
740 int len = n < 0 ? strlen(sp) : n;